简体   繁体   English

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

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

Is there any way to know if I'm compiling under a specific Microsoft Visual Studio version?有什么方法可以知道我是否在特定的 Microsoft Visual Studio 版本下编译?

_MSC_VER and possibly _MSC_FULL_VER is what you need. _MSC_VER和可能的_MSC_FULL_VER是您需要的。 You can also examine visualc.hpp in any recent boost install for some usage examples.您还可以在任何最近的 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.上面的版本号当然是指您在 about 框中看到的 Visual Studio 的主要版本,而不是名称中的年份。 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 将开始单调更新其范围,这意味着您应该检查范围,而不是精确的编译器值。

cl.exe /? will give a hint of the used version, eg:将提示使用的版本,例如:

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

Yep _MSC_VER is the macro that'll get you the compiler version.是的 _MSC_VER 是可以为您提供编译器版本的宏。 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. Visual C++ 的最新版本数采用<compiler-major-version>.00.<build-number>形式,其中 00 是次要编号。 So _MSC_VER will evaluate to <major-version><minor-version> .所以_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 .可以使用此处的其他内置预处理器变量访问更详细的版本控制信息(例如编译器内部版本号)。

_MSC_VER should be defined to a specific version number. _MSC_VER 应定义为特定的版本号。 You can either #ifdef on it, or you can use the actual define and do a runtime test.您可以对其使用#ifdef,也可以使用实际定义并进行运行时测试。 (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. :)) (如果出于某种原因您想根据编译它的编译器运行不同的代码?是的,可能您正在寻找#ifdef。:))

By using the _MSC_VER macro.通过使用_MSC_VER宏。

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/维护了一个用于检测特定编译器、操作系统、架构、标准等的宏列表。

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

In visual studio, go to help |在 Visual Studio 中,go 帮助 | about and look at the version of Visual Studio that you're using to compile your app.关于并查看您用于编译应用程序的 Visual Studio 版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确定我正在编译的MacOS版本? - How can I determine which version of MacOS I'm compiling on? 在Visual Studio Code中编译C ++代码 - Compiling C++ code in Visual Studio Code 获取“浮点异常:8”(我使用的是 Visual Studio Code) - Getting "Floating point exception: 8" (I'm using Visual Studio Code) 在Visual Studio 2013中,编译时出现有关缺少函数标头的错误 - In Visual Studio 2013, I'm getting an error about missing function header when compiling 使用Visual Studio编译源代码时出错 - Error when compiling source code with Visual studio 在 Visual Studio 中编译 cpp 代码时出错 - Error Compiling cpp code in visual studio 如何检测我是否正在用 C++ 编译 64 位架构 - How can I detect if I'm compiling for a 64bits architecture in C++ 在 Visual Studio Code 中编译 C++ 代码时如何设置 bigobj 选项? - How to set bigobj option when compiling C++ code in Visual Studio Code? 在Visual Studio 2008 Pro和标准版本中编译项目有什么区别吗? - Is there a difference between compiling projects in Visual Studio 2008 Pro and the Standard version? 如何制作不需要用户使用特定版本的Visual Studio的C ++ API? - How to make a C++ API that does not require user to use a particular version of Visual Studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM