原文: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选项能够屏蔽掉这些垃圾内容。