GCC/CLANG 扩展宏并调试


 

 

 

${CC} $(INC) -E demo.c > demo.i
${CC} $(INC) -E -P demo.c > demo.i
${CC} $(INC) -E demo.c | sed '/^\#/d' | indent -st -i2 > demo-e.c
${CC} $(INC) -E -P -C demo.c | indent -st -i2 > code-x.c
${CC} $(INC) -E -x c -P -C -traditional-cpp demo.c > demo-e.c
${CC} $(INC) -E -x c -C demo.c > demo-e.c

 

https://stackoverflow.com/questions/4900870/can-gcc-output-c-code-after-preprocessing

https://stackoverflow.com/questions/277258/how-do-i-see-a-c-c-source-file-after-preprocessing-in-visual-studio

 

 

https://jkorpela.fi/html/cpre.html

 

The options (switches) you need to give in such a case depend on the C compiler. The following instructions apply to the Gnu C compiler (gcc). For other compilers, the options could be similar, but please check the applicable manuals.

gcc options when using a preprocessor for non-C files
optioneffect
-E preprocessing only
-x c interpret files as C source files (instead of treating them as object files); this option is given to make the compiler preprocess them
-P don't generate #line directives (which would of course mess things up in HTML documents!)
-C do not ignore comments (since an HTML document might contain data which would be a comment in C)

When these options are used, gcc writes the preprocessed data (e.g. with #include directives replaced by the contents of the files referred to) to standard output. Thus, assuming you have a document demo.htm which is an HTML document except for the use of #include directives, you can generate an HTML document demo.html from it with the command
gcc -E -x c -P -C demo.htm >demo.html

 

 

 

https://www.howtoforge.com/tutorial/uncommon-but-useful-gcc-command-line-options/

在每个编译阶段查看中间代码的输出

你知道在通过 gcc 编译 c 语言代码的时候大体上共分为四个阶段吗?分别为预处理 -> 编译 -> 汇编 -> 链接。在每个阶段之后,gcc 都会产生一个将移交给下一个阶段的临时输出文件。但是生成的都是临时文件,因此我们并不能看到它们——我们所看到的只是我们发起编译命令,然后它生成的我们可以直接运行的二进制文件或可执行文件。

但是比如说在预处理阶段,如果调试时需要查看代码是如何进行处理的,你要怎么做呢?好消息是 gcc 编译器提供了相应的命令行选项,你可以在标准编译命令中使用这些选项获得原本被编译器删除的中间文件。我们所说的选项就是-save-temps

以下是 gcc 手册中对该选项的介绍:

永久存储临时的中间文件,将它们放在当前的文件夹下并根据源文件名称为其命名。因此,用 -c -save-temps 命令编译 foo.c 文件时会生成 foo.i foo.s 和 foo.o 文件。即使现在编译器大多使用的是集成的预处理器,这命令也会生成预处理输出文件 foo.i。

当与 -x 命令行选项结合使用时,-save-temps 命令会避免覆写与中间文件有着相同扩展名的输入源文件。相应的中间文件可以通过在使用 -save-temps 命令之前重命名源文件获得。

以下是怎样使用这个选项的例子:

  1. gcc -Wall -save-temps test.c -o test-exec

下图为该命令的执行结果,验证其确实产生了中间文件:

因此,在截图中你所看到的 test.i、test.s、 test.o 文件都是由 -save-temps 选项产生的。这些文件分别对应于预处理、编译和链接阶段。

 

posted @ 2022-01-19 13:15  sinferwu  阅读(225)  评论(0编辑  收藏  举报