opendir 是 POSIX 标准 C 库函数,不是 Ubuntu 特有的接口,因此不随 Ubuntu 版本变化,而是取决于 系统 C 库(glibc)和内核支持。
下面分点说明在 Ubuntu 上的情况。
opendir 是什么?opendir 用于打开一个目录流,定义在:
#include <dirent.h>
DIR *opendir(const char *name);
它属于 POSIX.1-2001 / POSIX.1-2008 标准。
opendir 的支持情况以下 Ubuntu 版本 全部支持 opendir,没有任何例外:
| Ubuntu 版本 | glibc 版本 | 支持情况 |
|---|---|---|
| Ubuntu 14.04 | 2.19 | ✅ |
| Ubuntu 16.04 | 2.23 | ✅ |
| Ubuntu 18.04 | 2.27 | ✅ |
| Ubuntu 20.04 | 2.31 | ✅ |
| Ubuntu 22.04 | 2.35 | ✅ |
| Ubuntu 24.04 | 2.39 | ✅ |
| Ubuntu 24.10 + | 2.40+ | ✅ |
✅ 从最早的 Ubuntu 到最新版本全部支持
opendir 由 glibc 提供,只要:
gcc / clang<dirent.h>就可以直接使用。
这些函数通常与 opendir 一起使用,也全部支持:
DIR *opendir(const char *name);
struct dirent *readdir(DIR *dirp);
int closedir(DIR *dirp);
此外:
| 函数 | 说明 | 支持情况 |
|---|---|---|
readdir_r |
已废弃 | ⚠️ 不推荐使用 |
fdopendir |
POSIX.1-2008 | ✅ 18.04+ 强烈推荐 |
dirfd |
获取目录 fd | ✅ |
| 平台 | 支持 |
|---|---|
| Linux (Ubuntu) | ✅ |
| macOS | ✅ |
| FreeBSD | ✅ |
| Windows (MinGW/MSYS) | ⚠️ 需 POSIX 模拟层 |
| Windows (原生) | ❌(需 FindFirstFile 等) |
#include <stdio.h>
#include <dirent.h>
int main() {
DIR *dir = opendir(".");
if (!dir) return 1;
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}
closedir(dir);
return 0;
}
编译:
gcc test.c -o test
opendir如果你关心的是 某个特定 Ubuntu 版本、交叉编译、嵌入式 glibc、或 Windows 替代方案,可以告诉我具体场景。