警告: 在 C99 中,隐式声明函数 'fsync' 是无效的

4

由于某种原因,当我编译我的代码时,编译器找不到fsync和truncate的原型。我收到以下消息:

cc -c -Wall -Wno-overflow  -pedantic -std=c99 -I/Users/matt/Programming/BitEagle_Projects/cbitcoin/include -I/usr/local/ssl/include -I/opt/local/include -O3 -arch i386 -arch x86_64 -D_POSIX_SOURCE -fPIC dependencies/storage/CBBlockChainStorage.c -o build/CBBlockChainStorage.o
dependencies/storage/CBBlockChainStorage.c:154:6: warning: implicit declaration of function 'fsync'
      is invalid in C99 [-Wimplicit-function-declaration]
                if(fsync(indexAppend)){
                   ^
dependencies/storage/CBBlockChainStorage.c:649:6: warning: implicit declaration of function
      'truncate' is invalid in C99 [-Wimplicit-function-declaration]
        if (truncate(filename, CBArrayToInt32(data, 0))) {
            ^

我该怎么做才能消除这些警告?我已经包含了unistd.h头文件。这是在使用clang编译的OSX系统上:

$ cc --version
Apple clang version 4.0 (tags/Apple/clang-421.0.60) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin12.2.0
Thread model: posix

谢谢你。

感谢您。


3
你包含了哪些头文件? - user529758
你的代码中是否包含了对应的头文件?Linux 中 unistd.h 中有一个名为 fsync 的原型。 - Asblarf
1个回答

6

如果您正在使用-std=c99,系统头文件将尝试提供与C标准兼容的声明/宏名称空间,其中不包括来自POSIX或其他标准或非标准扩展的任何附加函数。您需要定义相应的功能测试宏才能获取它们。例如,在命令行上输入-D_POSIX_C_SOURCE=200809L或在源文件中定义它。


如果您正在使用POSIX扩展,应该使用-std=gnu99 - benjarobin
1
@benjarobin,除非您不想启用GNU扩展。使用其他函数是另一回事。 - Daniel Fischer
3
实际上不是这样的。-std=gnu99 在编译器层面做了很多改动,导致它不符合 C99 标准——例如打开数学“优化”功能会导致错误结果。我认为您应该始终使用 -std=c99-std=c11 并使用适当的特性测试宏来获取所需的库功能,例如如果需要所有 GNU 库扩展,则使用 -D_GNU_SOURCE - R.. GitHub STOP HELPING ICE
_POSIX_SOURCE已被弃用,只能请求1990年代原始版本的POSIX,该版本可能缺少fsync。 - R.. GitHub STOP HELPING ICE

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接