Chinaunix首页 | 论坛 | 博客
  • 博客访问: 274115
  • 博文数量: 61
  • 博客积分: 655
  • 博客等级: 上士
  • 技术积分: 489
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-21 18:21
文章分类

全部博文(61)

文章存档

2014年(9)

2013年(23)

2012年(26)

2011年(3)

我的朋友

分类: LINUX

2012-01-11 09:42:23

使用gcc的-E -P选项展开源代码中的宏- -

                                      

原文出处:

内容简介:演示如何将C代码中的宏展开。
使用工具:gcc
测试平台:cygwin
任务描述:文件header1.h中描述了宏的声明,文件header2.h中使用到了这些宏,现在需要将文件header2.h中的宏展开,生成文件shit.h。
关键字:gcc cygwin macro #define expand precompile

文件1"header1.h",内容如下:
#define A1 10
#define A2 0x0A
#define A3 (A1+1)

文件2"header2.h",内容如下:
#include "header1.h"

typedef struct st_Shit
{
  int  m1[A1];
  int  m2[A2];
  char m3[A3];
}t_Shit;

使用如下命令:
gcc -E -P  -< header2.h > shit.h

header2.h中的宏展开之后,生成shit.h文件,内容如下:
typedef struct st_Shit
{
 int  m1[10 ];
 int  m2[0x0A ];
 char m3[(10 +1) ];
}t_Shit;

如果不使用-P开关,命令如下:
gcc -E -< header2.h > shit.h

header2.h中的宏展开之后,生成shit.h文件,内容如下:
# 1 ""
# 1 "header1.h" 1

# 1 "" 2

typedef struct st_Shit
{
 int  m1[10 ];
 int  m2[0x0A ];
 char m3[(10 +1) ];
}t_Shit;

可见,-P选项能够屏蔽掉这些垃圾内容

阅读(2213) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~