54

I want to do something like:

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

Since I want to use __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 the C++ preprocessor?

AJM
  • 1,317
  • 2
  • 15
  • 30
EddieV223
  • 5,085
  • 11
  • 36
  • 38

2 Answers2

66

You can make use of:

#ifdef __GNUC__
#ifdef __MINGW32__

For additional macros you might be interested in this page which shows other compiler macros.

AJM
  • 1,317
  • 2
  • 15
  • 30
Floris Velleman
  • 4,848
  • 4
  • 29
  • 46
  • Did your test cases include MinGW-64? – jww Jul 30 '15 at 22:15
  • @jww No but MinGW-64 will also define the 32 macro, so this would work for that as well. – Floris Velleman Aug 02 '15 at 14:07
  • 5
    A more up-to-date version of that compiler macros list is in the [Pre-defined Compiler Macros project at sourceforge](https://sourceforge.net/p/predef/wiki/Home/) – rakslice Oct 06 '17 at 00:49
  • 1
    The link to "this page" is dead. – Aaron Franke Jun 05 '22 at 20:05
  • Or `__GNUG__` which is equivalent to `(__GNUC__ && __cplusplus)` if you want to distinguish C++ from C. – Alex Che Jun 14 '22 at 22:18
  • Wayback Machine archived version of the linked page: https://web.archive.org/web/20160308010351/https://beefchunk.com/documentation/lang/c/pre-defined-c/precomp.html - though I concur with @rakslice that a more up-to-date list like https://github.com/cpredef/predef is preferable. – AJM Mar 06 '23 at 17:32
47

For GCC:

#ifdef __GNUC__

For MinGW:

#ifdef __MINGW32__

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

Kijewski
  • 25,517
  • 12
  • 101
  • 143
sedavidw
  • 11,116
  • 13
  • 61
  • 95
  • 2
    Did your test cases include MinGW-64? – jww Jul 30 '15 at 22:16
  • 2
    This answer was written pre-MinGW-64. But I think that defines the `__MINGW32__` macro as well. So should still work – sedavidw Sep 08 '15 at 14:50
  • This is kind of a moot point. I can't find a [MinGW-64 offered by the project](http://www.mingw.org/).... There's no sense in solving a problem that does not exist.... – jww Sep 09 '15 at 00:59
  • 10
    @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. – Alex Huszagh Jan 28 '18 at 21:29