Avery3R
Avery3R

Reputation: 3190

Is there a preprocessor define that is defined if the compiler is MSVC?

So I can do something like

#ifdef MSVC
//do compiler specific code here
#endif

Upvotes: 64

Views: 45619

Answers (5)

Pavel P
Pavel P

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

  • classical Intel's compilers. These define __ICL, and __INTEL_COMPILER
  • new Intel's llvm-based compilers. These define __clang__, and __INTEL_LLVM_COMPILER
  • clang-cl that defines __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

Mat
Mat

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

Alexey Kukanov
Alexey Kukanov

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

Will A
Will A

Reputation: 25008

_MSC_VER is one such predefined macro.

Upvotes: 2

mbx
mbx

Reputation: 6545

_MSC_VER should fit your needs

Upvotes: 2

Related Questions