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

Mat*_*ell 4 c macos posix compilation clang

由于某些原因,当我编译我的代码时,编译器找不到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))) {
            ^
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能删除这些警告?我包括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
Run Code Online (Sandbox Code Playgroud)

谢谢.

R..*_*R.. 6

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

  • 其实并不是.`-std = gnu99`在编译器级别进行了很多更改,导致它不符合C99 - 比如打开导致错误结果的数学"优化".我的观点是你应该总是使用`-std = c99`或`-std = c11`以及你想要的库特性的相应特征测试宏,例如`-D_GNU_SOURCE`,如果你想要所有的GNU库扩展. (3认同)