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

发布于 2024-07-08 07:25:32 字数 47 浏览 9 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

愁杀 2024-07-15 07:25:32

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

如果您想要预处理没有 #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++):

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

浮华 2024-07-15 07:25:32

大多数编译器都可以选择只运行预处理器。 例如,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.

所以你可以直接运行:

gcc -E foo.c

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

cpp foo.c

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

对于 Windows,我将把它留给其他发帖者来提供答案,因为我不是这方面的专家。

Most compilers have an option to just run the preprocessor. e.g., gcc provides -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:

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.

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

守望孤独 2024-07-15 07:25:32

右键单击解决方案资源管理器中的文件,转到“属性”。 在“配置属性”->“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).

佼人 2024-07-15 07:25:32

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

gcc -E code.c | sed '/^\#/d' | indent -st -i2 > code-x.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:

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.

苦笑流年记忆 2024-07-15 07:25:32

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

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

如果你想查看评论,请使用这个:

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

更多选项可在 此页面

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

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.

南烟 2024-07-15 07:25:32

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

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

审判长 2024-07-15 07:25:32

正如 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.

枯寂 2024-07-15 07:25:32

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

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

很糊涂小朋友 2024-07-15 07:25:32

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

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

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

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

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.

睫毛上残留的泪 2024-07-15 07:25:32

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

CL /P /C myprogram.c

这将生成一个名为 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:

CL /P /C myprogram.c

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文