C
Craig Gullixson
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.
In general I agree that the best solution is to avoid endian
sensitive code, although that is not always possible. It is also
possible to look for pre-defined symbols, although the presence of
such symbols are pretty platform and compiler dependent. In
general, there is no happy solution to this problem.
However, (although I'm straying from a strict discussion of C) for
what it is worth, I use configuration scripts which adjust the
Makefiles and creates a configuration dependent include file for
compile time platform and/or compiler dependancies. The following
is out of one of my configuration scripts and does work on the few
Unixish systems I've tried it on. The border variable eventually
ends up as a #define in a configure time config.h file. I haven't
attempted to adapt this such that it can be directly put into a
Makefile.
YMMV, etc., etc., etc.
--------------------------------------------------------------------------
(this is for sh or equivalent)
##### determine the byte ordering
theborder=`echo "abcd" | od -tx4 | head -1 | awk '{print $2}'`
if [ $theborder -eq 61626364 ] ; then
border="BORDER_BIG_ENDIAN"
elif [ $theborder -eq 64636261 ] ; then
border="BORDER_LITTLE_ENDIAN"
elif [ $theborder -eq 62616463 ] ; then
border="BORDER_PDP_ENDIAN"
else
border="BORDER_UNKNOWN"
fi
--------------------------------------------------------------------------
---Craig
________________________________________________________________________
Craig A. Gullixson
Instrument Engineer INTERNET: (e-mail address removed)
National Solar Observatory/Sac. Peak PHONE: (505) 434-7065
Sunspot, NM 88349 USA FAX: (505) 434-7029