How to identify platform/compiler from preprocessor macros?

117,187

Solution 1

For Mac OS:

#ifdef __APPLE__

For MingW on Windows:

#ifdef __MINGW32__

For Linux:

#ifdef __linux__

For other Windows compilers, check this thread and this for several other compilers and architectures.

Solution 2

See: http://predef.sourceforge.net/index.php

This project provides a reasonably comprehensive listing of pre-defined #defines for many operating systems, compilers, language and platform standards, and standard libraries.

Solution 3

Here's what I use:

#ifdef _WIN32 // note the underscore: without it, it's not msdn official!
    // Windows (x64 and x86)
#elif __unix__ // all unices, not all compilers
    // Unix
#elif __linux__
    // linux
#elif __APPLE__
    // Mac OS, not sure if this is covered by __posix__ and/or __unix__ though...
#endif

EDIT: Although the above might work for the basics, remember to verify what macro you want to check for by looking at the Boost.Predef reference pages. Or just use Boost.Predef directly.

Solution 4

If you're writing C++, I can't recommend using the Boost libraries strongly enough.

The latest version (1.55) includes a new Predef library which covers exactly what you're looking for, along with dozens of other platform and architecture recognition macros.

#include <boost/predef.h>

// ...

#if BOOST_OS_WINDOWS

#elif BOOST_OS_LINUX

#elif BOOST_OS_MACOS

#endif
Share:
117,187
Arenim
Author by

Arenim

Updated on June 20, 2020

Comments

  • Arenim
    Arenim almost 4 years

    I'm writing a cross-platform code, which should compile at linux, windows, Mac OS. On windows, I must support visual studio and mingw.

    There are some pieces of platform-specific code, which I should place in #ifdef .. #endif environment. For example, here I placed win32 specific code:

    #ifdef WIN32
    #include <windows.h>
    #endif
    

    But how do I recognize linux and mac OS? What are defines names (or etc.) I should use?

  • gman
    gman almost 13 years
    Does __APPLE__ distinguish between OSX and iOS?
  • Achin Riley
    Achin Riley over 12 years
    __APPLE__ is set for both OS X and iOS. You can #include <TargetConditionals.h> inside #ifdef __APPLE__, which then gives you a TARGET_OS_IPHONE #define.
  • Erbureth
    Erbureth about 12 years
    use __linux __ instead, linux is not defined when compiling with GCC with GNU extensions disabled (ie -std=c++0x)
  • rubenvb
    rubenvb about 12 years
    @Erbureth Fixed, but one should really use predef.sourceforge.net/index.php as in the highest rated answer.
  • 0xC0000022L
    0xC0000022L about 12 years
    @rubenvb: indeed. And it got too few votes still, I think :) ... I've turned to their site so many times.
  • rvalue
    rvalue over 10 years
    As of version 1.55, Predef is now included in Boost C++ Libraries.
  • Mick MacCallum
    Mick MacCallum almost 10 years
    I know the rules were different when you posted this, but I'm going to have to ask you to edit this post to include more relevant details. Now a days link only answers are strongly discouraged, and I'd like to offer you the chance to save this post before its removal.
  • hobb
    hobb almost 10 years
    As this is the only answer that actually answers the question as stated in the title, and the question is very generic and relevant I strongly advice against removing it. I wouldn't call it a "link only" answer either.
  • John Bartholomew
    John Bartholomew almost 10 years
    @0x7fffffff: What possible benefit to anyone would there be in duplicating the content of the other answers into this one? If you believe it's really important to have a single definitive answer, then perhaps you should create such an answer yourself (it shouldn't be hard: just glue together the existing answers in some sensible order). Personally I have better things to do with my time, but as a moderator clearly SO is more important to you than to me.
  • scones
    scones almost 10 years
    __MINGW64__ is also available when one uses mingw64
  • ideasman42
    ideasman42 over 9 years
    Since __MINGW64__ is referenced, think _MSC_VER for Windows/MSVC is worth a mention (which can also be used to check MSVC Version).
  • Trevor Boyd Smith
    Trevor Boyd Smith over 9 years
    Given that this is boost, this solution will work on different platforms/OSs AND different compilers.
  • rubenvb
    rubenvb about 9 years
    I'm sorry, but this answer is quite incorrect on all accounts and doesn't even answer the question.
  • jww
    jww over 8 years
    "I can't recommend using the Boost libraries strongly enough...." - I've evaluated Boost three times. It can't pass an evaluation... Most of the bugs report were lucky if they were acknowledged. Lack of acknowledgement points to deeper problems in the engineering process. I believe preprocessor macros and built in C++ Standard Libraries are a safer choice.
  • rubenvb
    rubenvb over 8 years
    __MINGW64__ is not endorsed by the MinGW-w64 authors, instead you should check for __MINGW32__, #include <_mingw.h> if it's defined, and check for __MINGW64_VERSION_MAJOR.
  • leonbloy
    leonbloy about 8 years
    For consistency (perhaps a little pedantic) : the first #if ask if defined, the others test for value. If would be more consistent to do #elif defined(__unix__) , etc, I think.