使用CMake对pthread的未定义引用

6tqwzwtp  于 2023-11-19  发布在  其他
关注(0)|答案(1)|浏览(248)

我正在尝试使用以下结构编译项目:

-project
    |-CMakeLists.txt       (will call it the first CMakeLists later)
    |-build
         |-some files here
    |-src
       |-main.cpp
       |-my_lib.h
       |-my_lib.cpp
       |-CMakeLists.txt      (will call it the second CMakeLists later)
    |-lib
       |-third_party_lib.so
    |-dep
       |-third_party_lib
               |-few .h files here
       |-CMakeLists.txt           (will call it the third CMakeLists later)

字符串
对于编译,我将构建文件夹并执行cmake ..cmake --build .,但我得到错误:

/usr/bin/ld: ../lib/third_party_lib.so: undefined reference to `pthread_key_create@GLIBC_2.34'
/usr/bin/ld: ../lib/third_party_lib.so: undefined reference to `pthread_setspecific@GLIBC_2.34'
/usr/bin/ld: ../lib/third_party_lib.so: undefined reference to `__libc_single_threaded@GLIBC_2.32'
/usr/bin/ld: ../lib/third_party_lib.so: undefined reference to `pthread_mutex_trylock@GLIBC_2.34'
/usr/bin/ld: ../lib/third_party_lib.so: undefined reference to `pthread_getspecific@GLIBC_2.34'
/usr/bin/ld: ../lib/third_party_lib.so: undefined reference to `std::__throw_bad_array_new_length()@GLIBCXX_3.4.29'
/usr/bin/ld: ../lib/third_party_lib.so: undefined reference to `lstat@GLIBC_2.33'
/usr/bin/ld: ../lib/third_party_lib.so: undefined reference to `pthread_once@GLIBC_2.34'
/usr/bin/ld: ../lib/third_party_lib.so: undefined reference to `pthread_cond_clockwait@GLIBC_2.34'


所以,我需要将pthread添加到我的项目中。我尝试了很多方法来做这件事,但它们都有效。这是第一个CMakeLists:

cmake_minimum_required(VERSION 3.13)
project(reader LANGUAGES CXX)
include(GNUInstallDirs)
include (CTest)
find_package(PkgConfig REQUIRED)
add_subdirectory(dep)
add_executable("${PROJECT_NAME}")
add_subdirectory(src)
install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ${CMAKE_INSTALL_FULL_BINDIR})


第二个:

add_library("core_${PROJECT_NAME}")
target_include_directories("core_${PROJECT_NAME}" PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

add_library(third_party_lib SHARED IMPORTED)
set_property(TARGET third_party_lib PROPERTY IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/lib/third_party_lib.so")
target_include_directories("core_${PROJECT_NAME}" PUBLIC ${CMAKE_SOURCE_DIR}/dep/third_party_lib)

target_sources("core_${PROJECT_NAME}" PRIVATE
    my_lib.cpp
    )

target_compile_features("core_${PROJECT_NAME}" PUBLIC cxx_std_17)
target_link_libraries("core_${PROJECT_NAME}" PUBLIC
    stdc++fs
    )

target_sources("${PROJECT_NAME}" PRIVATE main.cpp)
target_link_libraries("${PROJECT_NAME}" PRIVATE "core_${PROJECT_NAME}" third_party_lib)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries("${PROJECT_NAME}" PRIVATE Threads::Threads)
target_compile_features("${PROJECT_NAME}" PRIVATE cxx_std_17)
target_compile_definitions("${PROJECT_NAME}" PRIVATE
    VERSION="${PROJECT_VERSION}"
    )


第三个:

set(SAVED_BUILD_DOCUMENTATION ${BUILD_DOCUMENTATION})
set(BUILD_DOCUMENTATION OFF)
set(SAVED_BUILD_TESTING BUILD_TESTING)
set(BUILD_TESTING OFF)
set(BUILD_DOCUMENTATION ${SAVED_BUILD_DOCUMENTATION})
set(BUILD_TESTING ${SAVED_BUILD_TESTING})


正如你所看到的-我试图在第二个CMakeLists中包含必需的lib。但是错误仍然在这里。
我还能怎么办?

vm0i2vca

vm0i2vca1#

好像你的libc.so太老了。
__libc_single_threaded只存在于新的libc.so中。

相关问题