prosseek
prosseek

Reputation: 191109

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.

How can I do that?

Upvotes: 4

Views: 4233

Answers (5)

phuclv
phuclv

Reputation: 41972

If Boost is available then you can use BOOST_COMP_*, BOOST_OS_* and BOOST_PLAT_* in Boost.Predef which contains various predefined macros for determining the compiler and the target platform

This library defines a set of compiler, architecture, operating system, library, and other version numbers from the information it can gather of C, C++, Objective C, and Objective C++ predefined macros or those defined in generally available headers. The idea for this library grew out of a proposal to extend the Boost Config library to provide more, and consistent, information than the feature definitions it supports. What follows is an edited version of that brief proposal.

...

  • BOOST_ARCH_ for system/CPU architecture one is compiling for.
  • BOOST_COMP_ for the compiler one is using.
  • BOOST_LANG_ for language standards one is compiling against.
  • BOOST_LIB_C_ and BOOST_LIB_STD_ for the C and C++ standard library in use.
  • BOOST_OS_ for the operating system we are compiling to.
  • BOOST_PLAT_ for platforms on top of operating system or compilers.
  • BOOST_ENDIAN_ for endianness of the os and architecture combination.
  • BOOST_HW_ for hardware specific features.
  • BOOST_HW_SIMD for SIMD (Single Instruction Multiple Data) detection.

For example

#include <boost/predef.h>
// or just include the necessary header
// #include <boost/predef/os.h>

#if   BOOST_OS_WINDOWS
#elif BOOST_OS_ANDROID
#elif BOOST_OS_LINUX
#elif BOOST_OS_BSD
#elif BOOST_OS_AIX
#elif BOOST_OS_HAIKU
...
#endif

#if BOOST_COMP_GNUC
#elif BOOST_COMP_MSVC
#elif BOOST_COMP_CLANG
...
#endif

Demo on Godbolt

Upvotes: 0

gavenkoa
gavenkoa

Reputation: 48903

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

Upvotes: 3

Puppy
Puppy

Reputation: 147028

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.

Upvotes: 1

AProgrammer
AProgrammer

Reputation: 52324

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.

Upvotes: 0

Elalfer
Elalfer

Reputation: 5348

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

Upvotes: 6

Related Questions