【问题标题】:Simple .c file fails to compile pthreads.h简单的 .c 文件无法编译 pthreads.h
【发布时间】:2021-08-26 01:48:24
【问题描述】:

我是一个长期的 python 爱好者,将脚本移植到 c。我相信环境中有问题阻止代码编译。其他地方的研究让我相信它与 posix 头文件有关?也许有宏?我在 c 方面的经验不足,无法弄清楚。

相关的sn-p在这里:

pthread_t id;
thread_create(&id, NULL, refreshqb,NULL);
void *status;
pthread_start(id, (void**)&status);

我收到的错误是这样的。

t.c:91:4: warning: implicit declaration of function 'thread_create' is invalid in C99
      [-Wimplicit-function-declaration]
   thread_create(&id, NULL, refreshqb,NULL);
   ^
t.c:93:4: warning: implicit declaration of function 'pthread_start' is invalid in C99
      [-Wimplicit-function-declaration]
   pthread_start(id, (void**)&status);
   ^
2 warnings generated.
Undefined symbols for architecture x86_64:
  "_pthread_start", referenced from:
      _main in t-0d3a02.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

【问题讨论】:

  • 您已经声明了pthreads.h,但您似乎没有在 C 文件中使用它。这有点令人困惑。

标签: c multithreading


【解决方案1】:

编译代码时必须明确启用对 pthread 的支持。看起来你正在使用 clang,所以在使用 clang 编译时添加 -pthread 标志即可。

【讨论】:

  • 谢谢。我还没试过这个。那么“cc -pthread file.c”。
  • @user2930324 是的!如果这不能解决您的问题,请随时发表另一条评论,我可以再看看
【解决方案2】:

请尝试使用 pthread_create,同时包含正确的 posix 库头文件或“#include ”。

【讨论】:

  • 我做到了。源文件比我发布的要长得多。我刚刚包含了相关的 sn-p。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多