Compilers define macros that can be used to identify a compiler and platform from compiled code, such as C, C++, Fortran, et al.
This can be used for platform-specific or compiler-specific code.
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
There are numerous examples for C and C++ so here we 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:
CAUTION: these macros are actually not available in the Gfortran compiled programs as they are in GCC. A workaround is to have the build system define these for the particular compiler, OS, etc.
Intel oneAPI classic compiler macros include the Gfortran macros noted above and additionally:
Intel oneAPI Next Gen LLVM compiler macros set:
to distinguish from oneAPI Classic compilers.
Cray ftn Fortran compiler macros include:
Nvidia HPC compiler macros are printed by:
touch main.c main.cpp main.f90 nvc -dryrun main.c nvc++ -dryrun main.cpp nvfortran -dryrun main.f90
Nvidia HPC macros include:
Flang macros include
Other Fortran compiler macros that identify the compiler and platform can be found in CMake source code.