Hello,
The architectures I tested were armv7 and arm64 on FreeBSD 13. The compiler is clang 11.0.1.
FreeBSD defines _BYTE_ORDER but not __BYTE_ORDER. Likewise, it defines _BIG_ENDIAN but not __BIG_ENDIAN.
You should be able to make your code work with something like this:
#if defined(_BYTE_ORDER) && !defined(__BYTE_ORDER)
# define __BYTE_ORDER _BYTE_ORDER
#endif
I recommend patching the endianess detection code so it checks for the presence of the macros you want to test
in addition to using them (the C preprocessors substitutes 0 for undefined macros). For example, try this:
#if !defined(__BYTE_ORDER) || !defined(__BIG_ENDIAN)
# error cannot determine byte order
#endif
Also consider checking both for big and little endian instead of just assuming little endian if the big endian case does not apply.
This makes the code robust against architectures where the byte order is neither big nor little endian.
Also consider writing endian agnostic code instead of having separate code paths for both endianesses. This also has
the effect of making the code compliant with the strict aliasing rule which your code does not seem to be. For example,
you can use functions like these for accessing fields in files with a defined endianess without having to make assumptions
about the endianess of the architecture you are programming for:
This implementation compiles to reasonable code with modern gcc and clang and allows you to completely avoid
having to detect and deal with host endianess.
Yours,
Robert Clausecker