c - 在 OSX 上使用 TEMP_FAILURE_RETRY

标签 c macos gcc

我有一些使用 TEMP_FAILURE_RETRY 的代码,但无法构建它。我已定义 _GNU_SOURCE 并按此顺序包含 unistd.h

运行gcc -Wall -v archunix5a_2.c -o archunix5a_2 给出以下输出:

Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin16.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
 "/Library/Developer/CommandLineTools/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.12.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name archunix5a_2.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu penryn -target-linker-version 274.2 -v -dwarf-column-info -debugger-tuning=lldb -resource-dir /Library/Developer/CommandLineTools/usr/bin/../lib/clang/8.0.0 -Wall -fdebug-compilation-dir /Users/mateuszsadowski/Documents/programowanie/nauka/C/SOP/4 -ferror-limit 19 -fmessage-length 59 -stack-protector 1 -fblocks -fobjc-runtime=macosx-10.12.0 -fencode-extended-block-signature -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/fp/qqgmt88x247bd712wbbw8wh80000gn/T/archunix5a_2-7cb96f.o -x c archunix5a_2.c
clang -cc1 version 8.0.0 (clang-800.0.42.1) default target x86_64-apple-darwin16.3.0
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /Library/Developer/CommandLineTools/usr/bin/../lib/clang/8.0.0/include
 /Library/Developer/CommandLineTools/usr/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
archunix5a_2.c:113:13: warning: implicit declaration of
      function 'TEMP_FAILURE_RETRY' is invalid in C99
      [-Wimplicit-function-declaration]
        if (TEMP_FAILURE_RETRY(fsync(fd)) == -1)
            ^
1 warning generated.
 "/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.12.0 -o archunix5a_2 /var/folders/fp/qqgmt88x247bd712wbbw8wh80000gn/T/archunix5a_2-7cb96f.o -lSystem /Library/Developer/CommandLineTools/usr/bin/../lib/clang/8.0.0/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
  "_TEMP_FAILURE_RETRY", referenced from:
      _cleanup in archunix5a_2-7cb96f.o
      _main in archunix5a_2-7cb96f.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [archunix5a_2] Error 1

如果有任何帮助,我将不胜感激。

最佳答案

正如我之前所说,mac osx 不支持大多数 glib 库。如果您想使用TEMP_FAILURE_RETRY,您可以尝试以下操作 不要忘记包含 unistd。从这里阅读更多内容

here用这个宏

#include <unistd.h>  // for TEMP_FAILURE_RETRY

#ifndef TEMP_FAILURE_RETRY
#define TEMP_FAILURE_RETRY(exp)            \
  ({                                       \
    decltype(exp) _rc;                     \
    do {                                   \
      _rc = (exp);                         \
    } while (_rc == -1 && errno == EINTR); \
    _rc;                                   \
  })
#endif

我希望这有帮助

关于c - 在 OSX 上使用 TEMP_FAILURE_RETRY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41446716/

相关文章:

objective-c - 从 subview 访问 windowController 的按钮

c++ - 在远程机器上编译的调试过程

c - 为什么我可以在 gcc -std=c11 中使用 gets()?

c++ - 软件浮点和 x87 或 sse 禁用

c - 为什么这个函数被视为委托(delegate)?

c - 如何在c中使用指针反转数组

macos - IntelliJ - 新版本会覆盖吗

gcc - 如何在systemtap中检查用户空间函数的变量?

c - gdb "Program exited normally"当它不应该

c - 为什么我不能在C编程中将char写入二进制文件?