我尝试在我的 CPP 程序中使用fallocate()
orposix_fallocate()
函数,但我的编译器一直告诉我,它找不到该函数。所以我创建了这个最小的程序:
#include <fcntl.h>
int main() {
int fd = open("lol", O_RDWR|O_CREAT, 0666);
if (fd < 0) return 1;
return posix_fallocate(fd, 0, 400000);
}
但是当我试图编译它时,我的编译器告诉我:
c++ -g -O2 -Wall -Wextra -std=c++2a test.cc -o test
test.cc:5:12: error: use of undeclared identifier 'posix_fallocate'
return posix_fallocate(fd, 0, 400000);
^
1 error generated.
make: *** [test] Error 1
如您所见,我使用的是 c++20。我用 Catalina 和 clang 在我的 Mac 上试过这个。它在我尝试过的 Linux shell 上运行良好。
为什么会这样,有没有办法fallocate()
在 Mac 上工作?