简体   繁体   English

预处理器输出

[英]Preprocessor output

How do I view the output produced by the C pre-processor, prior to its conversion into an object file?在转换为目标文件之前,如何查看C预处理器生成的输出?

I want to see what the MACRO definitions do to my code.我想看看 MACRO 定义对我的代码做了什么。

gcc -E file.c

or

g++ -E file.cpp

will do this for you.会为你做这件事。 The -E switch forces the compiler to stop after the preprocessing phase, spitting all it's got at the moment to standard output. -E 开关强制编译器在预处理阶段结束后停止,将当前得到的所有内容输出到标准输出。

Note: Surely you must have some #include directives.注意:当然你必须有一些 #include 指令。 The included files get preprocessed, too, so you might get lots of output.包含的文件也经过预处理,因此您可能会得到大量输出。

For Visual C++ the switch is /E which spits the preprocessor output to screen.对于 Visual C++,开关是 /E,它将预处理器输出输出到屏幕。

You can also call the C Preprocessor directly.您也可以直接调用 C 预处理器。

cpp infile outfile

Check out man cpp for more info.查看man cpp了解更多信息。

For GCC,对于海湾合作委员会,

gcc -E -dM file.c

or

g++ -E -dM file.cpp

should do the job.应该做的工作。 -dM, as GNU Preprocessor manual puts it , should generate a list of '#define' directives for all the macros defined during the execution of the preprocessor, including predefined macros. -dM,正如 GNU 预处理器手册所说的那样,应该为预处理器执行期间定义的所有宏生成一个“#define”指令列表,包括预定义的宏。

It depends on the compiler you use.这取决于您使用的编译器。
With GCC, you can specify the -E flag on the command-line to let the compiler produce the pre-processor output.使用 GCC,您可以在命令行上指定-E标志,让编译器生成预处理器输出。

You can check out my script described here:您可以查看这里描述的我的脚本:

http://mosermichael.github.io/cstuff/all/projects/2011/09/16/preprocessor.html http://mosermichael.github.io/cstuff/all/projects/2011/09/16/preprocessor.html

It formats the preprocessor output into a (hopefully) readable html document: lines that are different due to preprocessor are marked in the file.它将预处理器输出格式化为(希望)可读的 html 文档:由于预处理器而不同的行在文件中标记。

If using CLion by Jetbrains, you can use the action "clangd: Preprocess current TU"如果使用 Jetbrains 的 CLion,您可以使用操作“clangd: Preprocess current TU”

So hit shift shift and start typing clangd...所以点击shift shift并开始输入clangd ...

动作弹出

Best assign it to a shortcut for simpler reuse in preferences->keymap:最好将它分配给一个快捷方式,以便在首选项-> 键盘映射中更简单地重用:

在此处输入图片说明

Shout out to marcosbento马科斯本托大喊大叫

PS: TU means 'translation unit' (see here LLVM translation unit ) PS:TU 表示“翻译单元”(请参阅​​此处的LLVM 翻译单元

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM