cmake 返回“未定义对 pthread_create 的引用”的错误

Posted

技术标签:

【中文标题】cmake 返回“未定义对 pthread_create 的引用”的错误【英文标题】:cmake returning error of "undefined reference to pthread_create" 【发布时间】:2020-07-20 09:39:49 【问题描述】:

我正在尝试构建 webrtc 项目:https://github.com/mpromonet/webrtc-streamer?fbclid=IwAR2HBwWIZvRhSlmKBllQj2knaBPn6oeEvS24STPLTZHbky2Ta5sbcoPCoc8

当我运行cmake . 时出现以下错误:

WEBRTCBUILD = Release
WEBRTCROOT = /home/mohamed/Desktop/upwork/SoftwareExperts/test3/webrtc-streamer/../webrtc
WEBRTCDESKTOPCAPTURE= ON
CMAKE_CXX_COMPILER_ID=GNU
ALSA_FOUND = FALSE
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
WEBRTC_LIBRARY
    linked by target "webrtc-streamer" in directory /home/mohamed/Desktop/upwork/SoftwareExperts/test3/webrtc-streamer

-- Configuring incomplete, errors occurred!
See also "/home/mohamed/Desktop/upwork/SoftwareExperts/test3/webrtc-streamer/CMakeFiles/CMakeOutput.log".
See also "/home/mohamed/Desktop/upwork/SoftwareExperts/test3/webrtc-streamer/CMakeFiles/CMakeError.log".

及以后“CMakeError.log”

的内容
Determining if the pthread_create exist failed with the following output:
Change Dir: /home/mohamed/Desktop/upwork/SoftwareExperts/test3/webrtc-streamer/CMakeFiles/CMakeTmp

Run Build Command:"/usr/bin/make" "cmTC_13271/fast"
/usr/bin/make -f CMakeFiles/cmTC_13271.dir/build.make CMakeFiles/cmTC_13271.dir/build
make[1]: Entering directory '/home/mohamed/Desktop/upwork/SoftwareExperts/test3/webrtc-streamer/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_13271.dir/CheckSymbolExists.c.o
/usr/bin/cc    -fPIE   -o CMakeFiles/cmTC_13271.dir/CheckSymbolExists.c.o   -c /home/mohamed/Desktop/upwork/SoftwareExperts/test3/webrtc-streamer/CMakeFiles/CMakeTmp/CheckSymbolExists.c
Linking C executable cmTC_13271
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_13271.dir/link.txt --verbose=1
/usr/bin/cc       CMakeFiles/cmTC_13271.dir/CheckSymbolExists.c.o  -o cmTC_13271 -rdynamic 
CMakeFiles/cmTC_13271.dir/CheckSymbolExists.c.o: In function `main':
CheckSymbolExists.c:(.text+0x1b): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
CMakeFiles/cmTC_13271.dir/build.make:97: recipe for target 'cmTC_13271' failed
make[1]: *** [cmTC_13271] Error 1
make[1]: Leaving directory '/home/mohamed/Desktop/upwork/SoftwareExperts/test3/webrtc-streamer/CMakeFiles/CMakeTmp'
Makefile:126: recipe for target 'cmTC_13271/fast' failed
make: *** [cmTC_13271/fast] Error 2

   
File /home/mohamed/Desktop/upwork/SoftwareExperts/test3/webrtc-streamer/CMakeFiles/CMakeTmp/CheckSymbolExists.c:
/* */
#include <pthread.h>

int main(int argc, char** argv)

  (void)argv;
#ifndef pthread_create
  return ((int*)(&pthread_create))[argc];
#else
  (void)argc;
  return 0;
#endif

在 CMakeLists.txt 之后:https://github.com/mpromonet/webrtc-streamer/blob/master/CMakeLists.txt

pthread 库安装在我的 ubuntu PC 上,因为使用 -lpthread 编译测试文件“test.c”没有问题

我错过了什么?

【问题讨论】:

这能回答你的问题吗? Undefined reference to pthread_create in Linux 来自建议的副本,尤其是这个答案可能是相关的:***.com/a/57685932/6782754 @MarekR 这与将问题链接到 pthread 无关。这个问题是关于 cmake 问题。我可以毫无问题地使用 -lpthread 编译测试文件 (.c) @MOHAMED 您的错误是链接问题。第二条评论直接指向回答(我提供了重复)如何在 cmake 项目中修复它。 这个错误是关于没有找到webrtc库,它应该是WEBRTC_LIBRARY变量的值。 CMakeError.log 的内容绝对与问题无关,您可以放心地将其从帖子中删除。具体来说,该行失败:find_library(WEBRTC_LIBRARY NAMES webrtc PATHS $WEBRTCOBJS)。如README 中所述,WebRTC SDK 预计位于../webrtc 目录下。确保你拥有它 【参考方案1】:

您需要将“pthread”添加到 CMakeLists.txt 中的“target_link_libraries”部分。

[编辑]

正如@arrowd 所建议的,正确的方法是使用“FindThreads”: https://cmake.org/cmake/help/latest/module/FindThreads.html

【讨论】:

一般情况下不会。正确的做法是使用FindThreads模块及其定义的变量。 好吧,谢谢你的信息,我不知何故错过了。

以上是关于cmake 返回“未定义对 pthread_create 的引用”的错误的主要内容,如果未能解决你的问题,请参考以下文章

cmake 返回“未定义对 pthread_create 的引用”的错误

'find_library'在CMake的循环中返回相同的值

CMake中return的使用

cmake安装方法

CMake--变量

CMake 找不到诅咒