编译器版本号的 gcc 预定义宏是什么?

发布于 2024-08-15 16:39:59 字数 96 浏览 15 评论 0原文

我遇到了 gcc v3.4.4 的错误,需要在代码中添加 #ifdef 来解决该版本的编译器的错误。

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

[浮城] 2024-08-22 16:39:59

来自 gnu cpp 手册...


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

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)))
自由范儿 2024-08-22 16:39:59

__GNUC____GNUC_MINOR____GNUC_PATCHLEVEL__

例如,GCC 4.0.1 就可以:

#define __GNUC__ 4
#define __GNUC_MINOR__ 0
#define __GNUC_PATCHLEVEL__ 1

当您想知道 GNU GCC 编译器在您当前的编程环境下定义了哪些预定义的预处理器指令时,请记住以下一个小命令行:

gcc -E -dM -

/dev/null |更少

__GNUC__, __GNUC_MINOR__ and __GNUC_PATCHLEVEL__.

For instance, GCC 4.0.1 will do:

#define __GNUC__ 4
#define __GNUC_MINOR__ 0
#define __GNUC_PATCHLEVEL__ 1

Here is a little command line that is nice to remember when you are wondering which are the predefined preprocessor directives defined by the GNU GCC compiler under your current programming environment:

gcc -E -dM - < /dev/null |less

辞旧 2024-08-22 16:39:59

gcc 版本有 3 个宏可供您测试。

__GNUC_MINOR__ 
 __GNUC_PATCHLEVEL__ 
 __GNUC__ 

例如,我的 gcc v 4.3.1 将它们定义为这样:

#define __GNUC_MINOR__ 1
#define __GNUC_PATCHLEVEL__ 3
#define __GNUC__ 4

您可以通过运行来查看定义的“buitin”宏

gcc -E -dM -xc /dev/null

There's 3 macros for the gcc version you can test on.

__GNUC_MINOR__ 
 __GNUC_PATCHLEVEL__ 
 __GNUC__ 

e.g. my gcc v 4.3.1 defines them as such:

#define __GNUC_MINOR__ 1
#define __GNUC_PATCHLEVEL__ 3
#define __GNUC__ 4

You can see the "buitin" macros defined by running

gcc -E -dM -x c /dev/null

夜巴黎 2024-08-22 16:39:59

来自在线文档

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

__版本__
该宏扩展为一个字符串常量,描述正在使用的编译器的版本。您不应依赖其具有任何特定形式的内容,但可以认为它至少包含版本号。

From the online docs:

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

and

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

你爱我像她 2024-08-22 16:39:59

附带说明一下,如果您使用 glib,那么您可以访问以下可用于检查 GNU C/C++ 编译器版本的宏:

#ifdef __GNUC__
#define G_GNUC_CHECK_VERSION(major, minor) \
    ((__GNUC__ > (major)) || \
     ((__GNUC__ == (major)) && \
      (__GNUC_MINOR__ >= (minor))))
#else
#define G_GNUC_CHECK_VERSION(major, minor) 0
#endif

注意该宏如何始终返回 false (0)当您不使用 GNU 编译器进行编译时。


因此,我在 snapdev 库版本中使用以下宏.h 标头。就我而言,我还想检查补丁并且使用了许多库,所以我编写了一个更通用的版本:

#define     SNAPDEV_CHECK_VERSION(wanted_major, wanted_minor, wanted_patch, current_major, current_minor, current_patch) \
                (((current_major) > (wanted_major)) || \
                    (((current_major) == (wanted_major)) \
                        && (((current_minor) > (wanted_minor)) || \
                            (((current_minor) == (wanted_minor)) \
                                && ((current_patch) >= (wanted_patch))))))

并且我对 GCC 进行了专门检查,因此您不必记住宏的名称(包括版本):

#define     SNAPDEV_CHECK_GCC_VERSION(wanted_major, wanted_minor, wanted_patch) \
                SNAPDEV_CHECK_VERSION(wanted_major, wanted_minor, wanted_patch, \
                                      __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)

正如我对大部分代码所做的那样,我编写了一个单元测试,因此我可以说,除非您向其提供无效数据(著名的最后一句话),否则测试不太可能失败。

As a side note, if you are using glib, then you have access to the following macro that can be used to check the GNU C/C++ compiler version:

#ifdef __GNUC__
#define G_GNUC_CHECK_VERSION(major, minor) \
    ((__GNUC__ > (major)) || \
     ((__GNUC__ == (major)) && \
      (__GNUC_MINOR__ >= (minor))))
#else
#define G_GNUC_CHECK_VERSION(major, minor) 0
#endif

Notice how the macro always returns false (0) when you are not compiling with the GNU compiler.


As a result, I use the following macro in my snapdev library version.h header. In my case, I wanted to also check the patch and I use many libraries, so I wrote a more generic version:

#define     SNAPDEV_CHECK_VERSION(wanted_major, wanted_minor, wanted_patch, current_major, current_minor, current_patch) \
                (((current_major) > (wanted_major)) || \
                    (((current_major) == (wanted_major)) \
                        && (((current_minor) > (wanted_minor)) || \
                            (((current_minor) == (wanted_minor)) \
                                && ((current_patch) >= (wanted_patch))))))

and I have a specialized check for GCC so you don't have to remember the name of the macros including the version:

#define     SNAPDEV_CHECK_GCC_VERSION(wanted_major, wanted_minor, wanted_patch) \
                SNAPDEV_CHECK_VERSION(wanted_major, wanted_minor, wanted_patch, \
                                      __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)

and as I do with much of my code, I wrote a unit test so I can say that it is unlikely that the test will fail unless you feed it invalid data (famous last words).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文