Compiler macro definitions

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 #ifdef logic. 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:

  • _WIN32 1
  • __linux__ 1
  • __unix__ 1
  • __APPLE__ 1

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_COMPILER 1

Intel oneAPI Next Gen LLVM compiler macros set:

  • __INTEL_LLVM_COMPILER 1

to distinguish from oneAPI Classic compilers.


Cray ftn Fortran compiler macros include:

  • _CRAYFTN

Nvidia HPC Fortran compiler macros are printed by:

nvfortran -dM

Nvidia HPC macros include:

  • __NVCOMPILER
  • __NVCOMPILER_LLVM__

Flang macros include

  • __FLANG 1

Other Fortran compiler macros that identify the compiler and platform can be found in CMake source code.