C++: making a single file without includes
Asked Answered
D

1

6

This might sound like a very weird question, but I do need this: I have a bunch of c++ header files (from some header-only libraries) and a single cpp file. What I'd like to do is generate a single cpp file that has no includes whatsoever.

Why? For a contest, in which the compiler doesn't have some libraries I'm using, and one in which you can only submit one single cpp file.

Ideally, this "script" would create a file that uses only what is actually needed, not just copy and paste recursively all "#includes".

I'm sure that the preprocessor generates some files when you add an "#include" in the code. So, how can I see these intermediate and (probably very long) files?

Dorison answered 14/1, 2016 at 3:21 Comment(1)
Consult your compiler's reference manual. Most compilers allow you to write preprocessed output to disk.Leatri
M
8
  • GCC provides the -E option, so it stops after preprocessing the files. The documentation has more options that can be applied to the preprocessor.

  • clang also accepts -E, as stated here.

  • Visual Studio provides the /P (Preprocess to a File) compiler switch. It instructs the compiler to:

    Preprocess[es] C and C++ source files and write[s] the preprocessed output to a file.

    The output is written to a file with a file name based on the source file with an .i extension. To specify a different file name, use the /Fi (Preprocess Output File Name) compiler switch.

  • Intel C++ Compiler will stop after preprocessing if you pass either the -E option (outputs to stdout) or -P (outputs to a file). On Windows these will be the /E and /P switches respectively. The manual has more information on this.

Margerymarget answered 14/1, 2016 at 3:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.