2

This answer regarding header and source files says:

The compiler sees one big source (.cpp) file with its headers properly included. The source file is the compilation unit that will be compiled into an object file.

Is it possible to view somehow this big .cpp (.c) file? If yes, how to do it on both Linux (gcc) and Windows (VisualStudio) platforms.

PS: I've tried /P option in Visual Studio (Right-click on the file on the Solution Explorer, goto Properties; Configuration Properties -> C/C++ -> Preprocessor -> Preprocess to a File -> [YES /P]) but I've obtained several *.i files which no more looked like c++ (maybe it is c++ but with many templates, definitions for memory allocations etc.). The answer I was referring to was talking about "one big .cpp file". So I've assumed that only #include directives will be replaced with corresponding files, or am I wrong and the *.i output is what I was asked for?

Community
  • 1
  • 1
Wakan Tanka
  • 7,542
  • 16
  • 69
  • 122

1 Answers1

5

On Linux use -E gcc option. It will print the "big" source code to stdout, allowing you to redirect it to a file.

On Visual Studio use the following options: /E for stdout or /P to print to a file.

By the way, your questions were already asked separately:

Those *.i files are exactly what you asked for, as you get one *.i file per one *.cpp file. You'll get the same on GNU compilers if you feed a single source file to gcc -E. An *.i file is a *.cpp file with all include completely unrolled. I suppose your C++ files included standard library headers directly or indirectly through other includes, so you got lots of scary-looking code in your preprocessed files. Nevertheless, it's still C++, and it's somewhat more "pure" C++ than what you have in your sources. Visual Studio preprocessor also includes #line directives to it's output. They will be used by compiler to report correct line numbers on compilation errors. You may suppress them with /EP.

To check that everything is correct you may create an empty project and play with a few of your own source and headers files without including any standard library or third-party headers.

Community
  • 1
  • 1
Sergey
  • 7,985
  • 4
  • 48
  • 80
  • 1
    Please don't answer question for which you found duplicates (or are just obvious duplicates). Flag it as a duplicate instead so it can be closed. – 2501 Aug 25 '16 at 09:04
  • @2501 please see my edit – Wakan Tanka Aug 25 '16 at 09:15
  • @2501 Ok, I'll mind it next time. Nevertheless, in this case the author edited his question and it is no longer a duplicate. May the duplicate mark be canceled? – Sergey Aug 25 '16 at 10:17
  • @Sergey Please explain why is the question not a duplicate. – 2501 Aug 25 '16 at 10:40