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_SIMDfor 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