假设我有一个带有许多预处理器指令的源文件。是否有可能看到它在预处理器完成后的样子?
当前回答
您通常需要对预处理器的输出进行一些后处理,否则所有宏都将扩展为一行,这很难读取和调试。对于C代码,像下面这样的代码就足够了:
gcc -E code.c | sed '/^\#/d' | indent -st -i2 > code-x.c
对于c++代码,这实际上要难得多。对于GCC/g++,我发现这个Perl脚本很有用。
其他回答
我对微软编译器一无所知,但在GCC上你可以使用这个:
gcc -E -P -o result.c my_file.h
如果你想看到注释,使用这个:
gcc -E -C -P -o result.c my_file.h
本页有更多选项。
在Visual Studio中,您可以使用/P编译文件(或项目)。
如果您正在使用微软的c++编译器,请尝试cl /EP。
cl.exe, Microsoft Visual c++的命令行接口,有三个不同的选项来输出预处理文件(因此在前面关于Visual c++的响应中不一致):
/E:预处理到stdout(类似于GCC的-E选项) /P:预处理到文件 /EP:预处理到标准输出,不使用#line指令
如果您希望预处理到一个没有#line指令的文件,请结合/P和/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.
推荐文章
- decltype(auto)的一些用途是什么?
- Shared_ptr转换为数组:应该使用它吗?
- 使用C返回一个数组
- Printf与std::字符串?
- 禁用复制构造函数
- 只接受特定类型的c++模板
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 如何为KnockoutJS调试模板绑定错误?
- 为什么pthreads的条件变量函数需要互斥?
- c++ 11中的递归lambda函数
- 如何在构建目标之外生成gcc调试符号?
- 在c++中指针使用NULL或0(零)吗?
- 在c++中,如何将int值附加到字符串中?
- __FILE__宏显示完整路径
- 就性能而言,使用std::memcpy()还是std::copy()更好?