[英]Run a “light” preprocessor for GCC
Is there a way to run the GCC preprocessor, but only for user-defined macros?有没有办法运行GCC预处理器,但仅适用于用户定义的宏?
I have a few one-liners and some #ifdef
, etc. conditionals, and I want to see what my code looks like when just those are expanded.我有一些单行代码和一些
#ifdef
等条件,我想看看我的代码在扩展它们时是什么样子。
As it is, the includes get expanded, my fprintf(stderr)
s turn into fprintf(((__getreeent())->_stderr)
, etc.事实上,包含得到扩展,我的
fprintf(stderr)
变成了fprintf(((__getreeent())->_stderr)
等等。
Call cpp
directly, eg直接调用
cpp
,例如
$ cat >foo.c <<EOF
#define FOO
#ifdef FOO
foo is defined
#else
foo is not defined
#endif
EOF
$ cpp foo.c
# 1 "foo.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "foo.c"
foo is defined
Of course, if you include any headers then those will be included in the output.当然,如果您包含任何标题,那么这些标题将包含在输出中。 One way to avoid that might be to just
grep -v
out the lines with #include
s (or perhaps just ones with #include <
and allow #include "
). Or you could specify the -nostdinc
option to remove just standard includes (but possibly leave in local libraries unless you specify include paths so that they won't be found) - this would warn about missing headers, though.避免这种情况的一种方法可能是使用
grep -v
用#include
s(或者可能只是带有#include <
并允许#include "
)。或者您可以指定-nostdinc
选项以仅删除标准包含(但可能会留在本地库中,除非您指定包含路径以便它们不会被找到)——不过,这会警告缺少头文件。
Edit: Or use the preprocessor itself to make the inclusion of headers conditional, wrap them in something like #ifndef TESTING_PREPROCESSOR
and use -DTESTING_PREPROCESSOR
.编辑:或者使用预处理器本身来包含头文件,将它们包装在
#ifndef TESTING_PREPROCESSOR
类的东西中并使用-DTESTING_PREPROCESSOR
。
可以使用像 unifdef、unifdefall 这样的工具——从代码中删除预处理器条件
cpp -nostdinc program.c
gcc -E inputfile.c > outputfile.c
outputfile.c will have your preprocessed code, but all macros will be expanded. outputfile.c 将包含您的预处理代码,但所有宏都将被扩展。
I find this trick very useful when debugging compilation of large systems with tons of includes, compiler flags, and makefile variables.我发现这个技巧在调试包含大量包含、编译器标志和 makefile 变量的大型系统的编译时非常有用。 It will expose include files that don't have header guards, and a bunch of other problems too.
它会暴露没有标题保护的包含文件,以及一堆其他问题。
如果只需要宏扩展并跳过#include
处理,则可能会有所帮助: https : //github.com/ned14/pcpp/pull/34
If you just want macro expansions and skip the #include
handling, you can try this repo .如果你只是想要宏扩展并跳过
#include
处理,你可以试试这个repo 。 It is a fork of the original pcpp .它是原始pcpp 的一个分支。 I added a
--no-include
option to skip the handling of #include
directive.我添加了一个
--no-include
选项来跳过#include
指令的处理。 All other macros will be processed based on your marco input to pcpp
.所有其他宏将根据您对
pcpp
的 marco 输入进行处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.