[CMake] Finding out the Endianness of a system at CMake config time

Sean McBride sean at rogue-research.com
Wed Nov 22 13:56:55 EST 2006


On 2006-11-22 19:00, Eric Noulard said:

>Sorry for my dummy answer:
>
>1) There is a TestBigEndian.cmake already shipped with CMake (as of 2.4.3)
>2) I should have checked ENDIANESS_RESULT and not
>    ENDIANESS_OUT

At the risk of repeating myself... :)

Remember that using TRY_COMPILE/TRY_RUN or any method that runs code on
the build machine breaks cross-compilation.  Notably, if you want to
build a Universal Binary for Mac OS X, you guarantee that it will not
work on one architecture.  Maybe it's not important for some people, but
it's good to be aware of it.

Since a compiler knows what CPU it is compiling for (by definition), I
think a better solution, in general, is to query the compiler.  gcc for
example always #defines __BIG_ENDIAN__ or __LITTLE_ENDIAN__ as
appropriate.  Other compilers probably do likewise, but I am not aware
of a compiler independent way to determine this, which is very unfortunate.

Also, with posix, there are APIs like ntohl() that always do the right
thing.  On some systems, sys/param.h indicates endianness.

-- 
____________________________________________________________
Sean McBride, B. Eng                 sean at rogue-research.com
Rogue Research                        www.rogue-research.com 
Mac Software Developer              Montréal, Québec, Canada




More information about the CMake mailing list