DEV Community

ib-dev-cpp
ib-dev-cpp

Posted on

Platform detection in C&C++

sometime when writing c or c++ code you need to write a code specific to the compiler/OS. and to this job the compiler have some predefined macros to detect the compiler or the OS.

Checking for OS:

to check which platform the code is compiled use these predefined macros:

for Windows              :  _WIN32
for Windows 64 bit       :  _WIN64 ( _WIN32 included )
for Mac OS & IOS         :  __APPLE__
for Linux & Linux-derived:  __linux__
for Android              :  __ANDROID__ ( __linux__ included )
for Akaros               :  __ros__
for NaCL                 :  __native_client__
for AsmJS                :  __asmjs__
for Fuschia              :  __Fuchsia__
Enter fullscreen mode Exit fullscreen mode

Checking for Compiler:

to check wich compiler used:

Visual Studio       _MSC_VER
gcc                 __GNUC__
clang               __clang__
emscripten          __EMSCRIPTEN__ (for asm.js and webassembly)
MinGW 32            __MINGW32__
MinGW-w64 32bit     __MINGW32__
MinGW-w64 64bit     __MINGW64__
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
pgradot profile image
Pierre Gradot

There is an awesome page with a lot a defines from OS, compilers, etc: sourceforge.net/p/predef/wiki/Home/