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?

1
  • 1
    Consult your compiler's reference manual. Most compilers allow you to write preprocessed output to disk. Commented Jan 14, 2016 at 3:27

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.