3

How can we know which operating system the code is running?

e.g. How to know the operating system like Unix-Linux, Solaris, HP Unix, Windows, Mac etc?

How can we determine operating system in C++ code with boost? I want to test with Boost v1.41 onwards.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Neel
  • 451
  • 1
  • 9
  • 23
  • One knows on which family of operating system code will run at compile time. Querying particular OS version could be done using platform-specific method. – user7860670 Sep 07 '17 at 07:38
  • I want to check at runtime. Some kind of class method which gives OS info. – Neel Sep 07 '17 at 07:39
  • You could use QSysInfo: https://stackoverflow.com/questions/3063110/get-the-current-operating-system-during-runtime-in-c – yacc Sep 07 '17 at 07:50
  • Boost offers only compile time recognition. – Michał Fita Sep 07 '17 at 08:00
  • 3
    If you don't know if you are compiling for Mac or Windows, you will compile it all wrong and the program will never start. So it *must* be known long before you try to run the program. – Bo Persson Sep 07 '17 at 08:43
  • @MichałFita because run time recognition is useless. It must be known at compile time. You can only check the OS version at run time. But the code must be targeted for that specific OS when compiling – phuclv Nov 30 '20 at 16:38

1 Answers1

4

Code must be compiled to the specific platform you're targeting so you must know the platform at compile time. For example Linux binaries can't be run on BSD, Solaris, AIX... so checking the OS at runtime makes no sense and is useless

That means the OS needs to be checked via preprocessor directives and not in a C statement. There's Boost.Predef which contains various predefined macros for determining the 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.

You can use BOOST_OS_* and BOOST_PLAT_* for your purpose. 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

The full list can be found in BOOST_OS operating system macros and BOOST_PLAT platform macros. BOOST_HW_ can also be used to detect the hardware platform

Demo on Godbolt

See also demo on BOOST_ARCH

phuclv
  • 37,963
  • 15
  • 156
  • 475