使用嵌套函数编译c文件时出错

There is an error when compiling a c file used a nested function

我知道 gcc 支持嵌套函数作为扩展,但我编译一个使用嵌套函数的 c 文件时出现错误。 错误信息是这样的:

test.cpp:6:40: error: function definition is not allowed here double

square (double z) { return z * z; }

嵌套函数是这样的:

foo (double a, double b)
 {
  double square (double z) { return z * z; }
  return square (a) + square (b);
 }

我的操作系统是MacOs,版本是10.12.6

当我在iTerm2中输入gcc -v时,响应是这样的:

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.0.0 (clang-900.0.38)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

您实际上使用的是Clang,而不是gcc

注意这个

Apple LLVM version 9.0.0 (clang-900.0.38)

来自 the clang websiteClang 与 GCC(GNU 编译器集合)

GCC supports many language extensions, some of which are not implemented by Clang. For instance, in C mode, GCC supports nested functions and has an extension allowing VLAs in structs.