在Mac OS上隐式声明函数'swapcontext'

时间:2017-01-19 21:32:25

标签: c multithreading macos

我只是将我的proj上传到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;
      ^

1 个答案:

答案 0 :(得分:0)

  

在MAC OS上,您应该将ucontext.h替换为sys/ucontext.h?此外,似乎你需要对许多包做同样的事情。

没有;您不应将<ucontext.h>替换为<sys/ucontext.h>; macOS手册很清楚你应该使用#include <ucontext.h>并反对这会导致问题。

您使用的编辑选项是什么?

标题包含#ifdef _XOPEN_SOURCE,然后定义函数,但还有一个#else子句,表示

#error The deprecated ucontext routines require _XOPEN_SOURCE to be defined

因此,在macOS上,最好不要使用这些函数 - 但是swapcontext()的标题和手册页都没有说出首选的替代方法(假设有替代方法),这是刺激性。如果您确实想使用ucontext函数,则必须确保在编译时定义_XOPEN_SOURCE。您可以直接在命令行上执行此操作,也可以通过标头执行此操作。 (我使用自己的标题posixver.h来完成任务;您可以在目录https://github.com/jleffler/soq/tree/master/src/libsoq中找到它的副本。)

  

使用-D_XOPEN_SOURCE进行编译现在会发出有关不推荐使用的函数的警告 - 我该怎么办?

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

请注意,编译器会告诉您哪个选项触发了警告,因此您可以使用no-前缀来禁止它。您还可以使用-Werror=deprecated-declarations强制它成为错误而不是警告。如果编译器在消息中没有包含类似标志的名称,则可能无法抑制警告或错误。