gcc的-D和-U参数:宏的设置与取消

来源:互联网 发布:编程培训机构logo 编辑:程序博客网 时间:2024/05/15 18:12

/********************************************************************************************************************************

原文地址:http://www.linuxdiyf.com/viewarticle.php?id=21325

转载说明:这个是-D的升级版!!  这个-U只对-D起作用,对-imacros不行。

********************************************************************************************************************************/

这两天做LFS注意到了gcc的-D参数:在gcc命令中定义宏,比如我有如下的代码:

CODE:
/* hello.c */
#include <stdio.h>

#ifdef YES
char* str = "Yes, this is a macro.";
#else
char* str = "No, there is no macro.";
#endif

int main()
{
printf("%s\n", str);
return 0;
}

使用-D传入宏YES来进行编译:
CODE:
recordus@LFS test # gcc -DYES -o helloyes hello.c
recordus@LFS test # ./helloyes
Yes, this is a macro.

而不传入宏则是这样的:
CODE:
recordus@LFS test # gcc -o hellono hello.c
recordus@LFS test # ./hellono
No, there is no macro.

gcc还有与-D对应的另一个参数-U用于取消宏,比如:
CODE:
root@LFS test # gcc -DYES -UYES -o helloyesno hello.c
root@LFS test # ./helloyesno
No, there is no macro.

这大概是这两个参数最简单的应用了:)

原创粉丝点击