免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2457 | 回复: 9
打印 上一主题 下一主题

请问:#if *与 if(*)有什么区别呢? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-25 13:46 |只看该作者 |倒序浏览
RT,是不是一样?

论坛徽章:
1
双子座
日期:2015-01-04 14:25:06
2 [报告]
发表于 2008-07-25 13:51 |只看该作者
不一样
一个是预处理机制里用的
一个C语言用的

论坛徽章:
0
3 [报告]
发表于 2008-07-25 21:56 |只看该作者
刚刚看GNU Coding Standards
Richard Stallman,
last updated June 10, 2008

3.5 Conditional Compilation
When supporting configuration options already known when building your program we
prefer using if (... ) over conditional compilation, as in the former case the compiler is
able to perform more extensive checking of all possible code paths.
For example, please write
if (HAS_FOO)
...
else
...
instead of:
#ifdef HAS_FOO
...
#else
...
#endif
A modern compiler such as GCC will generate exactly the same code in both cases, and
we have been using similar techniques with good success in several projects. Of course, the
former method assumes that HAS_FOO is defined as either 0 or 1.
While this is not a silver bullet solving all portability problems, and is not always appropriate,
following this policy would have saved GCC developers many hours, or even days,
per year.
In the case of function-like macros like REVERSIBLE_CC_MODE in GCC which cannot be
simply used in if( ...) statements, there is an easy workaround. Simply introduce another
macro HAS_REVERSIBLE_CC_MODE as in the following example:
#ifdef REVERSIBLE_CC_MODE
#define HAS_REVERSIBLE_CC_MODE 1
#else
#define HAS_REVERSIBLE_CC_MODE 0
#endif

[ 本帖最后由 ychang0918 于 2008-7-25 21:57 编辑 ]

论坛徽章:
38
2017金鸡报晓
日期:2017-02-08 10:39:4215-16赛季CBA联赛之深圳
日期:2023-02-16 14:39:0220周年集字徽章-年
日期:2022-08-31 14:25:28黑曼巴
日期:2022-08-17 18:57:0919周年集字徽章-年
日期:2022-04-25 13:02:5920周年集字徽章-20	
日期:2022-03-29 11:10:4620周年集字徽章-年
日期:2022-03-14 22:35:1820周年集字徽章-周	
日期:2022-03-09 12:51:3220周年集字徽章-年
日期:2022-02-10 13:13:4420周年集字徽章-周	
日期:2022-02-03 12:09:4420周年集字徽章-20	
日期:2022-01-25 20:14:2720周年集字徽章-周	
日期:2022-01-13 15:12:33
4 [报告]
发表于 2008-07-25 21:58 |只看该作者
不一样

论坛徽章:
0
5 [报告]
发表于 2008-07-25 22:00 |只看该作者
#if是条件编译,在预处理阶段处理,预处理完成后根本就没有这样这样的代码了
下一阶段才是编译,处理if分支语句

论坛徽章:
0
6 [报告]
发表于 2008-07-25 22:10 |只看该作者
原帖由 ychang0918 于 2008-7-25 21:56 发表
刚刚看GNU Coding Standards
Richard Stallman,
last updated June 10, 2008

3.5 Conditional Compilation
When supporting configuration options already known when building your program we
prefer ...

找到个翻译
如果已经知道配置选项,那么在编译你的程序时,应该使用if(...)而不是条件编译,因为在前一种情况下,编译器能够对所有可能的路径进行广泛的检查.
比如: 要写
if(HAS_FOO)
...
else
...
而不是:
#ifdef HAS_FOO
...
#else
...
#endif

对于这两种情况,现代的编译器比如GCC将会生成完全相同的代码,并且我们已经在几个项目中成功地使用了类似的技术.当然,前一种方法假定HAS_FOO被定义为0或1.
尽管它并不象银弹(silver bullet)那样解决了所有的可移植性问题,并且并不是总是很恰当,但是遵照这种策裸可以每年节省GCC开发者许多小时,甚至许多天.
在处理GCC中的类似函数的宏比如REVERSIBLE_CC_MODE时,不能简单地使用if(>...)语句.这里有一种简单的方法. 再引入另外一个宏HAS_REVERSIBLE_CC_MODE宏即可. 例如:
#ifdef REVERSIBLE_CC_MODE
#define HAS_REVERSIBLE_CC_MODE 1
#else
#define HAS_REVERSIBLE_CC_MODE 0
#endif

论坛徽章:
0
7 [报告]
发表于 2008-07-25 23:48 |只看该作者
原帖由 xi2008wang 于 2008-7-25 22:10 发表

找到个翻译
如果已经知道配置选项,那么在编译你的程序时,应该使用if(...)而不是条件编译,因为在前一种情况下,编译器能够对所有可能的路径进行广泛的检查.
比如: 要写
if(HAS_FOO)
...
els ...

多谢,知道了~~~

论坛徽章:
0
8 [报告]
发表于 2008-07-27 17:40 |只看该作者
简单说来,你做主的时候,用 #if;否则,用 if。

论坛徽章:
0
9 [报告]
发表于 2008-07-27 17:41 |只看该作者
原帖由 yecheng_110 于 2008-7-25 13:51 发表
不一样
一个是预处理机制里用的
一个C语言用的


预处理器也是C语言编译系统的一部分。

论坛徽章:
0
10 [报告]
发表于 2008-07-27 18:43 |只看该作者

回复 #6 xi2008wang 的帖子

很详细
学习
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP