DT_DIR undefined

11,632

You need to have the _BSD_SOURCE feature test macro defined to get those defines, they are not standard, and GCC does not define that macro when compiling for C99.

gcc -std=c99 -D_BSD_SOURCE -Wall a.c
Share:
11,632
RiaD
Author by

RiaD

Updated on June 04, 2022

Comments

  • RiaD
    RiaD almost 2 years

    I want to check if file returned by readdir is directory. I tried do it using DT_DIR constant (as man readdir says) but it's undefined. What file should I include to get it?

    Now I use

    #include <sys/types.h>
    #include <dirent.h>
    #include <stdlib.h>
    #include <errno.h>
    

    gcc version is 4.6.1

    Compilation string:

    gcc a.c --std=c99 -Wall
    
  • RiaD
    RiaD about 12 years
    Hm, thanks it works. But I guess it isn't always OK. to change this constant, because it may change something else. Is it way to do it other way? Are my own constants OK(i really don't think so) or they may be changed on other OS? (i.e 4 for me with directories)
  • RiaD
    RiaD about 12 years
    PS: -D_BSD_SOURCE is enough, we needn't value here
  • Mat
    Mat about 12 years
    Using -D_BSD_SOURCE doesn't change any constants. It pulls in additional defines and functions that come from BSD and are not POSIX or standard C. If your code requires that, then it is not portable. If you want it to be portable, don't rely on the d_type field of struct dirent, use stat instead. Hardcoding 4 is a bad idea.