C++ 使用宏检测 gcc 而不是 msvc / clang

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28166565/
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-28 20:55:31  来源:igfitidea点击:

Detect gcc as opposed to msvc / clang with macro

c++gcc

提问by ValarDohaeris

I am working on a project that has been built with both gcc and msvc so far. We recently started building with clang as well.

到目前为止,我正在开发一个使用 gcc 和 msvc 构建的项目。我们最近也开始使用 clang 进行构建。

There are some parts in the code, where platform-specific things are done:

代码中有一些部分,其中完成了特定于平台的事情:

#ifndef _WIN32
// ignore this in msvc
#endif

Since gcc has previously been the only non-windows build, this was equivalent to saying "do this only for gcc". But now it means "do this only for gcc and clang".

由于 gcc 以前是唯一的非 Windows 构建,因此这相当于说“仅对 gcc 执行此操作”。但现在它的意思是“仅对 gcc 和 clang 执行此操作”。

However there are still situations, where I would like to handle something specifically for gcc, and not for clang. Is there a simple and robust way to detect gcc, i.e.

然而,仍然有一些情况,我想专门为 gcc 处理一些事情,而不是为 clang 处理。有没有一种简单而强大的方法来检测gcc,即

#ifdef ???
// do this *only* for gcc
#endif

回答by dmg

__GNUC__
__GNUC_MINOR__
__GNUC_PATCHLEVEL__

These macros are defined by all GNU compilers that use the C preprocessor: C, C++, Objective-C and Fortran. Their values are the major version, minor version, and patch level of the compiler, as integer constants. For example, GCC 3.2.1 will define __GNUC__ to 3, __GNUC_MINOR__ to 2, and __GNUC_PATCHLEVEL__ to 1. These macros are also defined if you invoke the preprocessor directly.

这些宏由所有使用 C 预处理器的 GNU 编译器定义:C、C++、Objective-C 和 Fortran。它们的值是编译器的主要版本、次要版本和补丁级别,作为整数常量。例如,GCC 3.2.1 会将 __GNUC__ 定义为 3,将 __GNUC_MINOR__ 定义为 2,将 __GNUC_PATCHLEVEL__ 定义为 1。如果您直接调用预处理器,也会定义这些宏。

Also:

还:

__GNUG__

The GNU C++ compiler defines this. Testing it is equivalent to testing (__GNUC__ && __cplusplus).

GNU C++ 编译器对此进行了定义。测试它等同于测试 (__GNUC__ && __cplusplus)。

Source

来源

Apparently, clanguses them too. However it also defines:

显然,也clang使用它们。但是它也定义了:

__clang__
__clang_major__
__clang_minor__
__clang_patchlevel__

So you can do:

所以你可以这样做:

#ifdef __GNUC__
    #ifndef __clang__
...

Or even better (note the order):

甚至更好(注意顺序):

#if defined(__clang__)
....
#elif defined(__GNUC__) || defined(__GNUG__)
....
#elif defined(_MSC_VER)
....

回答by ValarDohaeris

With Boost, this becomes very simple:

使用 Boost,这变得非常简单:

#include <boost/predef.h>

#if BOOST_COMP_GNUC
// do this *only* for gcc
#endif

See also the Using the predefssection of the boost documentation.

另请参阅boost 文档的Using the predefs部分。

(credit to rubenvb who mentioned this in a comment, to Alberto M for adding the include, and to Frederik Aalund for correcting #ifdefto #if)

(感谢 rubenvb 在评论中提到了这一点,感谢 Alberto M 添加了包含,感谢 Frederik Aalund 更正#ifdef#if

回答by Khaled Lakehal

I use this define:

我使用这个定义:

#define GCC_COMPILER (defined(__GNUC__) && !defined(__clang__))

And test with it:

并用它测试:

#if GCC_COMPILER
...
#endif

回答by Tony Delroy

__GNUG__may be your best bet - see here. That tests for GNU C++ specifically, and not just a GNU C/C++/FORTRAN compiler per __GNUC__- i.e. "equivalent to testing (__GNUC__ && __cplusplus)".

__GNUG__可能是您最好的选择 - 请参阅此处。它专门针对 GNU C++ 进行测试,而不仅仅是针对 GNU C/C++/FORTRAN 编译器进行测试__GNUC__——即“等同于测试(__GNUC__ && __cplusplus)