"Nested functions are supported as an extension in GNU C" 中的 "extension" 是怎么回事?

What is the deal about "extension" in "Nested functions are supported as an extension in GNU C"?

online manual中,我可以看到,

Nested functions are supported as an extension in GNU C, but are not supported by GNU C++.

这是什么意思?我如何获得该扩展?

您不需要获取 扩展名。这是 GNU C 编译器中嵌入的功能。

FWIW,这里的 extension 指向 C 标准的扩展。详细说明,来自online manual

6 . Extensions

GNU C provides several language features not found in ISO standard C. (The -pedantic option directs GCC to print a warning message if any of these features is used.) To test for the availability of these features in conditional compilation, check for a predefined macro GNUC, which is always defined under GCC.

每个 C 编译器都支持该语言的一个或多个版本,可以是标准版本,也可以是非官方版本。例如,GCC 支持标准的三个版本,参见 https://gcc.gnu.org/onlinedocs/gcc/Standards.html#Standards

此外,GCC supports so-called extensions 该语言的其他功能可能是为了方便开发人员而存在的,或者是为了测试可能使其进入官方 C 标准的更高版本的实验性功能。默认情况下启用这些扩展,因此无需安装额外的包,它们只是编译器本身不可或缺的一部分。

这些扩展应该主要只在特定编译器是项目唯一受支持的目标时才使用。开发人员通常优先考虑可移植性和符合标准,以最大限度地提高代码库的可维护性。

关于这个特定的特性,重要的是要注意 C++11 添加了对 lambda 函数的支持,这可能证明 G++ 中不支持这个扩展是合理的,因为它们会发生冲突而没有任何额外的好处。

1st. 你说的link是针对当前GCC开发版的。此时它是 v6.0.0。我认为你并没有真正使用它。您很可能是 运行 某个 v4 或 v5 编译器。尝试 gcc -v 检查。

2nd. GCC has a few command line options 控制您愿意使用的确切 "dialect"。在特定情况下,嵌套函数不在任何当前标准 (AFAIK) 中,因此您需要确保您使用的是 select GNU 方言。默认的称为 gnu89,即“ISO C90(包括一些 C99 功能)”。选项 -std=gnu11 应该 select "the most advanced dialect"™。不过,强烈建议完整阅读文档。

3rd.一旦你知道你是哪个版本运行你可以去相关的在线文档并检查“Extensions to the C Language Family”(我link以 v4.9.3 为例)该扩展是否可用以及实际情况。

我会说,就是这样。