在 Debian(以及一般的 Linux/Unix 系统)中使用 opendir() 函数,是需要包含头文件的。
正确用法是:
#include <dirent.h>
#include <sys/types.h> // 通常也需要,定义 DIR、ino_t 等类型
示例:
#include <dirent.h>
#include <stdio.h>
int main() {
DIR *dir = opendir(".");
if (dir == NULL) {
perror("opendir");
return 1;
}
closedir(dir);
return 0;
}
编译:
gcc test.c -o test
总结:
<dirent.h><sys/types.h> 建议包含(很多实现已间接包含,但写上是好习惯)如果你是在写 Makefile、交叉编译,或者遇到具体报错,也可以贴出来我帮你看。