C++ 如何检测我是否正在使用特定的 Visual Studio 版本编译代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/70013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:38:03  来源:igfitidea点击:

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

c++visual-studio

提问by Vhaerun

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

有什么方法可以知道我是否在特定的 Microsoft Visual Studio 版本下编译?

回答by jilles de wit

_MSC_VERand possibly _MSC_FULL_VERis what you need. You can also examine visualc.hppin any recent boost install for some usage examples.

_MSC_VER并且可能_MSC_FULL_VER是您所需要的。您还可以在任何最近的 boost 安装中检查visualc.hpp以获取一些使用示例。

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

较新版本的编译器的一些值是:

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.

上面的版本号当然是指您在关于框中看到的 Visual Studio 的主要版本,而不是名称中的年份。可在此处找到完整列表。从最近开始,Visual Studio 将开始单调更新其范围,这意味着您应该检查范围,而不是确切的编译器值。

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

cl.exe /?将给出使用版本的提示,例如:

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
.....

回答by display101

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_VERwill evaluate to <major-version><minor-version>.

是的 _MSC_VER 是可以为您提供编译器版本的宏。Visual C++ 的最后一个版本的形式是<compiler-major-version>.00.<build-number>,其中 00 是次要编号。所以_MSC_VER将评估为<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:

似乎在编译器的连续版本之间进行了更新,没有修改compiler-minor-version,因此不需要以下代码:

#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.

可以使用此处的其他内置预处理器变量找到对更详细的版本信息(例如编译器版本号)的访问

回答by DJ Capelis

_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. :))

_MSC_VER 应定义为特定的版本号。您可以对其进行 #ifdef,也可以使用实际定义并进行运行时测试。(如果出于某种原因,您想根据编译时使用的编译器运行不同的代码?是的,您可能正在寻找 #ifdef。:))

回答by Jeff Hubbard

By using the _MSC_VERmacro.

通过使用_MSC_VER宏。

回答by Clifford

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.

作为更一般的答案,http://sourceforge.net/p/predef/wiki/Home/维护了用于检测特定编译器、操作系统、体系结构、标准等的宏列表。

回答by Clifford

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

回答by Haacked

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

在visual studio中,去帮助| 关于并查看用于编译应用程序的 Visual Studio 版本。