Skip to content
Advertisement

‘DT_REG Undeclared’ even when using 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?

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement