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

时间:2017-12-21 09:26:57

标签: c gcc clang nested-function

我知道嵌套函数作为gcc中的扩展支持,但我编译了一个带有错误的嵌套函数的c文件。 错误按摩是这样的:

  

test.cpp:6:40:错误:此处不允许使用函数定义double

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

嵌套函数是这样的:

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

我的操作系统是Mac Os,版本是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

1 个答案:

答案 0 :(得分:0)

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

注意这个

  

Apple LLVM version 9.0.0 (clang-900.0.38)

来自the clang website Clang vs GCC(GNU Compiler Collection)

  

GCC支持许多语言扩展,其中一些不是由Clang实现的。例如,在C模式下,GCC支持嵌套函数,并且具有允许在结构中使用VLA的扩展。

相关问题