使用 clang 与 gcc 编译嵌套函数

compiling nested functions with clang versus gcc

我有一个 c 文件,我可以使用 GCC 毫无问题地编译它,如下所示:

gcc foo.c

但是使用同一个文件我收到错误消息,在 main 中使用 clang:

定义了函数

clang foo.c

foo:230:1: error: function definition is not allowed here
{
^
foo.c:241:1: error: function definition is not allowed here
{
^
foo.c:253:1: error: function definition is not allowed here

这些错误实例是代码主要部分中新函数的定义。我想知道为什么 GCC 不为此烦恼而 clang 呢?

函数内定义的函数是对C语言的扩展,由gcc实现。这是默认启用的。如果你使 gcc 成为标准 C 编译器,如 -ansi -pedantic-std=C99 或类似的,它也会抱怨嵌套函数定义:

x.c: In function ‘main’:
x.c:8:5: warning: ISO C forbids nested functions [-Wpedantic]
     int nested(void)
     ^