什么是编译器版本号的gcc预定义宏? [英] What are the gcc predefined macros for the compiler's version number?

查看:190
本文介绍了什么是编译器版本号的gcc预定义宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个gcc v3.4.4的bug, p> GCC编译器预处理器预定义的宏是用来检测编译器的版本号的?

解决方案

来自gnu cpp手册。 ..

$ hr

$ p $ __ GNUC__
__GNUC_MINOR__
__GNUC_PATCHLEVEL__

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

__ GNUC_PATCHLEVEL __ 对于GCC 3.0是新的;它也出现在广泛使用的3.0版本的开发快照中(根据你拥有哪个快照,它们自称为GCC 2.96或2.97)。

你需要知道你的程序是否由GCC编译,或者是一个声称接受GNU C方言的非GCC编译器,你可以简单地测试 __ GNUC __ 。如果您需要编写取决于特定版本的代码,则必须更加小心。每次次要版本增加时,修补程序级别都会重置为零;每次主版本增加时(很少发生),次版本和补丁级别都会重置。如果您希望直接在条件中使用预定义的宏,您需要像这样编写它:

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


I have run into a bug with gcc v3.4.4 and which to put an #ifdef in my code to work around the bug for only that version of the compiler.

What are the GCC compiler preprocessor predefined macros to detect the version number of the compiler?

解决方案

From the gnu cpp manual...


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

这篇关于什么是编译器版本号的gcc预定义宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆