print compiler macro definitions
Compilers define macros that can be used to identify a compiler and platform from within C, C++ and Fortran code.
This can be useful for many purposes where short bits of platform-specific or compiler-specific code is needed.
If a significant amount of code is needed, it may be better to swap in different code files using the build system instead of lengthly #if defined(foo)
logic.
There are numerous examples for C and C++ so here we will focus on macros of Fortran compilers.
Gfortran
Gfortran compiler macro definitions are obtained in an OS-agnostic way by:
echo "" | gfortran -dM -E - > macros.txt
that creates a file “macros.txt” containing all the compiler macros.
commonly used macros to detect operating system / compiler configuration include:
_WIN32 1
__linux__ 1
__unix__ 1
__APPLE__ 1
Intel oneAPI
Intel oneAPI compiler macros include the Gfortran macros noted above and additionally:
__INTEL_COMPILER 1
Nvidia HPC Fortran
Nvidia HPC Fortran compiler macros are printed by:
nvfortran -dM
the Nvidia macros include the Gfortran macros above as well as:
__PGI 1
Flang
Flang macros include
__FLANG 1
Other compilers
Other Fortran compiler macros that identify the compiler and platform can be found in CMake source code.