我可以使cpp不输出#pragma上的空格吗?

时间:2019-01-17 14:51:14

标签: gcc c-preprocessor pragma

我正在用~/.bashrc#pragma push_macro在C预处理器中编写图灵机。我想让它在预处理完成后直接输出结果,而不是输出仍然需要运行以获得结果的仅打印程序。

部分源代码:

#pragma pop_macro

运行// main.c int printf(char*, ...); int main() { #define CODEFILE "command.c" #include "turing.h" #undef CODEFILE } // turing.h #ifndef PARSE #define PARSE(x) x #pragma push_macro("PARSE") #undef PARSE #pragma pop_macro("PARSE") #ifndef PARSE #error push_macro/pop_macro not supported! #else #define GET03(x,y,z) x #define GET13(x,y,z) y #define GET23(x,y,z) z #define GETN(n,l) PARSE(GET##n l) #define COMMAND GETN(03, MOVE) #define TNEXT GETN(13, MOVE) #define FNEXT GETN(23, MOVE) #define LEFT SPECIAL(1) #define RIGHT SPECIAL(2) #define NOP SPECIAL(3) #define WARN SPECIAL(4) #define HALT SPECIAL(5) #define DEPTH 0 #pragma push_macro("DEPTH") #undef DEPTH #define DEPTH 1 #endif #endif # # #ifdef PARSE #define STATUS 0 #define PTRL 0 #define PTR 0 # #include "infloop.h" # #include "clrmemlp.h" # #pragma pop_macro("DEPTH") #undef FINISHED #undef STATUS #undef PTRL #undef PTR # #endif 将得到:(某些行的末尾包含空格)

cpp main.c

这里的行号很烦人。然后我在# 1 "main.c" # 1 "<built-in>" # 1 "<command-line>" # 1 "main.c" int printf(char*, ...); int main() { # 1 "turing.h" 1 # 22 "turing.h" # 34 "turing.h" # 1 "infloop.h" 1 ... 文档中看到了

--help

使用此标志我可以得到

  -P                          Do not generate #line directives

但是,仍然有大量的空行。看来它们来自int printf(char*, ...); int main() { printf ("x = %d, ", 1); ... ,而且我还测试了:

#pragma

结果是

1
#pragma push_macro("x")
2
#define x 3
3
#undef x
4
#include "nul"
5

所以我很确定空行来自1 2 3 4 5 。有什么办法可以避免它们?

2 个答案:

答案 0 :(得分:0)

是的,您是对的#pragma指令扩展到新行。似乎gcc没有控制此行为的选项。

一个最小的例子是:

// turing.h
#pragma push_macro("Test")

// main.c
int main() {
#include "turing.h"
}

输出为:

int main() {

}

还要看一下,编译器无法理解的#pragma指令会随其扩展。

答案 1 :(得分:0)

您始终可以通过过滤器提供输出:

gcc -E -P main.c | grep '[^ ]'

grep选择具有某些字符而不是空格的所有行;换句话说,它将删除所有空行和仅由空格组成的行。使用GNU grep,您可以使用模式\S,该模式也将忽略仅包含空格和制表符的行。