3

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
chrisb2244
  • 2,940
  • 22
  • 44
  • Have found http://stackoverflow.com/questions/2008487/can-i-expand-include-files-inline-and-not-expand-directives?rq=1 as a similar (identical?) question, but the answer that's been accepted doesn't seem very clear to me - from what I'm reading it says that this is not possible, but apart from 'this is a stupid idea' I don't see why it shouldn't be possible... – chrisb2244 Feb 10 '14 at 07:05
  • 2
    If you are using gcc or clang, you can use -E: clang++ -E fileA.cpp (http://stackoverflow.com/questions/3742822/preprocessor-output) – Phillip Kinkade Feb 10 '14 at 07:08
  • @PhillipKinkade Thank you - I'd found the -E flag although clearly failed to interpret the output properly. This seems to be (roughly) what I'm looking for - I just need to read more carefully through the file it's given me to find the relevant sections. Cheers – chrisb2244 Feb 10 '14 at 07:24
  • @PhillipKinkade If you want to give an answer roughly to that effect, I'll mark it as accepted? – chrisb2244 Feb 10 '14 at 07:25

1 Answers1

5

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.

Community
  • 1
  • 1
Phillip Kinkade
  • 1,382
  • 12
  • 18
  • `-CC` is nice, but `-P` deletes the markers that tells you which file is which! Why would you pass that? – Potatoswatter Feb 10 '14 at 07:40
  • @Potatoswatter -P is optional. I included it here to look like the desired output in the original post. – Phillip Kinkade Feb 10 '14 at 07:41
  • I wouldn't read too much into the lack of an imaginative proposal of line markers where he doesn't know what to expect. They are essential for human interpretation of the output. – Potatoswatter Feb 10 '14 at 07:44
  • @Potatoswatter Yes and no. Quite often, if I'm only interested in how certain macro stuff expands, I'll just search the preprocessed file for the relevant symbols. So while they're useful for certain purposes, I wouldn't call them essential. – Angew is no longer proud of SO Feb 10 '14 at 08:05
  • Although obviously implementation dependent, `-E`/`/E` seem to be almost universal; the very first C compilers used `-E` to stop after the preprocessor (which was a separate step then), with preprocessor output going to standard out, and practically every compiler since has done likewise. – James Kanze Feb 10 '14 at 09:08