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

前端 未结 4 1450
感动是毒
感动是毒 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: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.

提交回复
热议问题