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


当前回答

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

其他回答

您通常需要对预处理器的输出进行一些后处理,否则所有宏都将扩展为一行,这很难读取和调试。对于C代码,像下面这样的代码就足够了:

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

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

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

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.

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

CL /P /C myprogram.c

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

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

如果您正在使用微软的c++编译器,请尝试cl /EP。