【发布时间】:2019-01-17 14:51:14
【问题描述】:
我正在用 #pragma push_macro 和 #pragma pop_macro 在 C 预处理器中编写图灵机。我想让它在预处理完成后直接输出结果,而不是输出一个仍然需要运行才能得到结果的仅打印程序。
部分源码:
// 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
所以我很确定空行来自#pragma。有什么办法可以避免吗?
【问题讨论】:
标签: gcc c-preprocessor pragma