4

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?

prosseek
  • 182,215
  • 215
  • 566
  • 871

4 Answers4

6

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

Elalfer
  • 5,312
  • 20
  • 25
3

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

gavenkoa
  • 45,285
  • 19
  • 251
  • 303
1

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.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • 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
1

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.

AProgrammer
  • 51,233
  • 8
  • 91
  • 143