How can I detect g++ and MinGW in C++ preprocessor?

32,986

Solution 1

You can make use of:

#ifdef __GNUC__
#ifdef __MINGW32__

For additional macro's you might be interested in this page which shows other compiler macros

Solution 2

For GCC:

#ifdef __GNUC__

For MinGW:

#ifdef __MINGW32__

x86_64-w64-mingw32-gcc defines both __MINGW32__ and __MINGW64__.

Share:
32,986
EddieV223
Author by

EddieV223

Updated on July 09, 2022

Comments

  • EddieV223
    EddieV223 almost 2 years

    I want to do something like:

    #ifdef GCC
    #define GetFunctionName() string("My function name is ") + __PRETTY_FUNCTION__;
    #endif
    

    Since I want to use pretty PRETTY_FUNCTION this is only supported by gnu as far as I know so I need to detect if I am compiling for g++ and MinGW, how can I do that? I'm guessing all I need to know are the compiler's preprocessor definitions, like I did for Microsoft below.

    #ifdef WIN32
    #define LogFuncBegin() gLogger.FuncBegin( __FUNCTION__ );
    #define LogFuncEndSuccess() gLogger.FuncEndSuccess( __FUNCTION__ );
    #endif
    

    How can I detect g++ and MinGW in C++ preprocessor?

  • jww
    jww almost 9 years
    Did your test cases include MinGW-64?
  • jww
    jww almost 9 years
    Did your test cases include MinGW-64?
  • Floris Velleman
    Floris Velleman almost 9 years
    @jww No but MinGW-64 will also define the 32 macro, so this would work for that as well.
  • sedavidw
    sedavidw over 8 years
    This answer was written pre-MinGW-64. But I think that defines the __MINGW32__ macro as well. So should still work
  • jww
    jww over 8 years
    This is kind of a moot point. I can't find a MinGW-64 offered by the project.... There's no sense in solving a problem that does not exist....
  • rakslice
    rakslice over 6 years
    A more up-to-date version of that compiler macros list is in the Pre-defined Compiler Macros project at sourceforge
  • Alex Huszagh
    Alex Huszagh over 6 years
    @jww MinGW-w64 is a hard fork of MinGW, because MinGW didn't do a great job. Pretty much everyone uses MinGW-w64 these days, not the original MinGW. Same with MSYS2 and MSYS.
  • Aaron Franke
    Aaron Franke almost 2 years
    The link to "this page" is dead.
  • osjerick
    osjerick almost 2 years
    Or __GNUG__ which is equivalent to (__GNUC__ && __cplusplus) if you want to distinguish C++ from C.