cmake 无法配置并出现 pthread 错误

2024-02-26

我正在尝试使用 cmake 配置项目,但出现错误。 CMakeError.log中的内容如下

Change Dir: /home/jjcasmar/D/libs/builds/sofa/CMakeFiles/CMakeTmp

Run Build Command(s):/usr/bin/make cmTC_9feda/fast && /usr/bin/make -f CMakeFiles/cmTC_9feda.dir/build.make CMakeFiles/cmTC_9feda.dir/build
make[1]: Entering directory '/mnt/D/jjcasmar/libs/builds/sofa/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_9feda.dir/src.c.o
/usr/bin/cc   -DCMAKE_HAVE_LIBC_PTHREAD   -o CMakeFiles/cmTC_9feda.dir/src.c.o   -c /home/jjcasmar/D/libs/builds/sofa/CMakeFiles/CMakeTmp/src.c
Linking C executable cmTC_9feda
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_9feda.dir/link.txt --verbose=1
/usr/bin/cc  -DCMAKE_HAVE_LIBC_PTHREAD    -rdynamic CMakeFiles/cmTC_9feda.dir/src.c.o  -o cmTC_9feda 
/usr/bin/ld: CMakeFiles/cmTC_9feda.dir/src.c.o: in function `main':
src.c:(.text+0x3e): undefined reference to `pthread_create'
/usr/bin/ld: src.c:(.text+0x4a): undefined reference to `pthread_detach'
/usr/bin/ld: src.c:(.text+0x5b): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
make[1]: *** [CMakeFiles/cmTC_9feda.dir/build.make:87: cmTC_9feda] Error 1
make[1]: Leaving directory '/mnt/D/jjcasmar/libs/builds/sofa/CMakeFiles/CMakeTmp'
make: *** [Makefile:121: cmTC_9feda/fast] Error 2


Source file was:
#include <pthread.h>

void* test_func(void* data)
{
  return data;
}

int main(void)
{
  pthread_t thread;
  pthread_create(&thread, NULL, test_func, NULL);
  pthread_detach(thread);
  pthread_join(thread, NULL);
  pthread_atfork(NULL, NULL, NULL);
  pthread_exit(NULL);

  return 0;
}

尽管如此,它表示编译器正确接受 -pthread

-- Found PythonInterp: /usr/bin/python (found version "3.8.2")
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - yes
-- Found Threads: TRUE

我尝试了一个非常简单的 CMakeLists (如下)来检查 pthreads 是否工作正常并且看起来工作正常(配置和构建正常)

cmake_minimum_required(VERSION 3.10)
project(Test)

find_package(Threads REQUIRED)

add_executable(Test a.cpp)
target_link_libraries(Test Threads::Threads)

当我配置时有以下输出

-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Configuring done
-- Generating done

我不确定如何找到问题的原因。任何想法都会有帮助


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

cmake 无法配置并出现 pthread 错误 的相关文章

随机推荐