Reputation: 3190
So I can do something like
#ifdef MSVC
//do compiler specific code here
#endif
Upvotes: 64
Views: 45619
Reputation: 16940
#if defined(_MSC_VER) && !defined(__clang__)
// code for msvc compiler
#endif
_MSC_VER is used to test MS Visual C/C++ compilers. However, there are other compilers that can be used from Visual Studio, and these compilers usually also define _MSC_VER
. predef is outdated, mentions old Intel compiler:
Intel's compiler also defines _MSC_VER and _MSC_FULL_VER on Windows. (1) You can exclude it by checking that __INTEL_COMPILER is not defined.
__INTEL_COMPILER
is defined by classic intel compiler (aka icc/icl/icpc). On windows icl.exe also defines __ICL
meaning that it uses clang front end.
However, that classic Intel compiler was replaced with a new Intel's llvm-based icx/icpx compiler which no longer defines __INTEL_COMPILER
.
List of some of the compilers that define _MSC_VER
__ICL
, and __INTEL_COMPILER
__clang__
, and __INTEL_LLVM_COMPILER
__clang__
.Note sure if icl.exe also defined __clang__
, if it did not and you want to exclude classical Intel compiler, then perhaps this is all that's needed:
#if defined(_MSC_VER) && !defined(__clang__) && !defined(__INTEL_COMPILER)
// code for msvc compiler
#endif
Upvotes: 2
Reputation: 206859
Look at the list of MSVC predefined macros. You'll find what you need.
_MSC_VER
is probably a good one.
Upvotes: 15
Reputation: 12784
It's _MSC_VER. More info at MSDN and at predef.
But, be aware that some other compilers may also define it, e.g. Intel's C++ Compiler for Windows also defines _MSC_VER. If this is a concern, use #if _MSC_VER && !__INTEL_COMPILER
.
Upvotes: 123