0

I'm trying to compile the project with the structure:

-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)

For compiling I'm going to build folder and do cmake .. and cmake --build . But I get errors:

/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'

So, I need to add pthread to my project. I tried many ways to do it but any of them works. This is the first 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})

The second:

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}"
    )

And the third:

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})

As you can see - I'm trying to include required lib in the second CMakeLists. But the error is still here.
What else can I do?

eng
  • 443
  • 1
  • 4
  • 10
Dmitry
  • 160
  • 9
  • 2
    Just `target_link_libraries("${PROJECT_NAME}" PRIVATE pthread)` should do the job. According to newer CMake docs `Threads::Threads` also should work, and even add correct compile time flags. What about removing all the noise from your project, and just trying `add_executable(main main.cpp)` `target_link_libraries(main PUBLIC Threads::Threads)`. – pptaszni Jul 20 '23 at 08:11
  • @pptaszni I added it before `target_link_libraries("${PROJECT_NAME}" PRIVATE "core_${PROJECT_NAME}" third_party_lib)` into the second CMakeLists and it didn't work. – Dmitry Jul 20 '23 at 08:15
  • 1
    A no sorry, I see now. You link `Threads::Threads` with the wrong target. Your `third_party_lib` needs it already. – pptaszni Jul 20 '23 at 08:17
  • 1
    https://stackoverflow.com/a/49487630/4165552 similar – pptaszni Jul 20 '23 at 08:22
  • @pptaszni I did this ``` add_library(third_party_lib SHARED IMPORTED) set_property(TARGET third_party_lib PROPERTY IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/lib/third_party_lib.so" ) set_property(TARGET third_party_lib PROPERTY INTERFACE_COMPILE_DEFINITIONS Threads::Threads ) ``` And it still not works. – Dmitry Jul 20 '23 at 10:23
  • 1
    Then dunno, debug your project. Maybe GLIBC versions are different, if the third party was compiled on some older system. – pptaszni Jul 20 '23 at 10:50
  • 1
    @pptaszni thanks! After all it was a version difference. I had 2.31 glibc and, as I see in the cmake output - it required 2.34. I compiled the project inside a docker container with newer ubuntu version and it works! – Dmitry Jul 21 '23 at 14:53

0 Answers0