SCANDIR隐含声明; alphasort是未申报 [英] Implicit declaration of scandir; alphasort is undeclared

查看:1387
本文介绍了SCANDIR隐含声明; alphasort是未申报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use scandir to print a list of files in the current directory. When I try to compile, I am receiving the following errors and warnings:

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

我包括< dirent.h> ,其中据我所知应该定义 SCANDIR()和所有相关的功能。而且我没有看到我的code任何错误:

I am including <dirent.h>, which to my knowledge should define scandir() and all related functions. And I don't see any errors in my code:

#include <dirent.h>
...
int printFiles(){
    struct dirent **nameList;
    int numOfFiles = scandir(".", &nameList, 0, alphasort);

    //TODO print file names
    return numOfFiles;
}
....

我运行Ubuntu 12.04,和我使用 GCC -c99 标志进行编译。

我,我只是忽视的东西?我想不通为什么它不能编译。

Am I simply overlooking something? I can't figure out why it's failing to compile.

推荐答案

如果您使用 -std = C99 中,只有函数是严格C99标准的一部分,是由标题文件包括在内。 SCANDIR()是不是在C99标准。因此,你必须设置一个preprocessor变量,以确保函数原型包括在内。例如,手册页 SCANDIR()表示设置 _BSD_SOURCE _SVID_SOURCE preprocessor变量之前你做的的#include 将解决这个问题。或者,你可以使用的#define _GNU_SOURCE 这将反过来为您设置很多不同的变量(包括 _BSD_SOURCE _SVID_SOURCE )。

If you use -std=c99, only functions that are strictly a part of the C99 standard are included by the header files. scandir() is not in the C99 standard. Therefore, you have to set a preprocessor variable to ensure that the function prototype is included. For example, the man page for scandir() indicates that setting the _BSD_SOURCE or _SVID_SOURCE preprocessor variables before you do the #include will fix the problem. Or, you can use #define _GNU_SOURCE which will in turn set quite a few different variables for you (including _BSD_SOURCE and _SVID_SOURCE).

您code仍然具有警示和工作编译,因为C允许你使用隐式定义的函数来编译和链接器将正确链接)调用 SCANDIR(的正常功能。

Your code will still compile with the warning and work because C allows you to compile with implicitly defined functions, and the linker will correctly link the call to scandir() to the proper function.

这篇关于SCANDIR隐含声明; alphasort是未申报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆