简体   繁体   中英

'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) "

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

Any ideas for the reason?

DT_REG is not part of ISO C11 extensions. Setting -std=c11 strictly enables only features defined in C11 standard.

You can use feature macros to enable additional extensions. As readdir manual mentions, you need _DEFAULT_SOURCE macro to enable file type constants.

You can do this in the source code before including 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

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