简体   繁体   English

有没有办法找出代码中的编译器版本?

[英]Is there a way to figure out the version of a compiler in code?

is it possible to get the version of compiler in code? 是否有可能在代码中获得编译器的版本? for example using some compiler directives? 例如使用一些编译器指令?
I am trying to find about the version of a compiler and then lets say if the version of Gcc or Visual C++ is C++11 compliant then compile this bit of code and if not it compile thats snippet instead 我正在尝试查找编译器的版本,然后说GccVisual C++的版本是否符合C++11然后编译这段代码,如果没有,则编译那是代码段

You can use __cplusplus macro to check if compiler supports C++11 so that it will work even on compilers you don't know about. 您可以使用__cplusplus宏来检查编译器是否支持C ++ 11,以便即使在您不知道的编译器上也可以使用。

#if __cplusplus >= 201103L
//C++ 11 code here
#endif

16.8 Predefined macro names 16.8预定义的宏名称

1 The following macro names shall be defined by the __cplusplus The name __cplusplus is defined to the value 201103L when compiling a C++ translation unit. 1以下宏名称应由__cplusplus定义。编译C ++转换单元时,名称__cplusplus定义为值201103L。

157) It is intended that future versions of this standard will replace the value of this macro with a greater value. 157)打算该标准的将来版本将用更大的值替换该宏的值。 Non-conforming compilers should use a value with at most five decimal digits. 不合格的编译器应使用最多包含五个十进制数字的值。

在gcc和clang中,您可以使用__VERSION__宏。

If you want to know what compiler you're using, they have their own predefined macros for that, linked in other comments. 如果您想知道使用的是哪个编译器,则它们具有自己的预定义宏,这些宏在其他注释中链接。 But you're indicating that you are doing this in order to discover the presence of C++11 support. 但是,这表示要这样做是为了发现C ++ 11支持。 In that case, the correct code is 在这种情况下,正确的代码是

#if __cplusplus <= 199711L
    //No C++11 support
#else
    //Congratulations, C++11 support!
#endif

According to the standard, compilers are required to set that variable, and it indicates the version. 根据标准,要求编译器设置该变量,并指示版本。 See it on Bjarne's page Bjarne的页面上查看

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM