Expand #includes to a text file for C++

泄露秘密 提交于 2021-02-19 05:23:35

问题


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 #included?

To be concrete, if I have

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

And output:

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

int main()
{
//do whatever
return(0);
}

Essentially, copy-pasting the files that are included into one large text/C++ code file, without compiling?

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!)


回答1:


For gcc and clang, you can use the -E option (see similar answer) to output the preprocessor output without compiling.

To also show comments like in your sample output, you can add in the -CC and -P flags:

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

All of the processor options for -E can be found on here, on gcc.gnu.org.

-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 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.

For the Visual C++ compiler, you can use /E. See this SO answer.



来源:https://stackoverflow.com/questions/21670816/expand-includes-to-a-text-file-for-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!