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

原文:http://journeyboy.bokee.com/614292.html

内容简介:演示如何将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
或 
gcc -E -P -o shit.h header2.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
或 
gcc -E -o shit.h header2.h

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

# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "<stdin>"

# 1 "header1.h" 1
# 3 "<stdin>" 2

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

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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值