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 lengthy #ifdef
logic.
There are numerous examples for C and C++ so here we focus on macros of Fortran compilers.
Gfortran compiler macros
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.
Clang LLVM compiler macros
LLVM-like compilers (including AMD AOCC) macro definitions are obtained in an OS-agnostic way by:
echo "" | clang -dM -E - > macros.txt
that creates a file “macros.txt” containing all the compiler macros.
__VERSION__
and __clang_version__
contain string version information.
Intel oneAPI LLVM compiler macros
oneAPI macros set:
__INTEL_LLVM_COMPILER 1
to distinguish from oneAPI Classic
compiler macros
like __INTEL_COMPILER 1
Cray compiler macros
Detect Cray
compiler wrapper
with compiler macro __CRAYXT_COMPUTE_LINUX_TARGET
instead than non-universal __CRAYXC
or __CRAYXE
.
or use Cray environment variable PE_ENV
and
check for CRAY
or PrgEnv-
.
NVIDIA HPC compiler macros
Print compiler macros like:
touch main.c main.cpp main.f90
nvc -dryrun main.c
nvc++ -dryrun main.cpp
nvfortran -dryrun main.f90
NVIDIA HPC macros include:
__NVCOMPILER
__NVCOMPILER_LLVM__
Flang-f18 compiler macros
Flang-f18 (flang-new) Fortran compiler
macros
include __flang__ 1
Other Fortran compiler macros that identify the compiler and platform can be found in CMake source code.