Take the 2-minute tour ×

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?

share|improve this question
runtime or compile-time? gcc or cl (VS)? –  aponomarenko Jan 18 '11 at 14:02

4 Answers 4

up vote 5 down vote accepted

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

share|improve this answer
See also predef.sourceforge.net for a collection. –  AProgrammer Jan 18 '11 at 14:14

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:


share|improve this answer

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.

share|improve this answer

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.

share|improve this answer
Using #ifdef, I did conditional compilation based on OS. So, doesn't it mean that compiler found the underlying OS. Am I wrong ? –  Mahesh Jan 18 '11 at 14:11
@Mahesh: That's target OS, not compiler host OS. –  Puppy Jan 18 '11 at 14:16
So, it's end system OS on which the application is going to be installed. Thanks for the info. –  Mahesh Jan 18 '11 at 14:18

Your Answer


