DEV Community

ib-dev-cpp
ib-dev-cpp

Posted on

4 4

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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

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/

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay