What is the most reliable way to find out CPU architecture when compiling C or C++ code? As far as I can tell, different compilers have their own set of non-standard preprocessor definitions (_M_X86
in MSVS, __i386__
, __arm__
in GCC, etc).
Is there a standard way to detect the architecture I'm building for? If not, is there a source for a comprehensive list of such definitions for various compilers, such as a header with all the boilerplate #ifdef
s?
Here is some information about Pre-defined Architecture Macros and other types of pre-defined macros.
This question asks where they are defined in the GCC source code.
If you need a fine-grained detection of CPU features, the best approach is to ship also a CPUID program which outputs to stdout or some "cpu_config.h" file the set of features supported by the CPU. Then you integrate that program with your build process.
There's nothing standard. Brian Hook documented a bunch of these in his "Portable Open Source Harness", and even tries to make them into something coherent and usable (ymmv regarding that). See the posh.h header on this site:
Note, the link above may require you to enter some bogus userid/password due to a DOS attack some time ago.
If you want a cross-compiler solution then just use
Boost.Predef
which containsBOOST_ARCH_
for system/CPU architecture one is compiling for.BOOST_COMP_
for the compiler one is using.BOOST_LANG_
for language standards one is compiling against.BOOST_LIB_C_
and BOOST_LIB_STD_ for the C and C++ standard library in use.BOOST_OS_
for the operating system we are compiling to.BOOST_PLAT_
for platforms on top of operating system or compilers.BOOST_ENDIAN_
for endianness of the os and architecture combination.BOOST_HW_
for hardware specific features.BOOST_HW_SIMD
for SIMD (Single Instruction Multiple Data) detection.For example
You can find out more on how to use it here
There's no inter-compiler standard, but each compiler tends to be quite consistent. You can build a header for yourself that's something like this:
There's not much point to a comprehensive list, because there are thousands of compilers but only 3-4 in widespread use (Microsoft C++, GCC, Intel CC, maybe TenDRA?). Just decide which compilers your application will support, list their #defines, and update your header as needed.
If you would like to dump all available features on a particular platform, you could run GCC like:
gcc -march=native -dM -E - </dev/null
It would dumps Marcos like
#define __SSE3__ 1
,#define __AES__ 1
, etc.