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 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:
Intel oneAPI compiler macros include the Gfortran macros noted above and additionally:
Nvidia HPC Fortran
Nvidia HPC Fortran compiler macros are printed by:
the Nvidia macros include the Gfortran macros above as well as:
Flang macros include
Other Fortran compiler macros that identify the compiler and platform can be found in CMake source code.