compiling nested functions with clang versus gcc

2019-03-01 06:46发布

问题:

I have a c file that I can compile with no problem using GCC like below:

gcc foo.c

however using the same file I am receiving error of having defined functions inside main using 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

these instances of errors are the definitions of a new function inside the main section of the code. I want to know why GCC doesn't get bothered with this yet clang does?

回答1:

Functions defined within functions are an extension to the C language, implemented by gcc. This is enabled by default. If you make gcc a Standard C compiler, as with -ansi -pedantic or -std=C99 or similar, it will also complain about nested function definitions:

x.c: In function ‘main’:
x.c:8:5: warning: ISO C forbids nested functions [-Wpedantic]
     int nested(void)
     ^


标签: gcc clang llvm