如何显示忽略GCC包含的“预处理"代码 [英] How to show 'preprocessed' code ignoring includes with GCC

查看:107
本文介绍了如何显示忽略GCC包含的“预处理"代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用gcc输出预处理"代码,但忽略"(不扩展)包括:

I'd like to know if it's possible to output 'preprocessed' code wit gcc but 'ignoring' (not expanding) includes:

ES我有这个主要提示:

ES I got this main:

#include <stdio.h>
#define prn(s) printf("this is a macro for printing a string: %s\n", s);

int int(){
char str[5] = "test"; 
prn(str);
return 0;
}

我运行gcc -E main -o out.c

我得到了:

/*
all stdio stuff
*/

int int(){
char str[5] = "test";
printf("this is a macro for printing a string: %s\n", str);
return 0;
}

我只想输出:

#include <stdio.h>
int int(){
char str[5] = "test";
printf("this is a macro for printing a string: %s\n", str);
return 0;
}

或者至少

int int(){
char str[5] = "test";
printf("this is a macro for printing a string: %s\n", str);
return 0;
}

PS:如果可以扩展本地" ""包含而不扩展全局" <>包含

PS: would be great if possible to expand "local" "" includes and not to expand "global" <> includes

推荐答案

我同意Matteo Italia的评论,即如果仅阻止扩展#include指令,那么生成的代码将不代表编译器实际看到的内容,因此在故障排除中将很少使用.

I agree with Matteo Italia's comment that if you just prevent the #include directives from being expanded, then the resulting code won't represent what the compiler actually sees, and therefore it will be of limited use in troubleshooting.

这是解决这个问题的想法.在包含之前和之后添加变量声明.任何合理唯一的变量都可以.

Here's an idea to get around that. Add a variable declaration before and after your includes. Any variable that is reasonably unique will do.

int begin_includes_tag;
#include <stdio.h>
... other includes
int end_includes_tag;

那么你可以做:

> gcc -E main -o out.c | sed '/begin_includes_tag/,/end_includes_tag/d'

sed命令将删除这些变量声明之间的所有内容.

The sed command will delete everything between those variable declarations.

这篇关于如何显示忽略GCC包含的“预处理"代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆