scandir的隐含声明; alphasort未声明

我正在尝试使用scandir打印当前目录中的文件列表。 当我尝试编译时,我收到以下错误和警告:

 warning: implicit declaration of function 'scandir' [-Wimplicit-function-declaration] error: 'alphasort' undeclared (first use in this function) note: each undeclared identifier is reported only once for each function it appears in 

我包括 ,据我所知,应该定义scandir()和所有相关的函数。 我在代码中看不到任何错误:

 #include  ... int printFiles(){ struct dirent **nameList; int numOfFiles = scandir(".", &nameList, 0, alphasort); //TODO print file names return numOfFiles; } .... 

我正在运行Ubuntu 12.04,我正在使用带有-c99标志的gcc进行编译。

我只是忽略了什么? 我无法弄清楚为什么它无法编译。

如果使用-std=c99 ,则头文件仅包含严格属于C99标准的函数。 scandir()不符合C99标准。 因此,您必须设置预处理器变量以确保包含函数原型。 例如, scandir()的手册页指示在执行#include之前设置_BSD_SOURCE_SVID_SOURCE预处理器变量将解决问题。 或者,您可以使用#define _GNU_SOURCE ,它将为您设置相当多的不同变量(包括_BSD_SOURCE_SVID_SOURCE )。

您的代码仍将使用警告编译并正常工作,因为C允许您使用隐式定义的函数进行编译,并且链接器将正确地将对scandir()的调用链接到正确的函数。

您使用的宏由您自己计算机上的dirent.h中的宏决定。它位于/usr/include/dirent.h中。

  1. 在dirent.h中找到scandir,你会发现制作scandir Invisible的宏。例如在我的conmputer中,它是“__USE_BSD,__ USE_MISC”。

     #if defined __USE_BSD || defined __USE_MISC /* Return the file descriptor used by DIRP. */ extern int dirfd (DIR *__dirp) __THROW __nonnull ((1)); # if defined __OPTIMIZE__ && defined _DIR_dirfd # define dirfd(dirp) _DIR_dirfd (dirp) # endif # ifndef MAXNAMLEN /* Get the definitions of the POSIX.1 limits. */ # include  /* `MAXNAMLEN' is the BSD name for what POSIX calls `NAME_MAX'. */ # ifdef NAME_MAX # define MAXNAMLEN NAME_MAX # else # define MAXNAMLEN 255 # endif # endif # define __need_size_t # include  # ifndef __USE_FILE_OFFSET64 extern int scandir (__const char *__restrict __dir, struct dirent ***__restrict __namelist, int (*__selector) (__const struct dirent *), int (*__cmp) (__const void *, __const void *)) __nonnull ((1, 2)); # else # ifdef __REDIRECT extern int __REDIRECT (scandir, (__const char *__restrict __dir, struct dirent ***__restrict __namelist, int (*__selector) (__const struct dirent *), int (*__cmp) (__const void *, __const void *)), scandir64) __nonnull ((1, 2)); # else # define scandir scandir64 # endif # endif 

    你知道现在该怎么办吗? 别担心!这不是直接#define这个宏的好方法。

  2. 打开文件features.h(位于/usr/include/features.h),搜索“__USE_MISC”里面,你可以看到下面的代码:

     #if defined _BSD_SOURCE || defined _SVID_SOURCE # define __USE_MISC 1 #endif 

    它告诉我们如果定义了“_BSD_SOURCE”或“_SVID_SOURCE”,那么__USE_MISC将被定义为auto。

  3. 考虑你自己的代码的兼容性,在你的文件的开头(在任何语句,如#include < .h> ||“ .h”之前)添加语句(任何一个或两个),这将使用scandir()。

     #define _BSD_SOURCE 1 #define _SVID_SOURCE 1 
  4. 保存您的文件并制作。

尝试#include 文件使用scandir并定义extern int alphasort(const void*,const void*); 或者extern int alphasort(); 在你的printFiles

还 – 你应该把你的程序与标准库联系起来(希望它已经完成)