c++visual-c++llvmicxicpx

Macro to check if I am running MSVC or Intel compiler


I am using the newest Visual Studio 2022 and also installed the Intel C++ compiler.

When I just write a simple main() to print the values of the macros MSC_VER and __INTEL_LLVM_COMPILER, I get

How is it possible that both have values defined? I must be using the wrong macros, so what's the right macro to check?


Solution

  • Compilers that aim for compatibility with MSVC typically define _MSC_VER. Similar to how compilers that support the GNU dialect of C define __GNUC__ to a version number: How to tell Clang to stop pretending to be other compilers?

    True MSVC will definitely not define __INTEL_LLVM_COMPILER or __llvm__, so

    #if defined(_MSC_VER) && !defined(__llvm__) && !defined(__INTEL_COMPILER)
      // real MSVC, or a different non-LLVM-based compiler emulating it.
    #else
    ...
    #endif
    

    Checking for __llvm__ should be good to exclude mainline Clang and clang-cl as well as Intel's LLVM-based ICX / ICPX and/or OneAPI stuff.

    __INTEL_COMPILER is defined by Intel's "classic" (non-LLVM) compilers, ICC (C) and ICPC (C++).