如何确定VS编译器版本--_MSC_VER || #if _MSC_VER > 1000 #pragma once #endif

本文介绍了通过内置宏_MSC_VER来确定Visual Studio编译器版本的方法,并提供了使用示例,帮助开发者根据不同的编译器版本选择合适的库文件。

如何确定VS编译器版本

_MSC_VER是MSVC编译器的内置宏,定义了编译器的版本,_MSC_VER 值对应版本关系

MSVC++ 11.0 _MSC_VER = 1700 (Visual Studio 2012)  
MSVC++ 10.0 _MSC_VER = 1600 (Visual Studio 2010) 
MSVC++ 9.0 _MSC_VER = 1500  (Visual Studio 2008)  
MSVC++ 8.0 _MSC_VER = 1400  (Visual Studio 2005)  
MSVC++ 7.1 _MSC_VER = 1310  (Visual Studio 2003) 
MSVC++ 7.0 _MSC_VER = 1300 (Visual Studio 2002) 
MSVC++ 6.0 _MSC_VER = 1200  
MSVC++ 5.0 _MSC_VER = 1100

 
example:

#if (_MSC_VER == 1300)  //vc7  (Visual Studio 2002) 
    #import "acax16ENU.tlb" no_implementation raw_interfaces_only named_guids
#elif (_MSC_VER == 1200)  //vc6
    #import "acad.tlb" no_implementation raw_interfaces_only named_guids
#elif (_MSC_VER == 1400) //vc8  (Visual Studio 2005)  
    #import "acax17ENU.tlb" no_implementation raw_interfaces_only named_guids
#elif (_MSC_VER == 1500) //vc9  (Visual Studio 2008)  
    #import "acax18ENU.tlb" no_implementation raw_interfaces_only named_guids
#endif


在程序中加入_MSC_VER宏可以根据编译器版本让编译器选择性地编译一段程序。

例如一个版本编译器产生的lib文件可能不能被另一个版

本的编译器调用,那么在开发应用程序的时候,在该程序的lib调用库中放入多个版本编译器产生的lib文件。

在程序中加入_MSC_VER宏,编译器就能够在调用的时根据其版本自动选择可以链接的lib库版本,如下所示。


#if _MSC_VER >= 1400 // for vc8, or vc9

#ifdef _DEBUG
        #pragma comment( lib, "SomeLib-vc8-d.lib" )
#else if
#pragma comment( lib, "SomeLib-vc8-r.lib" )
#endif


#else if _MSC_VER >= 1310 // for vc71
#ifdef _DEBUG
#pragma comment( lib, "SomeLib-vc71-d.lib" )
#else if
#pragma comment( lib, "SomeLib-vc71-r.lib" )
#endif


#else if _MSC_VER >=1200 // for vc6
#ifdef _DEBUG
#pragma comment( lib, "SomeLib-vc6-d.lib" )
#else if
#pragma comment( lib, "SomeLib-vc6-r.lib" )
#endif

#endif


==================================================================================================================

#if _MSC_VER > 1000

#pragma once

#endif

解释:

这是微软的预编译控制。
在_MSC_VER较小时,它对一些东西的支持与新版不同


_MSC_VER分解如下:

MS:Microsoft(微软)的简写

C:MSC就是Microsoft出的C编译器。

VER:Version(版本)的简写。

全部加在一起就是:Microsoft的C编译器的版本


#pragma once   

指示这个文件在编译时只被编译器包括一次!一般用到.h中防止文件被重复包括!   


#if _MSC_VER > 1000 

是指如果vc编译器的版本大于1000则这个语句被编译!大概小于1000的版本不支持#pragma

once这个语句

 

很多头文件中有

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

_MSC_VER 定义编译器的版本,VC++6.0就是1200  
#if   _MSC_VER   >   1000的意思就是如果编译器版本高于1000(VC++5.0)


把下面的window代码修改成移植到linux代码#if defined (_MSC_VER) && (_MSC_VER >= 1000) #pragma once #endif #ifndef _HEADER_BASESYSTEMCONFIG #define _HEADER_BASESYSTEMCONFIG #if _MSC_VER >= 1400 # ifndef _CRT_SECURE_NO_DEPRECATE # define _CRT_SECURE_NO_DEPRECATE # endif // [11/8/2006 Tgame.Tang] //# ifndef _DEBUG //# ifndef _SECURE_SCL //# define _SECURE_SCL 0//在release下关闭STL的安全特性,提高效率 //# else //# if _SECURE_SCL != 0 //# error("_SECURE_SCL must be defined as 0 while compile no debug version,You must include stl after include this file") //# endif //# endif //# endif #endif #pragma warning(disable:4995) #pragma warning (disable:4251) //!系统平台 #if defined(_WIN32) // // Comment out the following block if you want to run on Windows 9x // or Windows NT 3.51. // # ifndef _WIN32_WINNT // // Necessary for TryEnterCriticalSection. // # define _WIN32_WINNT 0x0500 # endif #define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料 # include <windows.h> # include <winsock2.h> # pragma comment( lib, "wsock32.lib" ) # pragma comment( lib, "Ws2_32.lib" ) #elif (defined(__sun) && defined(__sparc)) || (defined(__hpux)) # include <inttypes.h> #else // // The ISO C99 standard specifies that in C++ implementations the // macros for minimum/maximum integer values should only be defined if // explicitly requested with __STDC_LIMIT_MACROS. // # ifndef __STDC_LIMIT_MACROS # define __STDC_LIMIT_MACROS # endif # include <stdint.h> #endif //!字节序 #if defined(__i386) || defined(_M_IX86) || defined (__x86_64) # define SERIAL_LITTLE_ENDIAN #elif defined(__sparc) || defined(__sparc__) || defined(__hppa) || defined(__ppc__) || defined(_ARCH_COM) # define SERIAL_BIG_ENDIAN #else # error "Unknown architecture" #endif //!STL #include <vector> #include <string> #include <cassert> #include <iostream> #include <sstream> #include <string> #include <map> #include <queue> #include <stack> #include <list> #ifdef _Foundation_Import_ # define _Foundation_Export_ __declspec( dllimport ) #else # define _Foundation_Export_ __declspec( dllexport ) //# pragma warning (disable:4251) #endif //#define _Disable_Vt_ #endif
09-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值