简体   繁体   中英

sys/stat.h:456: error: nested function 'stat' declared 'extern'

I have a program which I made by modifing many places from original darknet (deep learning image recognition, Yolov2). I've been using it until several months ago, but today when I compile it, it gave me an error below :

gcc  -DSAVE_LAYER_INPUTS -DSAVE_INPUTS_LAYER_START=31 -DSAVE_INPUTS_LAYER_END=31 -DPRINT_INOUT -Wall -Wfatal-errors  -O3 -ffast-math -c ./src/convolutional_layer.c -o obj/convolutional_layer.o
In file included from ./src/convolutional_layer.c:463:
/usr/include/sys/stat.h: In function 'forward_convolutional_layer':
/usr/include/sys/stat.h:456: error: nested function 'stat' declared 'extern'

I used stat.h to check if a directory exists and if not, to make it. This error comes at the line #include and inside stat.h file. I looked into stat.h, but can't tell what's wrong. stat.h looks like this (I showed which one is line 456.)

#if defined __GNUC__ && __GNUC__ >= 2 && defined __USE_EXTERN_INLINES
/* Inlined versions of the real stat and mknod functions.  */

__extern_inline int
__NTH (stat (__const char *__path, struct stat *__statbuf))
{     // <=== line 456
  return __xstat (_STAT_VER, __path, __statbuf);
}

__NTH is just adding an attribute about throw. What is the problem? (using gcc 4.4.7 on CentOS 6.9)

In general, system headers have to be included outside of any functions in your code. The C11 standard says §7.1.2 Standard headers (emphasis added):

¶4 Standard headers may be included in any order; each may be included more than once in a given scope, with no effect different from being included only once, except that the effect of including <assert.h> depends on the definition of NDEBUG (see §7.2). If used, a header shall be included outside of any external declaration or definition, and it shall first be included before the first reference to any of the functions or objects it declares, or to any of the types or macros it defines. However, if an identifier is declared or defined in more than one header, the second and subsequent associated headers may be included after the initial reference to the identifier. The program shall not have any macros with names lexically identical to keywords currently defined prior to the inclusion of the header or when any macro defined in the header is expanded.

I've not found the equivalent wording in POSIX, but you should assume that similar rules apply.

Given the error message mentioning 'nested functions', it is likely that you're trying to include #include <sys/stat.h> from inside the scope of one of your functions, and given that the header defines some inline functions, you are accidentally trying to define those as nested functions, which is not allowed in general (though GCC has some support for nested function, but you should probably regard that as unportable).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM