简体   繁体   English

C ++中的编译器版本,名称和OS检测

[英]Compiler version, name, and OS detection in C++

I need to detect the OS name, compiler name, and version of the compiler with C++, as I need to change the setup for each case. 我需要使用C ++检测操作系统名称,编译器名称和编译器版本,因为我需要更改每种情况的设置。

How can I do that? 我怎样才能做到这一点?

I recommend define platform in build scripts by providing -D_i386 -DENDIAN=1234 -D_linux . 我建议通过提供-D_i386 -DENDIAN = 1234 -D_linux在构建脚本中定义平台。 But if you still think another predef project is your friend: 但如果您仍然认为另一个预先设定的项目是您的朋友:

http://sourceforge.net/apps/mediawiki/predef/index.php?title=Main_Page http://sourceforge.net/apps/mediawiki/predef/index.php?title=Main_Page

You won't be able to detect the operating system at compile-time. 您将无法在编译时检测到操作系统。 You will, however, be able to determine the compiler- virtually all compilers define macros indicating their presence, like __GNUC__ or something like that for GCC and MSVC has __MSC_VER__ or something similar. 但是,您将能够确定编译器 - 实际上所有编译器都定义了指示其存在的宏,如__GNUC__或类似GCC和MSVC具有__MSC_VER__或类似的东西。 You'll have to check their documentation for the actual macro names, I've forgotten. 您必须检查他们的文档中的实际宏名称,我已经忘记了。

Edit: For clarification, you can check what system's headers are included. 编辑:为了澄清,您可以检查包含哪些系统的标头。 For example, the Windows headers define a number of macros like WINNT_VER which give the minimum version of Windows to be targetted. 例如,Windows标头定义了许多宏,如WINNT_VER ,它们提供了最低版本的Windows。 But you can't detect the compiler's executing OS. 但是你无法检测编译器执行的操作系统。

Usually you leave that task to the build environment. 通常,您将该任务留给构建环境。 Either using commands like uname if you can assume a posixy set up, or by any other mean which is deemed suitable. 如果你可以假设一个posixy设置,或者通过任何其他被认为合适的方法,可以使用像uname这样的命令。

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

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