【问题标题】:Implicit declaration of function ‘swapcontext’ on Mac OSMac OS 上函数“swapcontext”的隐式声明
【发布时间】:2017-06-04 18:14:06
【问题描述】:

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

我在网上查了很多资料,将 ucontext.h 替换为 sys/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;
      ^

【问题讨论】:

  • “将 ucontext.h 替换为 sys/ucontext.h”。你为什么这么做?你这样做之前的行为是什么? swapcontext man page 表示包含 <ucontext.h>
  • 在 MAC OS 上,您应该将 ucontext.h 替换为 sys/ucontext.h 吗?此外,您似乎需要对许多软件包做同样的事情。试试make我的项目,很方便。

标签: c multithreading macos


【解决方案1】:

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

没有;你不应该用<sys/ucontext.h>替换<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 强制它成为错误而不是警告。如果编译器没有在消息中包含类似标志的名称,您可能无法抑制警告或错误。

【讨论】:

  • 你能看到我的github链接吗? Makefile 在那里。我更新了我的问题,我已经定义了_XOPEN_SOURCE
猜你喜欢
  • 2012-09-27
  • 2021-01-20
  • 1970-01-01
  • 2014-06-28
  • 2022-01-16
  • 2011-10-05
  • 2013-10-28
  • 2019-07-16
相关资源
最近更新 更多