将#includes 扩展为 C++ 的文本文件

chr*_*244 3 c++ c-preprocessor

是否可以扩展#includec++ 文件的行,可能使用C预处理器,这样我就可以读取扩展文件而#includes不是#included的文件?

具体来说,如果我有

文件A.cpp:

#include "fileB.H"

int main()
{
//do whatever
return(0);
}
Run Code Online (Sandbox Code Playgroud)

文件B.H:

#include "fileC.H"
//Some lines of code
Run Code Online (Sandbox Code Playgroud)

文件C.H

//Some other lines of code
Run Code Online (Sandbox Code Playgroud)

和输出:

//Some other lines of code
//Some lines of code

int main()
{
//do whatever
return(0);
}
Run Code Online (Sandbox Code Playgroud)

本质上,将包含的文件复制粘贴到一个大型文本/C++ 代码文件中,而不进行编译?

如果我使用相关运行 cpp,-I<Directory containing files to include>那么我会得到一个长文本文件,但不仅仅是代码,它给出了将传递给编译器的内容(废话!)

Phi*_*ade 5

对于 gcc 和 clang,您可以使用 -E 选项(参见类似答案)来输出预处理器输出而无需编译。

要在示例输出中显示注释,您可以添加 -CC 和 -P 标志:

clang++ -E -CC -P fileA.cpp

-E 的所有处理器选项都可以在 gcc.gnu.org 上找到。

-CC不要丢弃注释,包括在宏扩展期间。这与 -C 类似,不同之处在于宏中包含的注释也会传递到宏展开的输出文件。

-P禁止在预处理器的输出中生成线标记。当在非 C 代码的东西上运行预处理器时,这可能很有用,并且将被发送到可能被行标记混淆的程序。

对于 Visual C++ 编译器,您可以使用 /E。请参阅此 SO 答案。