编译器版本号的 gcc 预定义宏是什么?
我遇到了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
来自 gnu cpp 手册...
这些宏由所有使用 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__
。如果您需要编写依赖于特定版本的代码,则必须更加小心。每次增加次要版本时,补丁级别都会重置为零;每次增加主要版本(这种情况很少发生)时,次要版本和补丁级别都会重置。如果您希望直接在条件中使用预定义的宏,则需要这样编写:From the gnu cpp manual...
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:__GNUC__
、__GNUC_MINOR__
和__GNUC_PATCHLEVEL__
。例如,GCC 4.0.1 就可以:
当您想知道 GNU GCC 编译器在您当前的编程环境下定义了哪些预定义的预处理器指令时,请记住以下一个小命令行:
__GNUC__
,__GNUC_MINOR__
and__GNUC_PATCHLEVEL__
.For instance, GCC 4.0.1 will do:
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 版本有 3 个宏可供您测试。
例如,我的 gcc v 4.3.1 将它们定义为这样:
您可以通过运行来查看定义的“buitin”宏
There's 3 macros for the gcc version you can test on.
e.g. my gcc v 4.3.1 defines them as such:
You can see the "buitin" macros defined by running
来自在线文档:
和
From the online docs:
and
附带说明一下,如果您使用 glib,那么您可以访问以下可用于检查 GNU C/C++ 编译器版本的宏:
注意该宏如何始终返回 false (0)当您不使用 GNU 编译器进行编译时。
因此,我在 snapdev 库版本中使用以下宏.h 标头。就我而言,我还想检查补丁并且使用了许多库,所以我编写了一个更通用的版本:
并且我对 GCC 进行了专门检查,因此您不必记住宏的名称(包括版本):
正如我对大部分代码所做的那样,我编写了一个单元测试,因此我可以说,除非您向其提供无效数据(著名的最后一句话),否则测试不太可能失败。
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:
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:
and I have a specialized check for GCC so you don't have to remember the name of the macros including the version:
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).