Search code examples
c++visual-c++clangclang-cl

Distinguish between Clang CL and MSVC CL


There is CLang-CL which is a drop-in replacement for MSVC's CL.

Does anyone know how to distinguish if my code is currently compiled by clang-cl or msvc's cl? Without passing any extra defined macros on command line.

Using

#ifdef _MSC_VER
//.....
#endif

doesn't work, both compilers define _MSC_VER.

Also in regular CLang on Linux (Windows too) it was possible to do clang -dM -E - < /dev/null which dumps all defined macros. But clang-cl and msvc-cl both don't have such option to dump all defined macros as far as I know, so I don't know of a way to see a difference in list of defined macros for both compilers to figure out which macro to use to distinguish between these compilers.


Solution

  • The macro you're looking for is __clang__.

    Note that the regular Clang (not only Clang-CL) also defines it, so you want to check for both __clang__ and _MSC_VER at the same time.