如何在 C++ 预处理器中检测 g++ 和 MinGW?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17493759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:14:22  来源:igfitidea点击:

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

c++g++mingwc-preprocessorgnu

提问by EddieV223

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_FUNCTIONthis 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.

由于我想使用漂亮的PRETTY_FUNCTION,据我所知,这只受 gnu 支持,所以我需要检测我是否正在为 g++ 和 MinGW 进行编译,我该怎么做?我猜我需要知道的只是编译器的预处理器定义,就像我在下面为 Microsoft 所做的那样。

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

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

如何在 C++ 预处理器中检测 g++ 和 MinGW?

回答by Floris Velleman

You can make use of:

您可以使用:

#ifdef __GNUC__
#ifdef __MINGW32__

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

对于其他宏,您可能对此页面感兴趣,该页面显示了其他编译​​器宏

回答by sedavidw

For GCC:

对于海湾合作委员会:

#ifdef __GNUC__

For MinGW:

对于 MinGW:

#ifdef __MINGW32__

x86_64-w64-mingw32-gcc defines both __MINGW32__and __MINGW64__.

x86_64-w64-mingw32-gcc 定义了__MINGW32____MINGW64__