How do I see a C/C++ source file after preprocessing in Visual Studio?

前端 未结 10 740
一个人的身影
一个人的身影 2020-11-22 05:44

Let\'s say I have a source file with many preprocessor directives. Is it possible to see how it looks after the preprocessor is done with it?

相关标签:
10条回答
  • 2020-11-22 06:21

    cl.exe, the command line interface to Microsoft Visual C++, has three different options for outputting the preprocessed file (hence the inconsistency in the previous responses about Visual C++):

    • /E: preprocess to stdout (similar to GCC's -E option)
    • /P: preprocess to file
    • /EP: preprocess to stdout without #line directives

    If you want to preprocess to a file without #line directives, combine the /P and /EP options.

    0 讨论(0)
  • 2020-11-22 06:27

    CPIP is a new C/C++ preprocessor written in Python. If you want a detailed visual representation of a preprocessed file, give it a shot.

    CPIP is a C/C++ pre-processor implemented in Python. Most pre-processors regard pre-processing as a dirty job that just has to be done as soon as possible. This can make it very hard to track down subtle defects at the pre-processing stage as pre-processors throw away a lot of useful information in favor of getting the result as cheaply as possible.

    Few developers really understand pre-processing, to many it is an obscure bit of black magic. CPIP aims to improve that and by recording every detail of preprocessing so CPIP can can produce some wonderfully visual information about file dependencies, macro usage and so on.

    CPIP is not designed to be a replacement for cpp (or any other established pre-processor), instead CPIP regards clarity and understanding as more important than speed of processing.

    0 讨论(0)
  • 2020-11-22 06:36

    You typically need to do some postprocessing on the output of the preprocessor, otherwise all the macros just expand to one liners, which is hard to read and debug. For C code, something like the following would suffice:

    gcc -E code.c | sed '/^\#/d' | indent -st -i2 > code-x.c
    

    For C++ code, it's actually a lot harder. For GCC/g++, I found this Perl script useful.

    0 讨论(0)
  • 2020-11-22 06:36

    In Visual Studio you can compile a file (or project) with /P.

    0 讨论(0)
提交回复
热议问题