简体   繁体   English

&#39;DT_REG Undeclared&#39; 即使在使用时<dirent.h>函数中的头文件

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

I'm using the <dirent.h> header file in the function I'm referencing DT_REG, however, I'm getting error an saying " 'DT_REG' undeclared (first use in this function) "我在引用 DT_REG 的函数中使用了 <dirent.h> 头文件,但是,我收到错误消息“'DT_REG' undeclared (first use in this function)”

The snippet of the code is:代码片段是:

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

In my makefile I'm using "cc -std=c11 -Wall -Werror -pedantic".在我的 makefile 中,我使用了“cc -std=c11 -Wall -Werror -pedantic”。

Any ideas for the reason?有什么想法吗?

DT_REG is not part of ISO C11 extensions. DT_REG不是 ISO C11 扩展的一部分。 Setting -std=c11 strictly enables only features defined in C11 standard.设置-std=c11严格仅启用 C11 标准中定义的功能。

You can use feature macros to enable additional extensions.您可以使用功能宏来启用其他扩展。 As readdir manual mentions, you need _DEFAULT_SOURCE macro to enable file type constants.正如readdir 手册所述,您需要_DEFAULT_SOURCE宏来启用文件类型常量。

You can do this in the source code before including dirent.h您可以在包含dirent.h之前在源代码中执行此操作

#define _DEFAULT_SOURCE
#include <dirent.h>

or via command line as a compiler option或通过命令行作为编译器选项

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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