How to Detect if I'm Compiling Code with a particular Visual Studio version?
Asked Answered
F

7

280

Is there any way to know if I'm compiling under a specific Microsoft Visual Studio version?

Federalize answered 16/9, 2008 at 7:15 Comment(0)
B
543

_MSC_VER and possibly _MSC_FULL_VER is what you need. You can also examine visualc.hpp in any recent boost install for some usage examples.

Some values for the more recent versions of the compiler are:

MSVC++ 14.30 _MSC_VER == 1933 (Visual Studio 2022 version 17.3.4)
MSVC++ 14.30 _MSC_VER == 1932 (Visual Studio 2022 version 17.2.2)
MSVC++ 14.30 _MSC_VER == 1930 (Visual Studio 2022 version 17.0.2)
MSVC++ 14.30 _MSC_VER == 1930 (Visual Studio 2022 version 17.0.1)
MSVC++ 14.28 _MSC_VER == 1929 (Visual Studio 2019 version 16.11.2)
MSVC++ 14.28 _MSC_VER == 1928 (Visual Studio 2019 version 16.9.2)
MSVC++ 14.28 _MSC_VER == 1928 (Visual Studio 2019 version 16.8.2)
MSVC++ 14.28 _MSC_VER == 1928 (Visual Studio 2019 version 16.8.1)
MSVC++ 14.27 _MSC_VER == 1927 (Visual Studio 2019 version 16.7)
MSVC++ 14.26 _MSC_VER == 1926 (Visual Studio 2019 version 16.6.2)
MSVC++ 14.25 _MSC_VER == 1925 (Visual Studio 2019 version 16.5.1)
MSVC++ 14.24 _MSC_VER == 1924 (Visual Studio 2019 version 16.4)
MSVC++ 14.23 _MSC_VER == 1923 (Visual Studio 2019 version 16.3)
MSVC++ 14.22 _MSC_VER == 1922 (Visual Studio 2019 version 16.2)
MSVC++ 14.21 _MSC_VER == 1921 (Visual Studio 2019 version 16.1)
MSVC++ 14.2  _MSC_VER == 1920 (Visual Studio 2019 version 16.0)
MSVC++ 14.16 _MSC_VER == 1916 (Visual Studio 2017 version 15.9)
MSVC++ 14.15 _MSC_VER == 1915 (Visual Studio 2017 version 15.8)
MSVC++ 14.14 _MSC_VER == 1914 (Visual Studio 2017 version 15.7)
MSVC++ 14.13 _MSC_VER == 1913 (Visual Studio 2017 version 15.6)
MSVC++ 14.12 _MSC_VER == 1912 (Visual Studio 2017 version 15.5)
MSVC++ 14.11 _MSC_VER == 1911 (Visual Studio 2017 version 15.3)
MSVC++ 14.1  _MSC_VER == 1910 (Visual Studio 2017 version 15.0)
MSVC++ 14.0  _MSC_VER == 1900 (Visual Studio 2015 version 14.0)
MSVC++ 12.0  _MSC_VER == 1800 (Visual Studio 2013 version 12.0)
MSVC++ 11.0  _MSC_VER == 1700 (Visual Studio 2012 version 11.0)
MSVC++ 10.0  _MSC_VER == 1600 (Visual Studio 2010 version 10.0)
MSVC++ 9.0   _MSC_FULL_VER == 150030729 (Visual Studio 2008, SP1)
MSVC++ 9.0   _MSC_VER == 1500 (Visual Studio 2008 version 9.0)
MSVC++ 8.0   _MSC_VER == 1400 (Visual Studio 2005 version 8.0)
MSVC++ 7.1   _MSC_VER == 1310 (Visual Studio .NET 2003 version 7.1)
MSVC++ 7.0   _MSC_VER == 1300 (Visual Studio .NET 2002 version 7.0)
MSVC++ 6.0   _MSC_VER == 1200 (Visual Studio 6.0 version 6.0)
MSVC++ 5.0   _MSC_VER == 1100 (Visual Studio 97 version 5.0)

The version number above of course refers to the major version of your Visual studio you see in the about box, not to the year in the name. A thorough list can be found here. Starting recently, Visual Studio will start updating its ranges monotonically, meaning you should check ranges, rather than exact compiler values.

cl.exe /? will give a hint of the used version, e.g.:

c:\program files (x86)\microsoft visual studio 11.0\vc\bin>cl /?
Microsoft (R) C/C++ Optimizing Compiler Version 17.00.50727.1 for x86
.....
Bren answered 16/9, 2008 at 7:15 Comment(6)
Sorry, but why don't Microsoft make this information easy to find? this post is very useful but shouldn't be required.Trinatte
It appear that this macro doesn't reflect the compiler version used by the project. I.e. if you open a VS2010 project in a newer version without upgrading the project this macro doesn't reflect the compiler being used - only the IDE version.Vibes
I'm in the process of compilinf libFLAC together with some other libraries. They require compilation to be done using toolset v100. There's a code #if defined _MSC_VER # if _MSC_VER >= 1800 # include <inttypes.h>. inttypes.h cannot be found, though. Very strangeSejant
The place you linked to claims VS 2017 is MSVC++ version 15.0, but your answer claims it's MSVC++ 14.1. Which one is correct?Tuppeny
@Vibes : This macro exactly reflects the Toolset version used by the selected target of the build project. Or more distinct: Project->Properties->General->Platform Toolset.Sailfish
@chrisw We now document this at learn.microsoft.com/en-us/cpp/preprocessor/…, see _MSC_VER . Hope this helpsDupion
I
51

Yep _MSC_VER is the macro that'll get you the compiler version. The last number of releases of Visual C++ have been of the form <compiler-major-version>.00.<build-number>, where 00 is the minor number. So _MSC_VER will evaluate to <major-version><minor-version>.

You can use code like this:

#if (_MSC_VER == 1500)
   // ... Do VC9/Visual Studio 2008 specific stuff
#elif (_MSC_VER == 1600)
   // ... Do VC10/Visual Studio 2010 specific stuff
#elif (_MSC_VER == 1700)
   // ... Do VC11/Visual Studio 2012 specific stuff
#endif

It appears updates between successive releases of the compiler, have not modified the compiler-minor-version, so the following code is not required:

#if (_MSC_VER >= 1500 && _MSC_VER <= 1600)
   // ... Do VC9/Visual Studio 2008 specific stuff
#endif

Access to more detailed versioning information (such as compiler build number) can be found using other builtin pre-processor variables here.

Invercargill answered 24/9, 2010 at 10:25 Comment(5)
you just need to check == 1500 in that caseBrotherhood
@José, true we could simplify it to just check for VC++ 9 with _MSC_VER_ == 1500, however if MS, did modify the _MSC_VER with compiler updates, service packs, etc (I don't think they ever have), then the == 1500 check could break. Which is why I've coded it that way.Invercargill
__MSC_VER evaluate to the major and minor number components of the compiler's version. This will not change with an update, there is _MSC_FULL_VER with include the build number too, i have never need to use that.Brotherhood
I will up-vote the answer if you edit it to clarify this. Bests Jose.Brotherhood
@José: Answer updated to give a more correct and detailed answer.Invercargill
T
11

_MSC_VER should be defined to a specific version number. You can either #ifdef on it, or you can use the actual define and do a runtime test. (If for some reason you wanted to run different code based on what compiler it was compiled with? Yeah, probably you were looking for the #ifdef. :))

Toogood answered 16/9, 2008 at 7:19 Comment(0)
B
7

By using the _MSC_VER macro.

Banzai answered 16/9, 2008 at 7:21 Comment(0)
C
4

As a more general answer http://sourceforge.net/p/predef/wiki/Home/ maintains a list of macros for detecting specicic compilers, operating systems, architectures, standards and more.

Cryptocrystalline answered 22/5, 2013 at 18:41 Comment(0)
S
2

This is a little old but should get you started:

//******************************************************************************
// Automated platform detection
//******************************************************************************

// _WIN32 is used by
// Visual C++
#ifdef _WIN32
#define __NT__
#endif

// Define __MAC__ platform indicator
#ifdef macintosh
#define __MAC__
#endif

// Define __OSX__ platform indicator
#ifdef __APPLE__
#define __OSX__
#endif

// Define __WIN16__ platform indicator 
#ifdef _Windows_
#ifndef __NT__
#define __WIN16__
#endif
#endif

// Define Windows CE platform indicator
#ifdef WIN32_PLATFORM_HPCPRO
#define __WINCE__
#endif

#if (_WIN32_WCE == 300) // for Pocket PC
#define __POCKETPC__
#define __WINCE__
//#if (_WIN32_WCE == 211) // for Palm-size PC 2.11 (Wyvern)
//#if (_WIN32_WCE == 201) // for Palm-size PC 2.01 (Gryphon)  
//#ifdef WIN32_PLATFORM_HPC2000 // for H/PC 2000 (Galileo)
#endif
Scratchy answered 17/9, 2008 at 8:55 Comment(3)
I like this answer a lot more than the top voted one. Because _MSC_VER requires that you include stdio.hIntelsat
_MSC_VER does not require you to include stdio.h, which can be easily tested echo int i = _MSC_VER; > tmp.c cl /nologo /c tmp.cExhortation
@J.M.I.MADISON : Not only is it not true regarding MSC_VER which is a _predefined macro, this answer clearly does not answer the question - it detects the platform, not the compiler version version.Cryptocrystalline
R
-9

In visual studio, go to help | about and look at the version of Visual Studio that you're using to compile your app.

Radmen answered 16/9, 2008 at 7:18 Comment(3)
This can't be serious. Especially from someone with a reputation like this. I assume you were just making a joke?Icsh
I think he just interpreted the question in a different manner. After all, this is one of the first answers to the question.Nameless
AFAIK @Radmen is valid and correct, as the question does not include "from my code" or other statement telling that he/she is not already using a VS IDE.Inlier

© 2022 - 2024 — McMap. All rights reserved.