__GUNC__宏定义

本文介绍了GCC编译器中预定义的宏__GNUC__、__GNUC_MINOR__和__GNUC_PATCHLEVEL__,这些宏用于获取GCC的主版本号、次版本号和修正版本号。文章还提供了如何测试和使用这些宏的具体示例。
__GUNC__宏定义

__GNUC__  __GNUC_MINOR__ __GNUC_PATCHLEVEL__是gcc中的预定义宏
分别代表gcc的主版本号,次版本号,修正版本号.

引用:
https://gcc.gnu.org/onlinedocs/gcc-5.1.0/cpp/Common-Predefined-Macros.html#Common-Predefined-Macros

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

  __GNUC_PATCHLEVEL__ is new to GCC 3.0; it is also present in the widely-used development snapshots leading up to 3.0 (which identify themselves as GCC 2.96 or 2.97, depending on which snapshot you have).

  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 (which happens rarely), 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.
    
测试程序:
#include<stdio.h>
int main()
{
   #ifdef __GNUC__
     printf("__GUNC__ value is %d\n",__GNUC__);
   #endif
   #ifdef __GNUC_MINOR__
      printf("__GNUC_MINOR_ value is %d\n",__GNUC_MINOR__);
   #endif
   #ifdef __GNUC_PATCHLEVEL__
      printf("__GNUC_PATCHLEVEL_ value is %d\n",__GNUC_PATCHLEVEL__);
   #endif
}

编译 : gcc test_gunc_maro.c -g -o test_gunc_maro.exe
执行 : ./test_gunc_maro.exe
结果 :     __GUNC__ value is 4
                __GNUC_MINOR_ value is 4
                __GNUC_PATCHLEVEL_ value is 6
                
使用gcc -v查看我的gcc的版本执行的结果为: 4.4.6版本

 gcc -v :
    使用内建 specs。
    目标:x86_64-redhat-linux
    配置为:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
    线程模型:posix
    gcc 版本 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值