468,491 Members | 1,961 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

Any Method to Determine Endianness at Compile Time ?

How could I determine the endianness of my compile environment at
compile time, instead of run time? I need a macro ("some_expression"),
i.e.

#if some_expression
#define TARGET_IS_LITTLE_ENDIAN
#else
#define TARGET_IS_BIG_ENDIAN

No way or some way? TIA.
Mar 19 '08 #1
19 6461
In article <27**********************************@c19g2000prf. googlegroups.com>,
perry.yuan <pe********@gmail.comwrote:
>How could I determine the endianness of my compile environment at
compile time, instead of run time?
You can't write a compile-time expression to determine it, because
endianness is a property of representations, not values, and there
are no objects whose representations can be examined at compile
time.

One solution is to run a program at compile time that determines
the endianness and outputs a suitable #define to a file which
you then include.

-- Richard
--
:wq
Mar 19 '08 #2
"perry.yuan" <pe********@gmail.comwrites:
How could I determine the endianness of my compile environment at
compile time, instead of run time? I need a macro ("some_expression")
One way this is commonly done is to build in two stages. First,
compile and run a test program that tests for the
implementation's endianness. Then use the test program's output
to configure macros to be defined while building the rest of the
program.
--
"I've been on the wagon now for more than a decade. Not a single goto
in all that time. I just don't need them any more. I don't even use
break or continue now, except on social occasions of course. And I
don't get carried away." --Richard Heathfield
Mar 19 '08 #3
perry.yuan wrote:
How could I determine the endianness of my compile environment at
compile time, instead of run time? I need a macro ("some_expression"),
i.e.

#if some_expression
#define TARGET_IS_LITTLE_ENDIAN
#else
#define TARGET_IS_BIG_ENDIAN

No way or some way? TIA.
This is Question 10.16 in the comp.lang.c Frequently
Asked Questions (FAQ) list <http://www.c-faq.com/>. You've
been around this newsgroup long enough to have seen the
FAQ mentioned several dozens of times; shame on you for
not bothering to read it.

--
Er*********@sun.com
Mar 19 '08 #4
perry.yuan wrote:
How could I determine the endianness of my compile environment at
compile time, instead of run time? I need a macro ("some_expression"),
i.e.

#if some_expression
#define TARGET_IS_LITTLE_ENDIAN
#else
#define TARGET_IS_BIG_ENDIAN

No way or some way? TIA.
#include <stdio.h>
int main(void)
{
union {
char c;
int i;
} u;
u.i = 0;
u.c = 1;

if (u.i == 1)
printf("little endian\n");
else
printf("big endian\n");
}

This printsd "little endian" in the intel processor,
"big endian" in the power pc. I think it should work.

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Mar 19 '08 #5
jacob navia wrote:
perry.yuan wrote:
>How could I determine the endianness of my compile environment at
compile time, instead of run time? I need a macro ("some_expression"),
i.e.

#if some_expression
#define TARGET_IS_LITTLE_ENDIAN
#else
#define TARGET_IS_BIG_ENDIAN

No way or some way? TIA.

#include <stdio.h>
int main(void)
{
union {
char c;
int i;
} u;
u.i = 0;
u.c = 1;

if (u.i == 1)
printf("little endian\n");
else
printf("big endian\n");
}

This printsd "little endian" in the intel processor,
"big endian" in the power pc. I think it should work.
Sorry, I missed the "preprocessor" part.
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Mar 19 '08 #6
jacob wrote:
) perry.yuan wrote:
)How could I determine the endianness of my compile environment at
)compile time, instead of run time? I need a macro ("some_expression"),
^^^^^^^ ^^^

) int main(void)
) {
) union {
) char c;
) int i;
) } u;
) u.i = 0;
) u.c = 1;
)
) if (u.i == 1)
) printf("little endian\n");
) else
) printf("big endian\n");
) }

Isn't that a run time solution ?
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Mar 19 '08 #7
jacob navia <ja***@nospam.comwrites:
perry.yuan wrote:
>How could I determine the endianness of my compile environment at
compile time, instead of run time? I need a macro ("some_expression"),

This printsd "little endian" in the intel processor,
"big endian" in the power pc. I think it should work.
I don't think you actually read the OP's question.
--
Ben Pfaff
http://benpfaff.org
Mar 19 '08 #8
In article <sl********************@snail.stack.nl>,
Willem <wi****@stack.nlwrote:
>) int main(void)
) {
) union {
) char c;
) int i;
) } u;
) u.i = 0;
) u.c = 1;
)
) if (u.i == 1)
) printf("little endian\n");
) else
) printf("big endian\n");
) }
>Isn't that a run time solution ?
Not if you run it at compile time :-)

-- Richard
--
:wq
Mar 19 '08 #9
Richard Tobin wrote:
In article <sl********************@snail.stack.nl>,
Willem <wi****@stack.nlwrote:
>) int main(void)
) {
) union {
) char c;
) int i;
) } u;
) u.i = 0;
) u.c = 1;
)
) if (u.i == 1)
) printf("little endian\n");
) else
) printf("big endian\n");
) }
>Isn't that a run time solution ?

Not if you run it at compile time :-)

-- Richard
I sent a message recognizing my error around 30 seconds
after I sent the first answer.

Again:

Excuse, it was a mistake
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Mar 19 '08 #10
"perry.yuan" wrote:
>
How could I determine the endianness of my compile environment at
compile time, instead of run time? I need a macro ("some_expression"),
i.e.

#if some_expression
#define TARGET_IS_LITTLE_ENDIAN
#else
#define TARGET_IS_BIG_ENDIAN

No way or some way? TIA.
Hmm. If you're willing to run another program before the compile,
you could run something like this from your compiler script or
makefile (although it need only be run once on any given target
machine):

/* Method of determining endian-ness is Jacob Navia's */
/* Adjust path and filename to suit your situation */

#include <stdio.h>
#include <stdlib.h>
int main(void)
{ FILE *fp;
union
{ char c;
int i;
} u;
u.i = 0;
u.c = 1;
if (fp = fopen("/usr/include/endian.h","w"))
{ fprintf(fp,"#define %S_ENDIAN\n",(u.i&1)?"LITTLE":"BIG");
fclose(fp);
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}

then in your application just

#include <endian.h>

#ifdef BIG_ENDIAN
/* Big endian code */
#else
/* Little endian code */
#endif

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Mar 19 '08 #11

"perry.yuan" wrote:
How could I determine the endianness of my compile environment at
compile time, instead of run time?
Read the manuals for your CPU, operating system, and compiler.
I need a macro
You could use a macro to do conditional compilation, yes. Your compiler
might even provide you with endian-ness macros.

But why not do the checking at run-time instead? Your code would be
more portable that way. Read Jacob Navia's reply to your post.
I'd recommend that approach.

Another method I've seen is, use a char pointer to point to the 0th
byte of an int which is set to 65. If the char pointed to is "A",
you're little endian. If it's the NUL character instead, you're
big-endian.

--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant

Mar 19 '08 #12
In article <Gs******************************@giganews.com>,
Robbie Hatley <se**************@for.my.email.addresswrote:
>How could I determine the endianness of my compile environment at
compile time, instead of run time?
>Read the manuals for your CPU, operating system, and compiler.
This is a stupid, unhelpful answer. The aim is obviously to produce a
system that works on systems you don't necessarily have.
>But why not do the checking at run-time instead?
That is often sufficient, but is sometimes unacceptably inefficient
(or verbose, if you replicate large chunks of code). If efficiency is
important, arrange to run a program at compile time to determine the
answer.
>Another method I've seen is, use a char pointer to point to the 0th
byte of an int which is set to 65. If the char pointed to is "A",
you're little endian.
Why confuse the issue by bringing in ASCII codes?

-- Richard
--
:wq
Mar 19 '08 #13
Richard Tobin wrote:
Willem <wi****@stack.nlwrote:
>>int main(void) {
union {
char c;
int i;
} u;
u.i = 0;
u.c = 1;

if (u.i == 1)
printf("little endian\n");
else
printf("big endian\n");
}
>Isn't that a run time solution ?

Not if you run it at compile time :-)
On the cross-compiler !!

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com

Mar 19 '08 #14
"Robbie Hatley" <se**************@for.my.email.addresswrites:
"perry.yuan" wrote:
>How could I determine the endianness of my compile environment at
compile time, instead of run time?

Read the manuals for your CPU, operating system, and compiler.
>I need a macro

You could use a macro to do conditional compilation, yes. Your compiler
might even provide you with endian-ness macros.

But why not do the checking at run-time instead? Your code would be
more portable that way. Read Jacob Navia's reply to your post.
I'd recommend that approach.

Another method I've seen is, use a char pointer to point to the 0th
byte of an int which is set to 65. If the char pointed to is "A",
you're little endian. If it's the NUL character instead, you're
big-endian.
And the reason you can't use an int or long of value 1 is?
Mar 19 '08 #15
user923005 wrote:
We have to compiler for dozens of platforms here. We just figure out
the endianness before hand and compile with an appropriate macro
definition.
That would be my approach, as well - but I don't generally find
myself compiling here for other platforms. My normal mode of
operation has been to code, compile, and (at least) unit test on
this (nearly ancient) machine - then recompile and test on the
target machine. Whenever a cross-compiler has been needed, it's
always been available at the client's site.

Heh - I just realized that this is the same machine I first used
to read this group. I guess that makes it a "keeper". :-)

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Mar 19 '08 #16
There is no way in Standard C. But read the manual(s) for all
compilers that you are using. Most compilers will provide the
information that you need in some form. Write a header file that
checks for all compilers that you can identify and defines the macro
that you want, and compiles a #error statement if the compiler is not
one that you can identify.
Mar 19 '08 #17
"sr******@gmail.com" <sr******@gmail.comwrites:
[...]
>How could I determine the endianness of my compile environment at
compile time, instead of run time? I need a macro ("some_expression"),
i.e.
>#if some_expression
#define TARGET_IS_LITTLE_ENDIAN
#else
#define TARGET_IS_BIG_ENDIAN
[...]

The above (starting with "How could I determine") was written by
"perry.yuan" <pe********@gmail.com>. The attribution was lost because
Gordon Burditt, as usual, deliberately deleted it.
Probably declaring the option in MAKEFILE as a switch will take care
to compile for BIG-ENDIAN or LITTLE-ENDIAN. CISC are LE type whereas
RISC are BE type, one has to choice the option and then build which
takes care at compile time.
I don't believe there any correlation between big-endian
vs. little-endian and RISC vs. CISC. As far as I know, all four
combinations exist.

The best approach, if it's practical, is to write your code so it
doesn't matter whether the platform is big-endian or little-endian.
(This may not always be practical.)

(I do not grant permission to quote this, or anything else I write
here, without attribution.)

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 23 '08 #18
(I do not grant permission to quote this, or anything else I write
here, without attribution.)
Noted.

--
Richard Heathfield :-) <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Mar 23 '08 #19
Richard Heathfield <rj*@see.sig.invalidwrites:
>(I do not grant permission to quote this, or anything else I write
here, without attribution.)

Noted.
Retroactive exemptions may be granted if the response is sufficiently
humorous.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 23 '08 #20

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Lenard Lindstrom | last post: by
2 posts views Thread by Kapil Khosla | last post: by
3 posts views Thread by kelvSYC | last post: by
2 posts views Thread by SSM | last post: by
2 posts views Thread by Jeff User | last post: by
18 posts views Thread by friend.05 | last post: by
134 posts views Thread by jacob navia | last post: by
6 posts views Thread by gg9h0st | last post: by
18 posts views Thread by Indian.croesus | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.