使用 GCC 对 C 文件进行部分预处理(不删除 "define" 指令)
Partial preprocessing of C files with GCC (not removing "define" directives)
如果我传递 -E
或 -save-temps
命令行参数,GCC 可以输出完全预处理的 C++ 源文件。
我的问题是,我能否以某种方式获得部分预处理的 C++ 源文件,其中
a) 不满足#if、#ifdef、#ifndef条件的代码片段被剔除,
b) #include 指令已解析(包含头文件)等
但是
c) 普通的#define 指令未解析?
(这将是必要的并且非常有帮助,因为我希望获得尽可能紧凑和可读的输出。
解析#if 指令会缩短源代码,但解析#define 指令会使源代码的可读性降低且冗余。)
我已尝试创建一个尽可能紧凑的示例,以演示我想要实现的目标:
示例输入文件:
// header1.h
#ifndef header1_h
#define header1_h
int function1(int val) {
return val + MYCONST;
}
#endif
// header2.h
#ifndef header2_h
#define header2_h
int function1(int val) {
return val + val + MYCONST;
}
#endif
// main.c
#define MYCONST 1234
#define SETTING1
#ifdef SETTING1
#include "header1.h"
#endif
#ifdef SETTING2
#include "header2.h"
#endif
int main(void) {
int retVal = function1(99);
}
预期输出:
// main.i (GCC preprocessing output)
#define MYCONST 1234 // I would like to see the definition of MYCONST here
#define SETTING1
#define header1_h
int function1(int val) {
return val + MYCONST; // I would like to see MYCONST here instead of the resolved value
}
int main(void) {
int retVal = function1(99);
}
gcc
有一个选项 -fdirectives only
可以做一些接近你想要的事情:
-fdirectives-only
When preprocessing, handle directives, but do not expand macros.
The option’s behavior depends on the -E and -fpreprocessed options.
With -E, preprocessing is limited to the handling of directives such as #define, #ifdef, and #error. Other preprocessor operations, such as macro expansion and trigraph conversion are not performed. In addition, the -dD option is implicitly enabled.
With -fpreprocessed, predefinition of command line and most builtin macros is disabled. Macros such as __LINE__
, which are contextually dependent, are handled normally. This enables compilation of files previously preprocessed with -E -fdirectives-only.
With both -E and -fpreprocessed, the rules for -fpreprocessed take precedence. This enables full preprocessing of files previously preprocessed with -E -fdirectives-only.
在你的情况下,它应该被称为
% gcc -fdirectives-only -E -o main.i main.c
但是您得到的定义(那些内部定义的)、空行和 #line
行比您要求的要多。
如果我传递 -E
或 -save-temps
命令行参数,GCC 可以输出完全预处理的 C++ 源文件。
我的问题是,我能否以某种方式获得部分预处理的 C++ 源文件,其中
a) 不满足#if、#ifdef、#ifndef条件的代码片段被剔除,
b) #include 指令已解析(包含头文件)等
但是
c) 普通的#define 指令未解析?
(这将是必要的并且非常有帮助,因为我希望获得尽可能紧凑和可读的输出。 解析#if 指令会缩短源代码,但解析#define 指令会使源代码的可读性降低且冗余。)
我已尝试创建一个尽可能紧凑的示例,以演示我想要实现的目标:
示例输入文件:
// header1.h
#ifndef header1_h
#define header1_h
int function1(int val) {
return val + MYCONST;
}
#endif
// header2.h
#ifndef header2_h
#define header2_h
int function1(int val) {
return val + val + MYCONST;
}
#endif
// main.c
#define MYCONST 1234
#define SETTING1
#ifdef SETTING1
#include "header1.h"
#endif
#ifdef SETTING2
#include "header2.h"
#endif
int main(void) {
int retVal = function1(99);
}
预期输出:
// main.i (GCC preprocessing output)
#define MYCONST 1234 // I would like to see the definition of MYCONST here
#define SETTING1
#define header1_h
int function1(int val) {
return val + MYCONST; // I would like to see MYCONST here instead of the resolved value
}
int main(void) {
int retVal = function1(99);
}
gcc
有一个选项 -fdirectives only
可以做一些接近你想要的事情:
-fdirectives-only
When preprocessing, handle directives, but do not expand macros.
The option’s behavior depends on the -E and -fpreprocessed options.
With -E, preprocessing is limited to the handling of directives such as #define, #ifdef, and #error. Other preprocessor operations, such as macro expansion and trigraph conversion are not performed. In addition, the -dD option is implicitly enabled.
With -fpreprocessed, predefinition of command line and most builtin macros is disabled. Macros such as
__LINE__
, which are contextually dependent, are handled normally. This enables compilation of files previously preprocessed with -E -fdirectives-only.With both -E and -fpreprocessed, the rules for -fpreprocessed take precedence. This enables full preprocessing of files previously preprocessed with -E -fdirectives-only.
在你的情况下,它应该被称为
% gcc -fdirectives-only -E -o main.i main.c
但是您得到的定义(那些内部定义的)、空行和 #line
行比您要求的要多。