'DT_REG Undeclared' 即使在函数中使用 <dirent.h> 头文件

'DT_REG Undeclared' even when using <dirent.h> header file in function

我在我引用的函数中使用了 头文件 DT_REG,但是,我收到错误提示“'DT_REG' 未声明(首先在此函数中使用)“

代码片段是:

  DIR * dirp;
  struct dirent * entry;
  dirp = opendir(path);
  if(entry->d_type == DT_REG) { //.... }

在我的 makefile 中,我使用的是“cc -std=c11 -Wall -Werror -pedantic”。

有什么想法吗?

DT_REG 不是 ISO C11 扩展的一部分。设置 -std=c11 严格仅启用 C11 标准中定义的功能。

您可以使用 feature macros to enable additional extensions. As readdir manual 提及,您需要 _DEFAULT_SOURCE 宏来启用文件类型常量。

您可以在包含 dirent.h

之前在源代码中执行此操作
#define _DEFAULT_SOURCE
#include <dirent.h>

或通过命令行作为编译器选项

cc -std=c11 -D_DEFAULT_SOURCE -Wall -Werror -pedantic