在 Visual Studio 中预处理后如何查看 C/C++ 源文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/277258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:17:04  来源:igfitidea点击:

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

c++cdebuggingvisual-studio-2005c-preprocessor

提问by Geo

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?

假设我有一个包含许多预处理器指令的源文件。是否有可能看到预处理器完成后它的外观?

采纳答案by bk1e

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++):

cl.exe,Microsoft Visual C++ 的命令行界面,具有三种不同的输出预处理文件的选项(因此与之前关于 Visual C++ 的响应不一致):

If you want to preprocess to a file without #line directives, combine the /Pand /EPoptions.

如果要在没有 #line 指令的情况下预处理到文件,请结合使用/P/EP选项。

回答by Todd Gamblin

Most compilers have an option to just run the preprocessor. e.g., gcc provides -E:

大多数编译器都可以选择只运行预处理器。 例如,gcc 提供 -E:

   -E  Stop after the preprocessing stage; do not run the compiler proper.  
       The output is in the form of preprocessed source code, which is sent
       to the standard output.

So you can just run:

所以你可以运行:

gcc -E foo.c

If you can't find such an option, you can also just find the C preprocessor on your machine. It's usually called cpp and is probably already in your path. Invoke it like this:

如果你找不到这样的选项,你也可以在你的机器上找到 C 预处理器。它通常称为 cpp 并且可能已经在您的路径中。像这样调用它:

cpp foo.c

If there are headers you need to include from other directories , you can pass -I/path/to/include/dir to either of these, just as you would with a regular compile.

如果您需要从其他目录中包含头文件,您可以将 -I/path/to/include/dir 传递给其中任何一个,就像使用常规编译一样。

For Windows, I'll leave it to other posters to provide answers as I'm no expert there.

对于 Windows,我将把它留给其他海报来提供答案,因为我不是那里的专家。

回答by Jim Buck

Right-click on the file on the Solution Explorer, goto Properties. Under Configuration Properties->C/C++->Preprocessor, "Generate Preprocessed File" is what you are looking for. Then right-click on the file in the Solution Explorer and select "Compile". The preprocessed file is created in the output directory (e.g. Release, Debug) with an extension .i (thanks to Steed for his comment).

右键单击解决方案资源管理器上的文件,转到属性。在 Configuration Properties->C/C++->Preprocessor 下,“生成预处理文件”就是你要找的。然后在解决方案资源管理器中右键单击该文件并选择“编译”。预处理文件是在输出目录(例如 Release、Debug)中创建的,扩展名为 .i(感谢 Steed 的评论)。

回答by ididak

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:

您通常需要对预处理器的输出进行一些后处理,否则所有宏只会扩展为一个行,这很难阅读和调试。对于 C 代码,类似以下内容就足够了:

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 scriptuseful.

对于 C++ 代码,它实际上要困难得多。对于 GCC/g++,我发现这个 Perl 脚本很有用。

回答by dzav

I don't know anything about Microsoft compiler, but on GCC you can use this:

我对微软编译器一无所知,但在 GCC 上你可以使用这个:

gcc -E -P -o result.c my_file.h

If you want to see comments use this:

如果您想查看评论,请使用以下命令:

gcc -E -C -P -o result.c my_file.h

More options avaliable on this page.

此页面上提供了更多选项。

回答by Atempcode

Try cl /EPif you are using Microsoft's C++ compiler.

cl /EP如果您使用的是 Microsoft 的 C++ 编译器,请尝试。

回答by Andreas Magnusson

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

在 Visual Studio 中,您可以使用 /P 编译文件(或项目)。

回答by Sam Pittman

As bk1e and Andreas M. answered, the /P option for the compiler will cause it to preprocess a file. However, in my project using VS2005 and Platform Builder (for an embedded ARM processor), the project did not present an option in the dialog box (as described by Jim B) to enable that option.

正如 bk1e 和 Andreas M. 回答的那样,编译器的 /P 选项将使其预处理文件。但是,在我使用 VS2005 和 Platform Builder(用于嵌入式 ARM 处理器)的项目中,该项目没有在对话框中显示选项(如 Jim B 所述)来启用该选项。

I could run CL manually and add /P, but it failed because I did not know all of the appropriate command-line options that were invisibly being activated by Platform Builder during the full build. So I needed to know all of those options.

我可以手动运行 CL 并添加 /P,但它失败了,因为我不知道在完整构建期间由 Platform Builder 不可见地激活的所有适当的命令行选项。所以我需要知道所有这些选项。

My solution was to go look in the build.log file, and find the line that executed

我的解决方案是查看 build.log 文件,并找到执行的行

CL blah-blah-blah myfile.c

CL blah-blah-blah myfile.c

I copied this line to the clipboard. The "blah-blah-blah" part contained the build options, and was huge.

我将此行复制到剪贴板。“blah-blah-blah”部分包含构建选项,并且非常庞大。

Back in the IDE, I right-clicked on myfile.c, chose "Open Build Window", and then in that window I pasted the build command-line, and added a "/P".

回到 IDE,我右键单击 myfile.c,选择“打开构建窗口”,然后在该窗口中粘贴构建命令行,并添加一个“/P”。

CL /P blah-blah-blah myfile.c

CL /P blah-blah-blah myfile.c

Done. The myfile.i file was produced, which contained the preprocessor output.

完毕。生成了 myfile.i 文件,其中包含预处理器输出。

回答by thegreendroid

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

CPIP是一个用 Python 编写的新的 C/C++ 预处理器。如果您想要预处理文件的详细可视化表示,请试一试。

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.

CPIP 是一个用 Python 实现的 C/C++ 预处理器。大多数预处理器认为预处理是一项必须尽快完成的肮脏工作。这会使在预处理阶段追踪细微缺陷变得非常困难,因为预处理器会丢弃大量有用的信息,以尽可能便宜地获得结果。

很少有开发人员真正了解预处理,对许多人来说,这是一种晦涩的黑魔法。CPIP 旨在改进这一点,并通过记录预处理的每个细节,以便 CPIP 可以生成一些关于文件依赖项、宏使用等的精彩视觉信息。

CPIP 并非旨在替代 cpp(或任何其他已建立的预处理器),而是 CPIP 认为清晰度和理解比处理速度更重要。

回答by manty

On Windows OS, a simple one line answer to this question is to use the below command in DOS prompt to see the preprocessed file:

在 Windows 操作系统上,这个问题的一个简单的单行答案是在 DOS 提示符下使用以下命令来查看预处理文件:

CL /P /C myprogram.c

This will generate a file called myprogram.i. Open it and look out for your expanded preprocessors.

这将生成一个名为myprogram.i的文件。打开它并注意扩展的预处理器。