GCC我有一个 ac 文件,我可以使用如下命令毫无问题地进行编译:
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
Run Code Online (Sandbox Code Playgroud)
这些错误实例是代码主要部分中新函数的定义。我想知道为什么 GCC 不关心这个问题,而 clang 却关心这个?
函数内定义的函数是对 C 语言的扩展,由 gcc 实现。默认情况下启用此功能。如果您使 gcc 成为标准 C 编译器,与-ansi -pedantic或-std=C99类似,它也会抱怨嵌套函数定义:
x.c: In function \xe2\x80\x98main\xe2\x80\x99:\nx.c:8:5: warning: ISO C forbids nested functions [-Wpedantic]\n int nested(void)\n ^\nRun Code Online (Sandbox Code Playgroud)\n