Compiler version, name, and OS detection in C++

前端 未结 4 1434
感动是毒
感动是毒 2021-01-03 05:57

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.

How can I do that?

相关标签:
4条回答
  • 2021-01-03 06:17

    I recommend define platform in build scripts by providing -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

    0 讨论(0)
  • 2021-01-03 06:26

    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. 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. But you can't detect the compiler's executing OS.

    0 讨论(0)
  • 2021-01-03 06:30

    For most compilers you can find a list of predefined macros.

    • VS http://msdn.microsoft.com/en-us/library/b0084kay%28v=vs.80%29.aspx
    • gcc http://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
    0 讨论(0)
  • 2021-01-03 06:31

    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.

    0 讨论(0)
提交回复
热议问题