我如何查看 C/C++ 在 Visual Studio 中预处理后的源文件?
假设我有一个包含许多预处理器指令的源文件。 是否可以看到预处理器完成后的样子?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
cl.exe
是 Microsoft Visual C++ 的命令行界面,具有三个不同的选项用于输出预处理文件(因此前面有关 Visual C++ 的响应不一致):/E
: 预处理到 stdout (类似于 GCC 的 -E 选项)/P
: 预处理到文件/ EP
:在没有 #line 指令的情况下预处理到 stdout如果您想要预处理没有 #line 指令的文件,请结合
/P
和/EP
选项。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 directivesIf you want to preprocess to a file without #line directives, combine the
/P
and/EP
options.大多数编译器都可以选择只运行预处理器。 例如,gcc提供了-E:
所以你可以直接运行:
如果你找不到这样的选项,你也可以直接在你的机器上找到C预处理器。 它通常称为 cpp,并且可能已经在您的路径中。 像这样调用它:
如果需要从其他目录包含标头,则可以将 -I/path/to/include/dir 传递给其中任何一个,就像常规编译一样。
对于 Windows,我将把它留给其他发帖者来提供答案,因为我不是这方面的专家。
Most compilers have an option to just run the preprocessor. e.g., gcc provides -E:
So you can just run:
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:
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.
For Windows, I'll leave it to other posters to provide answers as I'm no expert there.
右键单击解决方案资源管理器中的文件,转到“属性”。 在“配置属性”->“C/C++”->“预处理器”下,您要查找“生成预处理文件”。 然后在解决方案资源管理器中右键单击该文件并选择“编译”。 预处理文件在输出目录(例如Release、Debug)中创建,扩展名为.i(感谢Steed 的评论)。
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).
您通常需要对预处理器的输出进行一些后处理,否则所有宏只会扩展为一个行,这很难阅读和调试。 对于 C 代码,类似下面的内容就足够了:
对于 C++ 代码,它实际上要困难得多。 对于 GCC/g++,我发现这个 Perl 脚本很有用。
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:
For C++ code, it's actually a lot harder. For GCC/g++, I found this Perl script useful.
我对 Microsoft 编译器一无所知,但在 GCC 上你可以使用这个:
如果你想查看评论,请使用这个:
更多选项可在 此页面。
I don't know anything about Microsoft compiler, but on GCC you can use this:
If you want to see comments use this:
More options avaliable on this page.
如果您使用的是 Microsoft 的 C++ 编译器,请尝试
cl /EP
。Try
cl /EP
if you are using Microsoft's C++ compiler.正如 bk1e 和 Andreas M. 回答的那样,编译器的 /P 选项将导致它预处理文件。 但是,在我使用 VS2005 和 Platform Builder(针对嵌入式 ARM 处理器)的项目中,该项目没有在对话框中提供启用该选项的选项(如 Jim B 所描述)。
我可以手动运行 CL 并添加 /P,但它失败了,因为我不知道在完整构建过程中 Platform Builder 无形中激活的所有适当的命令行选项。 所以我需要了解所有这些选项。
我的解决方案是查看 build.log 文件,找到执行
CL blah-blah-blah myfile.c
的行,我将此行复制到剪贴板。 “等等”部分包含构建选项,并且非常庞大。
回到 IDE,我右键单击 myfile.c,选择“打开构建窗口”,然后在该窗口中粘贴构建命令行,并添加“/P”。
CL /P blah-blah-blah myfile.c
完成。 生成了 myfile.i 文件,其中包含预处理器输出。
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.
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.
My solution was to go look in the build.log file, and find the line that executed
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.
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".
CL /P blah-blah-blah myfile.c
Done. The myfile.i file was produced, which contained the preprocessor output.
在 Visual Studio 中,您可以使用 /P 编译文件(或项目)。
In Visual Studio you can compile a file (or project) with /P.
CPIP 是一个用 Python 编写的新 C/C++ 预处理器。 如果您想要预处理文件的详细视觉表示,请尝试一下。
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.
在 Windows 操作系统上,这个问题的简单一行答案是在 DOS 提示符下使用以下命令来查看预处理文件:
这将生成一个名为 myprogram.i 的文件。 打开它并查找扩展的预处理器。
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:
This will generate a file called myprogram.i. Open it and look out for your expanded preprocessors.