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

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

我有一个程序,是我从原始暗网(深度学习图像识别,Yolov2)的许多地方修改而成的。几个月前我一直在使用它,但今天当我编译它时,它给了我一个错误:

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'

我使用 stat.h 检查目录是否存在,如果不存在,则创建它。此错误出现在 #include 行和 stat.h 文件内。我查看了 stat.h,但看不出哪里出了问题。 stat.h 看起来像这样(我显示了哪一个是第 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只是增加了一个关于throw的属性。问题是什么? (在 CentOS 6.9 上使用 gcc 4.4.7)

一般来说,系统 header 必须包含在代码的任何函数之外。 C11 标准说 §7.1.2 Standard headers(强调):

¶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.

我没有在 POSIX 中找到等效的措辞,但您应该假定适用类似的规则。

鉴于提到 'nested functions' 的错误消息,您可能试图从您的一个函数的范围内包含 #include <sys/stat.h>,并且鉴于 header定义了一些内联函数,你不小心试图将它们定义为嵌套函数,这通常是不允许的(虽然 GCC 对嵌套函数有一些支持,但你可能应该认为这是不可移植的)。