0

我正在尝试测试 gcc 预处理器的宏扩展。

我编写以下代码:(仅用于测试)

#include <stdio.h>

#define QUOTE "
#define TMPL hello

int main(){
    printf(QUOTE TMPL QUOTE);
    return 0;
}

编译结果为:

$ gcc main.c -o main
main.c:3:15: warning: missing terminating " character
main.c: In function ‘main’:
main.c:7: error: missing terminating " character
main.c:7: error: missing terminating " character
main.c:7: error: ‘hello’ undeclared (first use in this function)
main.c:7: error: (Each undeclared identifier is reported only once
main.c:7: error: for each function it appears in.)
main.c:7: warning: format not a string literal and no format arguments
main.c:7: warning: format not a string literal and no format arguments
$ 

然后我试着看看预处理的结果

$ gcc -E main.c -o tmp.c
main.c:3:15: warning: missing terminating " character
$ 

尽管给出了警告,但它以某种方式生成了正确的预处理代码tmp.c

int main(){
 printf(" hello ");
 return 0;
}

而我的编译器tmp.chello是正确打印的。

我想知道为什么gcc -E可以产生正确的代码,而gcc直接使用编译失败。gcc预处理器的两种方法有区别吗?

$ gcc --version
i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
4

1 回答 1

2

正如我所评论的,预处理器宏应该扩展为一系列词法分析器标记。使用 GCC 源代码,libcpp(负责预处理和标记化)正在生成标记流(不是纯字符)。最近的 GCC 4.8,gcc -Wall endyul.c -o endyl在您的示例中运行时,提供了非常有用的诊断:

endyul.c:3:15: warning: missing terminating " character [enabled by default]
 #define QUOTE "
               ^
endyul.c: In function 'main':
endyul.c:7:5: error: missing terminating " character
     printf(QUOTE TMPL QUOTE);
     ^
endyul.c:7:5: error: missing terminating " character
endyul.c:4:14: error: 'hello' undeclared (first use in this function)
 #define TMPL hello
              ^
endyul.c:7:18: note: in expansion of macro 'TMPL'
     printf(QUOTE TMPL QUOTE);
                  ^
endyul.c:4:14: note: each undeclared identifier is reported only once for each function it appears in
 #define TMPL hello
              ^
endyul.c:7:18: note: in expansion of macro 'TMPL'
     printf(QUOTE TMPL QUOTE);
                 ^

你的 GCC 4.2 已经很老了。你应该考虑升级它。

并且clang(3.3)也给出了一个很好的诊断:

 clang -Wall endyul.c -o endyul   
 endyul.c:3:15: warning: missing terminating '"' character [-Winvalid-pp-token]
 #define QUOTE "
               ^
 endyul.c:7:12: error: expected expression
     printf(QUOTE TMPL QUOTE);
            ^
 endyul.c:3:15: note: expanded from macro 'QUOTE'
 1 warning and 1 error generated.

再次阅读GCC 的 CPP 手册,特别是关于字符串化连接的章节。

于 2013-06-28T05:00:12.913 回答