c - 如何显示 'preprocessed' 代码忽略包括 GCC

标签 c linux gcc c-preprocessor

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

ES 我得到了这个主要的:

#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:如果可能扩展“本地”会很棒""包括但不展开“全局”<>包括

最佳答案

我同意 Matteo Italia 的评论,如果你只是阻止 #include 指令被扩展,那么生成的代码将不会代表编译器实际看到的内容,因此它会受到限制用于故障排除。

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

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 命令将删除这些变量声明之间的所有内容。

关于c - 如何显示 'preprocessed' 代码忽略包括 GCC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54891248/

相关文章:

linux - 将带有参数的命令重定向到文件

linux - Kubernetes 工作节点 cpu 和内存请求始终保持为零

c - 使用 X11 编译文件时出现问题

c - 动态数组 C 上的无效赋值

c - 从 C 中的 wav 文件中删除暂停

c - 抑制 R 中的 C 警告消息

Mono 上的 C# 用户控制?

c++ - ld64.so 存在于 ldd 中,在运行时丢失

c++ - 什么是类(class)的 VTT?

c - c 中多维数组的指针