Common Predefined Macros

本文详细介绍了GNU编译器集合(GCC)中常用的预定义宏,包括__GNUC__、__GNUC_MINOR__、__GNUC_PATCHLEVEL__等,这些宏用于描述编译器版本信息。文章还解释了如何使用这些宏来检测特定版本的GCC,并提供了实用的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The common predefined macros are GNU C extensions. They are available with the same meanings regardless of the machine or operating system on which you are using GNU C or GNU Fortran. Their names all start with double underscores.

__COUNTER__
This macro expands to sequential integral values starting from 0. In conjunction with the ## operator, this provides a convenient means to generate unique identifiers. Care must be taken to ensure that __COUNTER__ is not expanded prior to inclusion of precompiled headers which use it. Otherwise, the precompiled headers will not be used. 
__GFORTRAN__
The GNU Fortran compiler defines this. 
__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 version x.y.z defines __GNUC__ to x__GNUC_MINOR__ to y, and __GNUC_PATCHLEVEL__ to z. These macros are also defined if you invoke the preprocessor directly.

If all you need to know is whether or not your program is being compiled by GCC, or a non-GCC compiler that claims to accept the GNU C dialects, you can simply test __GNUC__. If you need to write code which depends on a specific version, you must be more careful. Each time the minor version is increased, the patch level is reset to zero; each time the major version is increased, the minor version and patch level are reset. If you wish to use the predefined macros directly in the conditional, you will need to write it like this:

          /* Test for GCC > 3.2.0 */
          #if __GNUC__ > 3 || \
              (__GNUC__ == 3 && (__GNUC_MINOR__ > 2 || \
                                 (__GNUC_MINOR__ == 2 && \
                                  __GNUC_PATCHLEVEL__ > 0))

Another approach is to use the predefined macros to calculate a single number, then compare that against a threshold:

          #define GCC_VERSION (__GNUC__ * 10000 \
                               + __GNUC_MINOR__ * 100 \
                               + __GNUC_PATCHLEVEL__)
          ...
          /* Test for GCC > 3.2.0 */
          #if GCC_VERSION > 30200

Many people find this form easier to understand. 

__GNUG__
The GNU C++ compiler defines this. Testing it is equivalent to testing (__GNUC__ && __cplusplus)
__STRICT_ANSI__
GCC defines this macro if and only if the -ansi switch, or a -std switch specifying strict conformance to some version of ISO C or ISO C++, was specified when GCC was invoked. It is defined to ‘1’. This macro exists primarily to direct GNU libc's header files to use only definitions found in standard C. 
__BASE_FILE__
This macro expands to the name of the main input file, in the form of a C string constant. This is the source file that was specified on the command line of the preprocessor or C compiler. 
__INCLUDE_LEVEL__
This macro expands to a decimal integer constant that represents the depth of nesting in include files. The value of this macro is incremented on every ‘#include’ directive and decremented at the end of every included file. It starts out at 0, its value within the base file specified on the command line. 
__ELF__
This macro is defined if the target uses the ELF object format. 
__VERSION__
This macro expands to a string constant which describes the version of the compiler in use. You should not rely on its contents having any particular form, but it can be counted on to contain at least the release number. 
__OPTIMIZE____OPTIMIZE_SIZE____NO_INLINE__
These macros describe the compilation mode. __OPTIMIZE__ is defined in all optimizing compilations. __OPTIMIZE_SIZE__ is defined if the compiler is optimizing for size, not speed. __NO_INLINE__ is defined if no functions will be inlined into their callers (when not optimizing, or when inlining has been specifically disabled by -fno-inline).

These macros cause certain GNU header files to provide optimized definitions, using macros or inline functions, of system library functions. You should not use these macros in any way unless you make sure that programs will execute with the same effect whether or not they are defined. If they are defined, their value is 1. 

__GNUC_GNU_INLINE__
GCC defines this macro if functions declared inline will be handled in GCC's traditional gnu90 mode. Object files will contain externally visible definitions of all functions declared inline without extern or static. They will not contain any definitions of any functions declared extern inline
__GNUC_STDC_INLINE__
GCC defines this macro if functions declared inline will be handled according to the ISO C99 or later standards. Object files will contain externally visible definitions of all functions declared extern inline. They will not contain definitions of any functions declared inline without extern.

If this macro is defined, GCC supports the gnu_inline function attribute as a way to always get the gnu90 behavior. 

__CHAR_UNSIGNED__
GCC defines this macro if and only if the data type char is unsigned on the target machine. It exists to cause the standard header file limits.h to work correctly. You should not use this macro yourself; instead, refer to the standard macros defined in limits.h
__WCHAR_UNSIGNED__
Like __CHAR_UNSIGNED__, this macro is defined if and only if the data type wchar_t is unsigned and the front-end is in C++ mode. 
__REGISTER_PREFIX__
This macro expands to a single token (not a string constant) which is the prefix applied to CPU register names in assembly language for this target. You can use it to write assembly that is usable in multiple environments. For example, in the m68k-aout environment it expands to nothing, but in the m68k-coff environment it expands to a single ‘%’. 
__USER_LABEL_PREFIX__
This macro expands to a single token which is the prefix applied to user labels (symbols visible to C code) in assembly. For example, in the m68k-aout environment it expands to an ‘_’, but in the m68k-coff environment it expands to nothing.

This macro will have the correct definition even if -f(no-)underscores is in use, but it will not be correct if target-specific options that adjust this prefix are used (e.g. the OSF/rose -mno-underscores option). 

__SIZE_TYPE____PTRDIFF_TYPE____WCHAR_TYPE____WINT_TYPE____INTMAX_TYPE____UINTMAX_TYPE____SIG_ATOMIC_TYPE____INT8_TYPE____INT16_TYPE____INT32_TYPE____INT64_TYPE____UINT8_TYPE____UINT16_TYPE____UINT32_TYPE____UINT64_TYPE____INT_LEAST8_TYPE____INT_LEAST16_TYPE____INT_LEAST32_TYPE____INT_LEAST64_TYPE____UINT_LEAST8_TYPE____UINT_LEAST16_TYPE____UINT_LEAST32_TYPE____UINT_LEAST64_TYPE____INT_FAST8_TYPE____INT_FAST16_TYPE____INT_FAST32_TYPE____INT_FAST64_TYPE____UINT_FAST8_TYPE____UINT_FAST16_TYPE____UINT_FAST32_TYPE____UINT_FAST64_TYPE____INTPTR_TYPE____UINTPTR_TYPE__
These macros are defined to the correct underlying types for the size_tptrdiff_twchar_twint_tintmax_tuintmax_tsig_atomic_tint8_tint16_tint32_tint64_tuint8_tuint16_tuint32_tuint64_tint_least8_tint_least16_t,int_least32_tint_least64_tuint_least8_tuint_least16_tuint_least32_tuint_least64_tint_fast8_tint_fast16_tint_fast32_tint_fast64_tuint_fast8_tuint_fast16_tuint_fast32_tuint_fast64_tintptr_t, and uintptr_t typedefs, respectively. They exist to make the standard header files stddef.hstdint.h, and wchar.h work correctly. You should not use these macros directly; instead, include the appropriate headers and use the typedefs. Some of these macros may not be defined on particular systems if GCC does not provide a stdint.h header on those systems. 
__CHAR_BIT__
Defined to the number of bits used in the representation of the char data type. It exists to make the standard header given numerical limits work correctly. You should not use this macro directly; instead, include the appropriate headers. 
__SCHAR_MAX____WCHAR_MAX____SHRT_MAX____INT_MAX____LONG_MAX____LONG_LONG_MAX____WINT_MAX____SIZE_MAX____PTRDIFF_MAX____INTMAX_MAX____UINTMAX_MAX____SIG_ATOMIC_MAX____INT8_MAX____INT16_MAX____INT32_MAX____INT64_MAX____UINT8_MAX____UINT16_MAX____UINT32_MAX____UINT64_MAX____INT_LEAST8_MAX____INT_LEAST16_MAX____INT_LEAST32_MAX____INT_LEAST64_MAX____UINT_LEAST8_MAX____UINT_LEAST16_MAX____UINT_LEAST32_MAX____UINT_LEAST64_MAX____INT_FAST8_MAX____INT_FAST16_MAX____INT_FAST32_MAX____INT_FAST64_MAX____UINT_FAST8_MAX____UINT_FAST16_MAX____UINT_FAST32_MAX____UINT_FAST64_MAX____INTPTR_MAX____UINTPTR_MAX____WCHAR_MIN____WINT_MIN____SIG_ATOMIC_MIN__
Defined to the maximum value of the signed charwchar_tsigned shortsigned intsigned longsigned long longwint_tsize_tptrdiff_tintmax_tuintmax_tsig_atomic_tint8_tint16_tint32_tint64_tuint8_tuint16_tuint32_tuint64_t,int_least8_tint_least16_tint_least32_tint_least64_tuint_least8_tuint_least16_tuint_least32_tuint_least64_tint_fast8_tint_fast16_tint_fast32_tint_fast64_tuint_fast8_tuint_fast16_tuint_fast32_tuint_fast64_tintptr_t, anduintptr_t types and to the minimum value of the wchar_twint_t, and sig_atomic_t types respectively. They exist to make the standard header given numerical limits work correctly. You should not use these macros directly; instead, include the appropriate headers. Some of these macros may not be defined on particular systems if GCC does not provide a stdint.h header on those systems. 
__INT8_C__INT16_C__INT32_C__INT64_C__UINT8_C__UINT16_C__UINT32_C__UINT64_C__INTMAX_C__UINTMAX_C
Defined to implementations of the standard stdint.h macros with the same names without the leading __. They exist the make the implementation of that header work correctly. You should not use these macros directly; instead, include the appropriate headers. Some of these macros may not be defined on particular systems if GCC does not provide a stdint.h header on those systems. 
__SCHAR_WIDTH____SHRT_WIDTH____INT_WIDTH____LONG_WIDTH____LONG_LONG_WIDTH____PTRDIFF_WIDTH____SIG_ATOMIC_WIDTH____SIZE_WIDTH____WCHAR_WIDTH____WINT_WIDTH____INT_LEAST8_WIDTH____INT_LEAST16_WIDTH____INT_LEAST32_WIDTH____INT_LEAST64_WIDTH____INT_FAST8_WIDTH____INT_FAST16_WIDTH____INT_FAST32_WIDTH____INT_FAST64_WIDTH____INTPTR_WIDTH____INTMAX_WIDTH__
Defined to the bit widths of the corresponding types. They exist to make the implementations of limits.h and stdint.h behave correctly. You should not use these macros directly; instead, include the appropriate headers. Some of these macros may not be defined on particular systems if GCC does not provide a stdint.h header on those systems. 
__SIZEOF_INT____SIZEOF_LONG____SIZEOF_LONG_LONG____SIZEOF_SHORT____SIZEOF_POINTER____SIZEOF_FLOAT____SIZEOF_DOUBLE____SIZEOF_LONG_DOUBLE____SIZEOF_SIZE_T____SIZEOF_WCHAR_T____SIZEOF_WINT_T____SIZEOF_PTRDIFF_T__
Defined to the number of bytes of the C standard data types: intlonglong longshortvoid *floatdoublelong doublesize_twchar_twint_t and ptrdiff_t
__BYTE_ORDER____ORDER_LITTLE_ENDIAN____ORDER_BIG_ENDIAN____ORDER_PDP_ENDIAN__
__BYTE_ORDER__ is defined to one of the values __ORDER_LITTLE_ENDIAN____ORDER_BIG_ENDIAN__, or __ORDER_PDP_ENDIAN__ to reflect the layout of multi-byte and multi-word quantities in memory. If __BYTE_ORDER__ is equal to __ORDER_LITTLE_ENDIAN__or __ORDER_BIG_ENDIAN__, then multi-byte and multi-word quantities are laid out identically: the byte (word) at the lowest address is the least significant or most significant byte (word) of the quantity, respectively. If__BYTE_ORDER__ is equal to __ORDER_PDP_ENDIAN__, then bytes in 16-bit words are laid out in a little-endian fashion, whereas the 16-bit subwords of a 32-bit quantity are laid out in big-endian fashion.

You should use these macros for testing like this:

          /* Test for a little-endian machine */
          #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__

__FLOAT_WORD_ORDER__
__FLOAT_WORD_ORDER__ is defined to one of the values __ORDER_LITTLE_ENDIAN__ or __ORDER_BIG_ENDIAN__ to reflect the layout of the words of multi-word floating-point quantities. 
__DEPRECATED
This macro is defined, with value 1, when compiling a C++ source file with warnings about deprecated constructs enabled. These warnings are enabled by default, but can be disabled with -Wno-deprecated
__EXCEPTIONS
This macro is defined, with value 1, when compiling a C++ source file with exceptions enabled. If -fno-exceptions is used when compiling the file, then this macro is not defined. 
__GXX_RTTI
This macro is defined, with value 1, when compiling a C++ source file with runtime type identification enabled. If -fno-rtti is used when compiling the file, then this macro is not defined. 
__USING_SJLJ_EXCEPTIONS__
This macro is defined, with value 1, if the compiler uses the old mechanism based on setjmp and longjmp for exception handling. 
__GXX_EXPERIMENTAL_CXX0X__
This macro is defined when compiling a C++ source file with the option -std=c++0x or -std=gnu++0x. It indicates that some features likely to be included in C++0x are available. Note that these features are experimental, and may change or be removed in future versions of GCC. 
__GXX_WEAK__
This macro is defined when compiling a C++ source file. It has the value 1 if the compiler will use weak symbols, COMDAT sections, or other similar techniques to collapse symbols with “vague linkage” that are defined in multiple translation units. If the compiler will not collapse such symbols, this macro is defined with value 0. In general, user code should not need to make use of this macro; the purpose of this macro is to ease implementation of the C++ runtime library provided with G++. 
__NEXT_RUNTIME__
This macro is defined, with value 1, if (and only if) the NeXT runtime (as in -fnext-runtime) is in use for Objective-C. If the GNU runtime is used, this macro is not defined, so that you can use this macro to determine which runtime (NeXT or GNU) is being used. 
__LP64___LP64
These macros are defined, with value 1, if (and only if) the compilation is for a target where long int and pointer both use 64-bits and int uses 32-bit. 
__SSP__
This macro is defined, with value 1, when -fstack-protector is in use. 
__SSP_ALL__
This macro is defined, with value 2, when -fstack-protector-all is in use. 
__SSP_STRONG__
This macro is defined, with value 3, when -fstack-protector-strong is in use. 
__SSP_EXPLICIT__
This macro is defined, with value 4, when -fstack-protector-explicit is in use. 
__SANITIZE_ADDRESS__
This macro is defined, with value 1, when -fsanitize=address or -fsanitize=kernel-address are in use. 
__SANITIZE_THREAD__
This macro is defined, with value 1, when -fsanitize=thread is in use. 
__TIMESTAMP__
This macro expands to a string constant that describes the date and time of the last modification of the current source file. The string constant contains abbreviated day of the week, month, day of the month, time in hh:mm:ss form, year and looks like "Sun Sep 16 01:03:52 1973". If the day of the month is less than 10, it is padded with a space on the left.

If GCC cannot determine the current date, it will emit a warning message (once per compilation) and __TIMESTAMP__ will expand to "??? ??? ?? ??:??:?? ????"

__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8__GCC_HAVE_SYNC_COMPARE_AND_SWAP_16
These macros are defined when the target processor supports atomic compare and swap operations on operands 1, 2, 4, 8 or 16 bytes in length, respectively. 
__GCC_HAVE_DWARF2_CFI_ASM
This macro is defined when the compiler is emitting DWARF CFI directives to the assembler. When this is defined, it is possible to emit those same directives in inline assembly. 
__FP_FAST_FMA__FP_FAST_FMAF__FP_FAST_FMAL
These macros are defined with value 1 if the backend supports the fmafmaf, and fmal builtin functions, so that the include file math.h can define the macros FP_FAST_FMAFP_FAST_FMAF, and FP_FAST_FMAL for compatibility with the 1999 C standard. 
__GCC_IEC_559
This macro is defined to indicate the intended level of support for IEEE 754 (IEC 60559) floating-point arithmetic. It expands to a nonnegative integer value. If 0, it indicates that the combination of the compiler configuration and the command-line options is not intended to support IEEE 754 arithmetic for float and double as defined in C99 and C11 Annex F (for example, that the standard rounding modes and exceptions are not supported, or that optimizations are enabled that conflict with IEEE 754 semantics). If 1, it indicates that IEEE 754 arithmetic is intended to be supported; this does not mean that all relevant language features are supported by GCC. If 2 or more, it additionally indicates support for IEEE 754-2008 (in particular, that the binary encodings for quiet and signaling NaNs are as specified in IEEE 754-2008).

This macro does not indicate the default state of command-line options that control optimizations that C99 and C11 permit to be controlled by standard pragmas, where those standards do not require a particular default state. It does not indicate whether optimizations respect signaling NaN semantics (the macro for that is __SUPPORT_SNAN__). It does not indicate support for decimal floating point or the IEEE 754 binary16 and binary128 types. 

__GCC_IEC_559_COMPLEX
This macro is defined to indicate the intended level of support for IEEE 754 (IEC 60559) floating-point arithmetic for complex numbers, as defined in C99 and C11 Annex G. It expands to a nonnegative integer value. If 0, it indicates that the combination of the compiler configuration and the command-line options is not intended to support Annex G requirements (for example, because -fcx-limited-range was used). If 1 or more, it indicates that it is intended to support those requirements; this does not mean that all relevant language features are supported by GCC. 
__NO_MATH_ERRNO__

This macro is defined if -fno-math-errno is used, or enabled by another option such as -ffast-math or by default.



eliminate 淘汰,清除
subject to 服从...
optimizations 优化
aligned 对准的,均衡的
tutorial  个别辅导时间; 教程,辅导材料; 使用说明书; 辅导课;
decent 正派的; 得体的; (服装等) 相称的,合宜的; 相当好的
minor 较小的,少数的,未成年人,选修
invoke 祈求,提出...以证明或支持,借助
threshold 门槛,入口,开始; [物理学] 临界值; <英>级限协定; [航空学] 跑道入口;
阈值的,临界值的; <英>按物价指数变动工资的,工资极限的,级限的;
equivalent to 等同

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值