50

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
  • 90,984
  • 81
  • 374
  • 818
EddieV223
  • 4,785
  • 10
  • 34
  • 37

2 Answers2

63

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

Floris Velleman
  • 4,748
  • 3
  • 28
  • 45
42

For GCC:

#ifdef __GNUC__

For MinGW:

#ifdef __MINGW32__

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

kay
  • 24,516
  • 10
  • 94
  • 138
sedavidw
  • 10,038
  • 12
  • 53
  • 88
  • 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. – Alexander Huszagh Jan 28 '18 at 21:29