预定义__GNUC__宏

一、 介绍预定义宏“__GNUC__”

一.1 __GNUC__ 是gcc编译器编译代码时预定义的一个宏。

需要针对gcc编写代码时, 可以使用该宏进行条件编译。

一.2 __GNUC__ 的值表示gcc的版本。

需要针对gcc特定版本编写代码时,也可以使用该宏进行条件编译。

一.3 __GNUC__ 的类型是“int”

该宏被扩展后, 得到的是整数字面值。
可以通过仅预处理,查看宏扩展后的文本。见:《查看源文件预处理结果》
同时下面的示例也能体现出这一点。


二、 测试预定义宏__GNUC__

示例:
#include <assert.h>
#include <stdio.h>
#include <typeinfo>

#ifndef __GNUC__
#error sample for gcc compiler
#else
/* use gcc special extension: #warning , __attribute__, etc. */
#endif

int main() {
printf("hello gcc %d\n",__GNUC__);
assert( typeid(__GNUC__)==typeid(int) );
printf("press Enter to exit\n");
(void)getchar();
}



修改:

——2009/04/18

__GNUC__
__GNUC_MINOR__
__GNUC_PATCHLEVEL__
These macros are defined by all GNU compilers that use the C preprocessor:
C, C++, and Objective-C. 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. They are defined only when the entire compiler is in use; if you invoke the
preprocessor directly, they are not defined.

—— http://gcc.gnu.org/onlinedocs/gcc-3.1.1/cpp/Common-Predefined-Macros.html#Common%20Predefined%20Macros

相关链接:

——源代码
http://immature.googlecode.com/svn/trunk/iMmature/sample/predefined_macro/extension/__GNUC__/

——《查看源文件预处理结果》
http://www.cppblog.com/ownwaterloo/archive/2009/04/16/get_result_of_preprocessing.html

——《预定义_MSC_VER宏》
http://www.cppblog.com/ownwaterloo/archive/2009/04/15/predefined_macro__MSC_VER.html



Creative Commons License
本作品采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可。

转载请注明 :
文章作者 - OwnWaterloo
发表时间 - 2009年04月16日
原文链接 - http://www.cppblog.com/ownwaterloo/archive/2009/04/16/predefined_macro___GNUC__.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值