This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Robust detection of endianness at compile time.
- From: Andrew Haley <aph-gcc at littlepinkcloud dot COM>
- To: "Lee Rhodes" <lee at rhoba dot com>
- Cc: <gcc-help at gcc dot gnu dot org>
- Date: Sun, 29 Jul 2007 11:02:15 +0100
- Subject: Re: Robust detection of endianness at compile time.
- References: <002b01c7d1c1$0078ab70$0701a8c0@lee8075b>
Lee Rhodes writes:
> This issue started in a previous thread, but I have studied it a bit more
> and decided to create another thread with a more focused title. Hopefully,
> there is an expert in the readers of this forum that can explain the whys.
>
> There doesn't appear to be a simple and robust (i.e., cross-platform) way of
> determining the endianness of the underlying platform at compile-time.
Yes, there is.
Endianness, like hundreds of other environment quersies needed at
compile time, is handled by GNU Autoconf. There's nothing special
about endianness: it's just another property of the environment you're
compiling for.
The macro you need is here:
? Macro: AC_C_BIGENDIAN ([action-if-true], [action-if-false], [action-if-unknown])
If words are stored with the most significant byte first (like
Motorola and SPARC CPUs), execute action-if-true. If words are
stored with the least significant byte first (like Intel and VAX
CPUs), execute action-if-false.
This macro runs a test-case if endianness cannot be determined
from the system header files. When cross-compiling, the test-case
is not run but grep'ed for some magic values. action-if-unknown is
executed if the latter case fails to determine the byte sex of the
host system.
The default for action-if-true is to define `WORDS_BIGENDIAN'. The
default for action-if-false is to do nothing. And finally, the
default for action-if-unknown is to abort configure and tell the
installer which variable he should preset to bypass this test.
> 5. Where is the documentation on how one should write cross-platform code
> wrt endianess using GCC?
http://www.gnu.org/software/autoconf/manual/autoconf.html
Andrew.