2

In the online manual, I can see,

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

What does that mean? How do I get that extension?

sepp2k
  • 363,768
  • 54
  • 674
  • 675
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
  • 1
    Please remember that this particular extension requires the stack to be executable which defeats an important security mechanism and may make your program more prone to exploits. – fuz Dec 22 '15 at 10:30
  • @FUZxxl please site the sources for the said claim. It will helpful for further reading. – Aquarius_Girl Dec 22 '15 at 10:31
  • 1
    gcc uses [trampolines](https://gcc.gnu.org/onlinedocs/gccint/Trampolines.html) to implement pointers to nested functions, which are explained in depth [here](http://stackoverflow.com/a/8179587/417501) and require the stack to be executable. – fuz Dec 22 '15 at 10:37
  • to get this extension, do not use the `-std=cXX` switches – M.M Dec 22 '15 at 10:43

3 Answers3

3

You don't need to get the extension. That is a feature embedded in the GNU C compiler.

FWIW, the extension here point to the extension over the C standard(s). To elaborate, from the 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.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
2

Every C compiler supports one or several versions of the language, which could be standard or unofficial versions. For example, GCC supports three versions of the standard, see https://gcc.gnu.org/onlinedocs/gcc/Standards.html#Standards.

In addition, GCC supports so-called extensions, additional features for the language, that may exist for developer convenience, or for the purpose of testing experimental features that may make it into later versions of the official C standard. These extensions are enabled by default, so there is no additional package to install, they are just an integral part of the compiler itself.

These extensions should mostly only be used if the specific compiler is the only supported target for the project. Developers usually prioritize portability and conformance to the standards to maximize maintainability of a code base.

About this specific feature, it is important to note that C++11 added the support for lambda functions which probably justifies the lack of support of this extension in G++, as they would conflict without any additional benefit.

SirDarius
  • 41,440
  • 8
  • 86
  • 100
  • 1
    “Every C compiler conforms to one or several versions of the language.“—that would be nice if it was true. – fuz Dec 22 '15 at 10:29
  • @FUZxxl at least they try to :) Note that I did not say that they conformed to versions of the **standard**. For example, embedded C compilers may support an unofficial subset of the standard. I still regard this as a *version of the language*. – SirDarius Dec 22 '15 at 10:32
  • @FUZxxl: No, if a compiler supports an extension then users of the compiler will be tempted to write code that only works on that one compiler. (I think Apple removed nested functions from some compiler version, and found a total of eight uses of the feature, all easily replaced with non-nested functions). – gnasher729 Dec 22 '15 at 10:36
  • @SirDarius you can't say *conforms to* if you're not talking about some specification though. – M.M Dec 22 '15 at 10:43
  • @M.M of course you're right, a specification isn't necessarily a standard though. I can rephrase this to be clearer. – SirDarius Dec 22 '15 at 11:11
1

1st. The link you mention is for the current GCC development version. At this moment it's v6.0.0. I think you are not really using that. Very likely you are running some v4 or v5 compiler. Try gcc -v to check.

2nd. GCC has a few command line options that control the exact "dialect" you are willing to use. In the specific case, nested functions are not in any current standard (AFAIK) so you need to ensure you are selecting a GNU dialect. The default one is called gnu89 that is "ISO C90 (including some C99 features)". The option -std=gnu11 should select "the most advanced dialect"™. A complete read of the documentation is highly recommended, though.

3rd. Once you know which version you are running you can go to the relevant online documentation and check in the "Extensions to the C Language Family" (I linked to v4.9.3 as an example) whether that extension is available and what's actually about.

I'd say, that's it.

EnzoR
  • 3,107
  • 2
  • 22
  • 25