Guide to predefined macros in C++ compilers (gcc, clang, msvc etc.)
When writing portable C++ code you need to write conditional code that depends on compiler used or the OS for which the code is written. To perform those checks you need to check pre-processor macros that various compilers set.
To list gcc’s pre-defined macros: gcc -x c /dev/null -dM -E
The -x c /dev/null -dM -E also works for mingw (which is based on gcc). tdm-gcc不好使,把 /dev/null 换成 nul: 可以。
Option Summary (Using the GNU Compiler Collection (GCC))
- -x language Specify explicitly the language for the following input files (rather than letting the compiler choose a default based on the file name suffix).
- -dM Instead of the normal output, generate a list of ‘#define’ directives for all the macros defined during the execution of the preprocessor, including predefined macros.
- -E Stop after the preprocessing stage; do not run the compiler proper.
... #define __MINGW32__ 1 #define __GNUC__ 10 #define __VERSION__ "10.3.0" #define WINNT 1 #define WIN32 1 // ifdef WIN32和ifdef _WIN32都能用没啥神秘的,因为两个都定义了。按字符算钱的话,还有__WIN32__ #define _WIN32 1 #define __WINNT 1 #define __WINNT__ 1 #define __WIN32__ 1 ... #define __WCHAR_WIDTH__ 16 #define __WCHAR_MAX__ 0xffff ... #define __declspec(x) __attribute__((x)) ... #define __INT64_C(c) c ## LL #define __UINT64_C(c) c ## ULL