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

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

问题描述

我在 gcc v3.4.4 中遇到了一个错误,在我的代码中放置一个 #ifdef 以解决该错误,仅适用于该版本的编译器.

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.

GCC编译器预处理器预定义的宏来检测编译器的版本号有哪些?

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

推荐答案

来自gnu cpp手册...

From the gnu cpp manual...

__GNUC__
__GNUC_MINOR__
__GNUC_PATCHLEVEL__

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

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__ 是 GCC 3.0 的新内容;它也出现在 3.0 之前的广泛使用的开发快照中(根据您拥有的快照,它们将自己标识为 GCC 2.96 或 2.97).

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

如果您只需要知道您的程序是由 GCC 编译,还是由声称接受 GNU C 方言的非 GCC 编译器编译,您可以简单地测试 __GNUC__.如果您需要编写依赖于特定版本的代码,则必须更加小心.每次增加次要版本,补丁级别重置为零;每次增加主要版本(这种情况很少发生)时,都会重置次要版本和补丁级别.如果你想直接在条件语句中使用预定义的宏,你需要这样写:

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天全站免登陆