target_link_libraries for lpthread, ldl, and lutils

Hello, i am new at using cmake for compiling and building c++ code.

here is my CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
project(example)
add_subdirectory(pybind11)

find_package( Threads REQUIRED )
#find_package( OpenCV REQUIRED )

include_directories( ${OpenCV_INCLUDE_DIRS})

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

add_executable(demo demo_python.cpp)

target_link_libraries(demo ${CMAKE_THREAD_LIBS_INIT} ${CMAKE_DL_LIBS})

target_link_libraries(demo pybind11::embed)# ${OpenCV_LIBS})

i have checked and ensured that my computer has the required library. and i am keep getting this error, which say that i haven’t linked pthread, ldl, and lutil.

/usr/local/lib/libpython3.7m.a(thread.o): In function `PyThread_tss_create':
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:750: undefined reference to `pthread_key_create'
/usr/local/lib/libpython3.7m.a(thread.o): In function `PyThread_tss_delete':
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:767: undefined reference to `pthread_key_delete'
/usr/local/lib/libpython3.7m.a(thread.o): In function `PyThread_start_new_thread':
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:222: undefined reference to `pthread_create'
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:239: undefined reference to `pthread_detach'
/usr/local/lib/libpython3.7m.a(thread.o): In function `PyThread_acquire_lock_timed':
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:372: undefined reference to `sem_wait'
/usr/local/lib/libpython3.7m.a(thread.o): In function `PyThread_start_new_thread':
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:203: undefined reference to `pthread_attr_setstacksize'
/usr/local/lib/libpython3.7m.a(thread.o): In function `PyThread_tss_delete':
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:767: undefined reference to `pthread_key_delete'
/usr/local/lib/libpython3.7m.a(thread.o): In function `_pythread_pthread_set_stacksize':
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:640: undefined reference to `pthread_attr_setstacksize'
/usr/local/lib/libpython3.7m.a(thread.o): In function `PyThread_create_key':
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:677: undefined reference to `pthread_key_create'
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:682: undefined reference to `pthread_key_delete'
/usr/local/lib/libpython3.7m.a(thread.o): In function `PyThread_set_key_value':
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:712: undefined reference to `pthread_setspecific'
/usr/local/lib/libpython3.7m.a(thread.o): In function `PyThread_delete_key':
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:696: undefined reference to `pthread_key_delete'
/usr/local/lib/libpython3.7m.a(thread.o): In function `PyThread_delete_key_value':
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:704: undefined reference to `pthread_setspecific'
/usr/local/lib/libpython3.7m.a(thread.o): In function `PyThread_get_key_value':
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:723: undefined reference to `pthread_getspecific'
/usr/local/lib/libpython3.7m.a(thread.o): In function `PyThread_allocate_lock':
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:292: undefined reference to `sem_init'
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:292: undefined reference to `sem_init'
/usr/local/lib/libpython3.7m.a(thread.o): In function `PyThread_free_lock':
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:317: undefined reference to `sem_destroy'
/usr/local/lib/libpython3.7m.a(thread.o): In function `PyThread_acquire_lock_timed':
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:369: undefined reference to `sem_trywait'
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:369: undefined reference to `sem_trywait'
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:372: undefined reference to `sem_wait'
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:372: undefined reference to `sem_wait'
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:369: undefined reference to `sem_trywait'
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:372: undefined reference to `sem_wait'
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:369: undefined reference to `sem_trywait'
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:369: undefined reference to `sem_trywait'
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:369: undefined reference to `sem_trywait'
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:372: undefined reference to `sem_wait'
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:369: undefined reference to `sem_trywait'
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:366: undefined reference to `sem_timedwait'
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:372: undefined reference to `sem_wait'
/usr/local/lib/libpython3.7m.a(thread.o): In function `PyThread_release_lock':
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:441: undefined reference to `sem_post'
/usr/local/lib/libpython3.7m.a(thread.o): In function `PyThread_tss_set':
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:776: undefined reference to `pthread_setspecific'
/usr/local/lib/libpython3.7m.a(thread.o): In function `PyThread_tss_get':
/home/ion/Documents/Python-3.7.7/Python/thread_pthread.h:784: undefined reference to `pthread_getspecific'
/usr/local/lib/libpython3.7m.a(dynload_shlib.o): In function `_PyImport_FindSharedFuncptr':
/home/ion/Documents/Python-3.7.7/./Python/dynload_shlib.c:96: undefined reference to `dlopen'
/home/ion/Documents/Python-3.7.7/./Python/dynload_shlib.c:127: undefined reference to `dlsym'
/home/ion/Documents/Python-3.7.7/./Python/dynload_shlib.c:102: undefined reference to `dlerror'
/home/ion/Documents/Python-3.7.7/./Python/dynload_shlib.c:96: undefined reference to `dlopen'
/home/ion/Documents/Python-3.7.7/./Python/dynload_shlib.c:83: undefined reference to `dlsym'
/usr/local/lib/libpython3.7m.a(posixmodule.o): In function `os_forkpty_impl':
/home/ion/Documents/Python-3.7.7/./Modules/posixmodule.c:6048: undefined reference to `forkpty'
/usr/local/lib/libpython3.7m.a(posixmodule.o): In function `os_openpty_impl':
/home/ion/Documents/Python-3.7.7/./Modules/posixmodule.c:5954: undefined reference to `openpty'
/usr/local/lib/libpython3.7m.a(signalmodule.o): In function `signal_pthread_sigmask_impl':
/home/ion/Documents/Python-3.7.7/./Modules/signalmodule.c:855: undefined reference to `pthread_sigmask'
/usr/local/lib/libpython3.7m.a(signalmodule.o): In function `signal_pthread_kill_impl':
/home/ion/Documents/Python-3.7.7/./Modules/signalmodule.c:1124: undefined reference to `pthread_kill'
/usr/local/lib/libpython3.7m.a(timemodule.o): In function `time_pthread_getcpuclockid':
/home/ion/Documents/Python-3.7.7/./Modules/timemodule.c:342: undefined reference to `pthread_getcpuclockid'
/usr/local/lib/libpython3.7m.a(faulthandler.o): In function `faulthandler_thread':
/home/ion/Documents/Python-3.7.7/./Modules/faulthandler.c:564: undefined reference to `pthread_sigmask'
collect2: error: ld returned 1 exit status
CMakeFiles/demo.dir/build.make:84: recipe for target 'demo' failed
make[2]: *** [demo] Error 1
CMakeFiles/Makefile2:95: recipe for target 'CMakeFiles/demo.dir/all' failed
make[1]: *** [CMakeFiles/demo.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

Please help me. I am very frustrated and tired because of this error. i have spent 2 days of ssearching and trying with no result.
thank you very much.

best regards,
Albert Christianto

Could you post the link line that gets used for that link step and the value of the variables in the target_link_libraries?

Hi @ben.boeckel,
Sorry for my late response because I was distracted on another task.
Thank you very much for your responses. Actually, I have successfully solved it in 3 hours after i posted this question. It turns out, the problem is in my own python interpreter library which hasn’t been linked with pthread libs, dl libs, and utility libs correctly.

However, i am quite confuse though why i can’t force my own apps to be linked with these 3 libraries.
So, the value of the variables in the target_link_libraries
here is my CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
project(example)
add_subdirectory(pybind11)

find_package( Threads REQUIRED )
#find_package( Utils REQUIRED )

include_directories( ${OpenCV_INCLUDE_DIRS})

message(${CMAKE_THREAD_LIBS_INIT})#pthread library
message(${CMAKE_DL_LIBS})#dl libs
#message(lutil)#utility libs
message(pybind11::embed)#pybind library

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

add_executable(demo demo_python.cpp)

target_link_libraries(demo ${CMAKE_THREAD_LIBS_INIT} ${CMAKE_DL_LIBS} lutil)

target_link_libraries(demo pybind11::embed)

and here the output of the cmake

-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.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
-- Found PythonInterp: /usr/bin/python3.7 (found version "3.7.7")
-- Found PythonLibs: /usr/lib/libpython3.7m.so
-- Performing Test HAS_CPP14_FLAG
-- Performing Test HAS_CPP14_FLAG - Success
-- pybind11 v2.4.dev4
-- 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
pthread
dl
pybind11::embed
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ion/Documents/embed_python

and I am still don’t know what the flags is for lutil in cmake.

best regards,
Albert Christianto

You probably just want util, not lutil. The first l is part of the flags to tell it what library to link to.

hi @ben.boeckel,
Thank you. Now i know the cmake flag for the utility library.
so right now i can’t reproduce the error, since i have fixed the python library.
I will try to read CMake documentation once again to fix my lacks of knowledge in CMake. ^v^
best regards,
Albert Christianto