假设我有一个带有许多预处理器指令的源文件。是否有可能看到它在预处理器完成后的样子?
当前回答
正如bk1e和Andreas M.回答的那样,编译器的/P选项将导致它对文件进行预处理。然而,在我使用VS2005和Platform Builder(用于嵌入式ARM处理器)的项目中,该项目没有在对话框中显示选项(如Jim B所描述的)来启用该选项。
我可以手动运行CL并添加/P,但它失败了,因为我不知道Platform Builder在完整构建期间无形地激活的所有适当的命令行选项。所以我需要知道所有的选项。
我的解决方案是查看build.log文件,并找到执行的行
CL等等myfile。c
我把这行复制到剪贴板上。“废话”部分包含构建选项,而且非常大。
回到IDE,我右键单击myfile.c,选择“Open Build Window”,然后在该窗口中粘贴构建命令行,并添加“/P”。
CL /P等等myfile.c
完成了。myfile。生成I文件,其中包含预处理器输出。
其他回答
大多数编译器都有只运行预处理器的选项。例如,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,我将把它留给其他人来提供答案,因为我不是这方面的专家。
如果您正在使用微软的c++编译器,请尝试cl /EP。
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.
我对微软编译器一无所知,但在GCC上你可以使用这个:
gcc -E -P -o result.c my_file.h
如果你想看到注释,使用这个:
gcc -E -C -P -o result.c my_file.h
本页有更多选项。
cl.exe, Microsoft Visual c++的命令行接口,有三个不同的选项来输出预处理文件(因此在前面关于Visual c++的响应中不一致):
/E:预处理到stdout(类似于GCC的-E选项) /P:预处理到文件 /EP:预处理到标准输出,不使用#line指令
如果您希望预处理到一个没有#line指令的文件,请结合/P和/EP选项。
推荐文章
- Visual Studio:如何打破处理异常?
- 在C和c++中静态变量存储在哪里?
- 确保您的项目构建设置正在生成一个dSYM文件。对于所有配置,DEBUG_INFORMATION_FORMAT都应该设置为dwarf-with-dsym
- 如何获得GDB中所有线程的回溯?
- 为什么标准迭代器范围是[begin, end]而不是[begin, end]?
- c++双地址操作符?(& &)
- errno线程安全吗?
- 如何在C程序中获取当前目录?
- 函数标题中的箭头操作符(->)
- 如何在c++中初始化一个向量
- 返回类型为'?:'(三元条件运算符)
- 当分配vector时,它们使用的是堆上的内存还是堆栈上的内存?
- 互斥实例/教程?
- 如何添加一个'或'条件在#ifdef
- 纯虚函数的实现