c - Mac OS 上函数 ‘swapcontext’ 的隐式声明

标签 c multithreading macos

我刚刚将我的项目上传到github: https://github.com/gitlwh/whyisthis/tree/master

您可以克隆它并在 Mac 上运行。这很方便。 当我在根文件夹中运行 make 时,它显示:

uthread.c:21:3: warning: implicit declaration of function ‘swapcontext’ [-Wimplicit-function-declaration]
   swapcontext(&currentThread->context,&scheduler_context);
   ^
uthread.c: In function ‘main_manager’:
uthread.c:31:2: warning: implicit declaration of function ‘setcontext’ [-Wimplicit-function-declaration]
  setcontext(&mainThread->context);
  ^
uthread.c: In function ‘initialer’:
uthread.c:72:5: warning: implicit declaration of function ‘getcontext’ [-Wimplicit-function-declaration]
     getcontext(&initThread->context);
     ^
uthread.c: In function ‘THREAD_INIT’:
uthread.c:112:3: warning: implicit declaration of function ‘makecontext’ [-Wimplicit-function-declaration]
   makecontext(&mainThread->context, (void(*)(void)) main_manager, 0, NULL, NULL);
   ^

我在网上查找了很多资料,并用 sys/ucontext.h 替换了 ucontext.h。我实在不知道这是为什么。

更新: 如果我更改为 ucontext.h,它将显示:

uthread.c: In function ‘signal_handler’:
uthread.c:21:3: warning: ‘swapcontext’ is deprecated [-Wdeprecated-declarations]
   swapcontext(&currentThread->context,&scheduler_context);
   ^
In file included from uthread.h:8:0,
                 from uthread.c:1:
/usr/include/ucontext.h:40:6: note: declared here
 int  swapcontext(ucontext_t * __restrict, const ucontext_t * __restrict) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_5, __MAC_10_6, __IPHONE_2_0, __IPHONE_2_0) __WATCHOS_PROHIBITED __TVOS_PROHIBITED;
      ^
uthread.c: In function ‘main_manager’:
uthread.c:31:2: warning: ‘setcontext’ is deprecated [-Wdeprecated-declarations]
  setcontext(&mainThread->context);
  ^
In file included from uthread.h:8:0,
                 from uthread.c:1:
/usr/include/ucontext.h:39:6: note: declared here
 int  setcontext(const ucontext_t *) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_5, __MAC_10_6, __IPHONE_2_0, __IPHONE_2_0) __WATCHOS_PROHIBITED __TVOS_PROHIBITED;
      ^
uthread.c:32:2: warning: ‘setcontext’ is deprecated [-Wdeprecated-declarations]
  setcontext(&scheduler_context);
  ^
In file included from uthread.h:8:0,
                 from uthread.c:1:
/usr/include/ucontext.h:39:6: note: declared here
 int  setcontext(const ucontext_t *) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_5, __MAC_10_6, __IPHONE_2_0, __IPHONE_2_0) __WATCHOS_PROHIBITED __TVOS_PROHIBITED;
      ^
uthread.c: In function ‘thread_manager’:
uthread.c:43:2: warning: ‘setcontext’ is deprecated [-Wdeprecated-declarations]
  setcontext(&scheduler_context);
  ^
In file included from uthread.h:8:0,
                 from uthread.c:1:
/usr/include/ucontext.h:39:6: note: declared here
 int  setcontext(const ucontext_t *) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_5, __MAC_10_6, __IPHONE_2_0, __IPHONE_2_0) __WATCHOS_PROHIBITED __TVOS_PROHIBITED;
      ^
uthread.c: In function ‘scheduler’:
uthread.c:55:9: warning: ‘swapcontext’ is deprecated [-Wdeprecated-declarations]
         swapcontext(&scheduler_context,&currentThread->context);
         ^
In file included from uthread.h:8:0,
                 from uthread.c:1:
/usr/include/ucontext.h:40:6: note: declared here
 int  swapcontext(ucontext_t * __restrict, const ucontext_t * __restrict) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_5, __MAC_10_6, __IPHONE_2_0, __IPHONE_2_0) __WATCHOS_PROHIBITED __TVOS_PROHIBITED;
      ^
uthread.c: In function ‘initialer’:
uthread.c:72:5: warning: ‘getcontext’ is deprecated [-Wdeprecated-declarations]
     getcontext(&initThread->context);
     ^
In file included from uthread.h:8:0,
                 from uthread.c:1:
/usr/include/ucontext.h:37:6: note: declared here
 int  getcontext(ucontext_t *) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_5, __MAC_10_6, __IPHONE_2_0, __IPHONE_2_0) __WATCHOS_PROHIBITED __TVOS_PROHIBITED;
      ^
uthread.c: In function ‘THREAD_INIT’:
uthread.c:112:3: warning: ‘makecontext’ is deprecated [-Wdeprecated-declarations]
   makecontext(&mainThread->context, (void(*)(void)) main_manager, 0, NULL, NULL);
   ^
In file included from uthread.h:8:0,
                 from uthread.c:1:
/usr/include/ucontext.h:38:6: note: declared here
 void makecontext(ucontext_t *, void (*)(), int, ...) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_5, __MAC_10_6, __IPHONE_2_0, __IPHONE_2_0) __WATCHOS_PROHIBITED __TVOS_PROHIBITED;
      ^
uthread.c: In function ‘THREAD_CREATE’:
uthread.c:138:3: warning: ‘makecontext’ is deprecated [-Wdeprecated-declarations]
   makecontext(&newThread->context, (void(*)(void)) thread_manager, 2, func, arg);
   ^
In file included from uthread.h:8:0,
                 from uthread.c:1:
/usr/include/ucontext.h:38:6: note: declared here
 void makecontext(ucontext_t *, void (*)(), int, ...) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_5, __MAC_10_6, __IPHONE_2_0, __IPHONE_2_0) __WATCHOS_PROHIBITED __TVOS_PROHIBITED;
      ^
uthread.c:143:3: warning: ‘getcontext’ is deprecated [-Wdeprecated-declarations]
   getcontext(&mainThread->context);
   ^
In file included from uthread.h:8:0,
                 from uthread.c:1:
/usr/include/ucontext.h:37:6: note: declared here
 int  getcontext(ucontext_t *) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_5, __MAC_10_6, __IPHONE_2_0, __IPHONE_2_0) __WATCHOS_PROHIBITED __TVOS_PROHIBITED;
      ^
uthread.c:157:3: warning: ‘getcontext’ is deprecated [-Wdeprecated-declarations]
   getcontext(&newThread->context);
   ^
In file included from uthread.h:8:0,
                 from uthread.c:1:
/usr/include/ucontext.h:37:6: note: declared here
 int  getcontext(ucontext_t *) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_5, __MAC_10_6, __IPHONE_2_0, __IPHONE_2_0) __WATCHOS_PROHIBITED __TVOS_PROHIBITED;
      ^
uthread.c:169:3: warning: ‘makecontext’ is deprecated [-Wdeprecated-declarations]
   makecontext(&newThread->context, (void(*)(void)) thread_manager, 2, func, arg);
   ^
In file included from uthread.h:8:0,
                 from uthread.c:1:
/usr/include/ucontext.h:38:6: note: declared here
 void makecontext(ucontext_t *, void (*)(), int, ...) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_5, __MAC_10_6, __IPHONE_2_0, __IPHONE_2_0) __WATCHOS_PROHIBITED __TVOS_PROHIBITED;
      ^
uthread.c:172:3: warning: ‘swapcontext’ is deprecated [-Wdeprecated-declarations]
   swapcontext(&currentThread->context,&scheduler_context);
   ^
In file included from uthread.h:8:0,
                 from uthread.c:1:
/usr/include/ucontext.h:40:6: note: declared here
 int  swapcontext(ucontext_t * __restrict, const ucontext_t * __restrict) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_5, __MAC_10_6, __IPHONE_2_0, __IPHONE_2_0) __WATCHOS_PROHIBITED __TVOS_PROHIBITED;
      ^
uthread.c: In function ‘THREAD_EXIT’:
uthread.c:209:2: warning: ‘setcontext’ is deprecated [-Wdeprecated-declarations]
  setcontext(&scheduler_context);
  ^
In file included from uthread.h:8:0,
                 from uthread.c:1:
/usr/include/ucontext.h:39:6: note: declared here
 int  setcontext(const ucontext_t *) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_5, __MAC_10_6, __IPHONE_2_0, __IPHONE_2_0) __WATCHOS_PROHIBITED __TVOS_PROHIBITED;
      ^
uthread.c: In function ‘THREAD_YIELD’:
uthread.c:217:5: warning: ‘getcontext’ is deprecated [-Wdeprecated-declarations]
     getcontext(&currentThread->context);
     ^
In file included from uthread.h:8:0,
                 from uthread.c:1:
/usr/include/ucontext.h:37:6: note: declared here
 int  getcontext(ucontext_t *) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_5, __MAC_10_6, __IPHONE_2_0, __IPHONE_2_0) __WATCHOS_PROHIBITED __TVOS_PROHIBITED;
      ^
uthread.c:221:9: warning: ‘setcontext’ is deprecated [-Wdeprecated-declarations]
         setcontext(&scheduler_context);
         ^
In file included from uthread.h:8:0,
                 from uthread.c:1:
/usr/include/ucontext.h:39:6: note: declared here
 int  setcontext(const ucontext_t *) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_5, __MAC_10_6, __IPHONE_2_0, __IPHONE_2_0) __WATCHOS_PROHIBITED __TVOS_PROHIBITED;
      ^

最佳答案

On MAC OS, you should replace ucontext.h with sys/ucontext.h? Also, it seems you need to do same thing with many packages.

没有;你不应该替换 <ucontext.h><sys/ucontext.h> ; macOS 手册非常清楚,您应该使用 #include <ucontext.h>违背这一点将会导致问题。

您使用什么编译选项?

header 包含#ifdef _XOPEN_SOURCE然后定义函数,但还有一个 #else条款说

#error The deprecated ucontext routines require _XOPEN_SOURCE to be defined

因此,在 macOS 上,最好不要使用这些函数,但不要使用 swapcontext() 的标题或手册页。说首选的替代方案是什么(假设有替代方案),这很令人恼火。如果您确实想使用 ucontext功能,必须保证_XOPEN_SOURCE是在编译时定义的。您可以直接在命令行上或通过 header 执行此操作。 (我使用自己的 header posixver.h 来完成该任务;您可以在目录 https://github.com/jleffler/soq/tree/master/src/libsoq 中找到它的副本。)

<小时/>

Compiling with -D_XOPEN_SOURCE now gives warnings about the function being deprecated — what can I do about that?

该函数已被标记为已弃用,并且在使用它时会收到警告,因为它已弃用 - 但该函数现在已声明。如果您不想要弃用警告,请添加 -Wno-deprecated-declarations到编译器选项。或者重写代码以使用其他机制来管理您正在使用 swapcontext() 所做的任何事情。 。

请注意,编译器会告诉您哪个选项触发了警告,以便您可以抑制它,如 no- 所示。字首。您还可以使用-Werror=deprecated-declarations强制其成为错误而不是警告。如果编译器不包含消息中那样的标志名称,您可能无法抑制警告或错误。

关于c - Mac OS 上函数 ‘swapcontext’ 的隐式声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41752086/

相关文章:

c - 指令指针和函数指针恒定性

c - strcpy() 返回值

c - 在 C 中使用 for 循环打印值时输出错误

c++ - wxWidgets GUI 应用程序中的多线程?

cocoa - 处理 cocoa 中的菜单事件

c - 在不使用 realloc 的情况下在 C 中增长数组?

java - JUnit 性能测试 (Contiperf)

python - 使用python多线程下载循环

javascript - 在 OS X Safari 中捕捉 Mac 触控板缩放

android - ADT Bundle for mac 已损坏