scandir not being included from dirent.h

95 views Asked by At
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <dirent.h>

int main(int argc, char const *argv[]){
    struct dirent **namelist;

    int n;
    n = scandir(".", &namelist, NULL, alphasort);

    printf("Number Files %d\n", n);

    if (n == -1){
        perror("scandir");
        exit(EXIT_FAILURE);
    }

    while (n--){
        printf("%s\n", namelist[n]->d_name);
        free(namelist[n]);
    }
}

I get an error for alphasort, "error: 'alphasort' undeclared (first use in this function)" and I get an implicit declaration of function 'scandir' error. Other programs successfully compile and include libraries.

I have made sure I have the dirent.h library on my computer. The VSCode quick fix is to edit include path settings but even when I add the path to C:\MinGW\x86_64-w64-mingw32\include which contains the library it does not compile.

1

There are 1 answers

0
HolyBlackCat On

MinGW doesn't appear to support scandir. It doesn't try provide POSIX APIs in general, the fact that opendir is provided is already a bit surprising.