将#includes扩展为C ++的文本文件 [英] Expand #includes to a text file for C++
问题描述
是否可以使用 C 预处理程序扩展c ++文件的 #include
行,以便我可以在没有#的情况下读取扩展文件包含
,但包含 #include
d?
Is it possible to expand out #include
lines of a c++ file, probably using the C preprocessor, such that I can read an extended file without #includes
, but instead with the files that are #include
d?
具体来说,如果我有
fileA.cpp:
fileA.cpp:
#include "fileB.H"
int main()
{
//do whatever
return(0);
}
fileB.H:
#include "fileC.H"
//Some lines of code
fileC.H
//Some other lines of code
并输出:
//Some other lines of code
//Some lines of code
int main()
{
//do whatever
return(0);
}
本质上,无需复制即可将粘贴的文件复制粘贴到一个大的text/C ++代码文件中?
Essentially, copy-pasting the files that are included into one large text/C++ code file, without compiling?
如果我使用相关的 -I<包含要包含的文件的目录
运行cpp,那么我会得到一个长文本文件,而不仅仅是代码,它给出了将传递给编译器的内容(h!)
If I run the cpp with relevant -I<Directory containing files to include>
then I get a long text file, but rather than just code, it gives what would be passed to the compiler (duh!)
推荐答案
对于gcc和clang,您可以使用-E选项(看到类似的答案),无需编译即可输出预处理器输出.
For gcc and clang, you can use the -E option (see similar answer) to output the preprocessor output without compiling.
要在示例输出中也显示注释,可以添加-CC和-P标志:
To also show comments like in your sample output, you can add in the -CC and -P flags:
clang ++ -E -CC -P fileA.cpp
-E的所有处理器选项都可以可以在gcc.gnu.org上找到.
-CC 请勿丢弃注释,包括在宏扩展过程中.就像-C一样,除了宏中包含的注释也被传递到扩展宏的输出文件.
-CC Do not discard comments, including during macro expansion. This is like -C, except that comments contained within macros are also passed through to the output file where the macro is expanded.
-P 禁止在预处理器的输出中生成线标记.在运行预处理器时,这可能会很有用不是C代码的东西,将被发送到程序可能会被线标记弄糊涂了.
-P Inhibit generation of linemarkers in the output from the preprocessor. This might be useful when running the preprocessor on something that is not C code, and will be sent to a program which might be confused by the linemarkers.
对于Visual C ++编译器,可以使用/E.请参阅此SO答案.
For the Visual C++ compiler, you can use /E. See this SO answer.
这篇关于将#includes扩展为C ++的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!