部分preprocess C或C ++源文件? [英] Partially preprocess a C or C++ source file?
问题描述
是否有部分pre-过程中的 C 或C ++源文件的方法吗?所谓部分preprocess我的意思是扩大一些,但不是所有的#include指令。例如,我想扩大#包括指着我的头的项目,而不是指向其他库头文件的#includes。
Is there a way to partially pre-process a C or C++ source file? By "partially preprocess" I mean expanding some but not all of the #include directives. For example, I would like to expand #includes pointing to my project headers, but not #includes pointing to other libraries' headers.
我试图做到这一点通过运行的gcc -E
仅在 -I
标记为我的项目标题和不是 -I
标志库,但是,这并不工作,因为gcc时遇到的#include它不能扩展提供了一个错误。
I tried to do this by running gcc -E
with only the -I
flags for my project headers and not the -I
flags for the libraries, but that doesn't work because gcc gives an error when it encounters an #include it cannot expand.
修改:我真的不关心preprocessor的相对于宏扩展行为
EDIT: I do not really care about the preprocessor's behaviour with respect to macro expansion.
推荐答案
的C preprocessor是不是足够聪明,这样做对自己。如果你只在的#include
感兴趣的话,你应该推出自己的工具(在譬如Perl)来处理的源文件,扩大了的#include
行你感兴趣,而忽略休息。
The C preprocessor isn't smart enough to do this on its own. If you're only interested in #include
, you should just roll your own tool (in, say, Perl) to process the source files, expanding #include
lines that interest you and ignoring the rest.
本脚本prefixes无趣标题行与 //忽略
:
This script prefixes uninteresting header lines with // Ignored
:
#!/usr/bin/perl
use warnings;
use strict;
my @uninteresting = qw(iostream vector map);
my $uninteresting = join '|', @uninteresting;
while (<>) {
s%(#include <(?:$uninteresting)>)%// Ignored $1%;
print;
}
现在你可以这样做:
cat sourcefile.cpp | perl ignore-meh.pl | g++ -E
如果你想获得真正看中的:
And if you want to get really fancy:
#!/usr/bin/perl
use warnings;
use strict;
while (<>) {
s%// Ignored (#include <[^>]+>)%$1%;
print;
}
现在你可以这样做:
cat sourcefile.cpp | perl ignore-meh.pl | g++ -E | perl restore-meh.pl
这篇关于部分preprocess C或C ++源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!