使用宏检测gcc而不是msvc / clang [英] Detect gcc as opposed to msvc / clang with macro
问题描述
我正在研究一个已经用gcc和msvc构建的项目。我们最近也开始使用clang进行构建。
代码中有一些部分,其中包含特定于平台的事情:
#ifndef _WIN32
//在msvc中忽略
#endif
由于gcc以前是唯一的非Windows版本,这相当于说只为gcc做这个。但现在它的意思是这样做只适用于gcc和clang。
然而,仍然存在一些情况,我想专门为gcc处理某些事情, 。是否有一种简单而强大的方法来检测gcc,即:
#ifdef ???
//这样做*仅*用于gcc
#endif
__ GNUC__
__GNUC_MINOR__
__GNUC_PATCHLEVEL__
这些宏由所有使用C预处理器的GNU编译器定义:C,C ++,Objective-C和Fortran。它们的值是编译器的主版本,次版本和补丁级别,作为整数常量。例如,GCC 3.2.1将把__GNUC__定义为3,__GNUC_MINOR__定义为2,__GNUC_PATCHLEVEL__定义为1.如果直接调用预处理器,也会定义这些宏。
另外:
__ GNUG__
GNU C ++编译器对此进行了定义。测试它相当于测试(__GNUC__&& __cplusplus)。
显然,
clang
也使用它们。不过它也定义了:$ pre code $ __ clang__
__clang_major__
__clang_minor__
__clang_patchlevel__
您可以这样做:
#ifdef __GNUC__
#ifndef __clang__
...
或甚至更好(注意订单):
#if defined(__ clang__)
....
#elif defined(__ GNUC__)||定义(__ GNUG__)
....
#elif defined(_MSC_VER)
....
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.
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".
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.
#ifdef ???
// do this *only* for gcc
#endif
__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.
Also:
__GNUG__
The GNU C++ compiler defines this. Testing it is equivalent to testing (__GNUC__ && __cplusplus).
Apparently, clang
uses them too. However it also defines:
__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)
....
这篇关于使用宏检测gcc而不是msvc / clang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!