【问题标题】:warning: implicit declaration of function 'fsync' is invalid in C99警告:函数“fsync”的隐式声明在 C99 中无效
【发布时间】:2012-12-07 04:30:24
【问题描述】:

由于某种原因,当我编译我的代码时,编译器找不到 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。这是在 OSX 上使用 clang:

$ 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

谢谢。

【问题讨论】:

  • 你包含了哪些头文件?
  • 你的代码中是否包含了相应的头文件?在 Linux 中的 unistd.h 中有一个 fsync 原型。

标签: c macos compilation posix clang


【解决方案1】:

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

【讨论】:

  • 如果你使用 POSIX 扩展,你应该使用-std=gnu99
  • @benjarobin 除非您不想启用 gnu 扩展。使用附加功能是另一回事。
  • 其实不是。 -std=gnu99 在编译器级别进行了很多更改,导致它不符合 C99——比如打开数学“优化”会给出错误的结果。我的观点是您应该始终使用-std=c99-std=c11 以及您想要的库功能的适当功能测试宏,例如-D_GNU_SOURCE 如果你想要所有 GNU 库扩展。
  • _POSIX_SOURCE 已弃用,只能请求 1990 年代原始版本的 POSIX,它可能缺少 fsync。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 2013-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多