Cmake error 'libmylib.so undefined reference to 'pthread_create'

asked 2017-11-13 07:07:22 -0600

Merle gravatar image

I am trying to use an external library (libmylib.so) in my package ROS. My problem is that this library required pthread to compile.

I have already use pthread in my work. So, I add pthread rt in the target_link_libraries :

target_link_libraries(mypackage 
    ${catkin_LIBRARIES} 
    ${mylib} 
    pthread rt)

Without using CMake it works fine but using Cmake I foud two errors:

${PATH_TO_MY_LIB}/libmylib.so: undefined reference to 'pthread_create' 
${PATH_TO_MY_LIB}/libmylib.so: undefined reference to 'pthread_cancel'

I founded the same error with these others tests :

Test 1:

add_definietions("-std=c++11 -Wall -g -lpthread")
list(APPEND CMAKE_CXX_FLAGS "-lpthread"

Test 2:

find(package(Threads REQUIRED)
target_link_libraries( mypackage 
    ${catkin_LIBRARIES} 
    ${mylib} 
    ${CMAKE_THREAD_LIBS_INIT} )

Test 3:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")

Test 4:

set(CMAKE_LINKER_FLAGS "-pthread" CACHE STRING "Linker Flags" FORCE)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_LINKER_FLAGS}" CACHE STRING "" FORCE)
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS}" CACHE STRING "" FORCE)
set(CMAKE_EXE_LINKER_FLAGS    "${CMAKE_LINKER_FLAGS}" CACHE STRING "" FORCE)

Test 5:

set_target_properties(mypackage PROPERTIES COMPILE_FLAGS -pthread LINK_FLAGS -pthread)

I am open to any suggestion ! Thank you

edit retag flag offensive close merge delete

Comments

maybe this will be helpful: https://answers.ros.org/question/2744... Also, once you add libraries in the project's CMakeLists.txt, delete the following before running catkin_make: (1)devel directory (2) build directory (3)"global" CMakeLists.txt

BuilderMike gravatar image BuilderMike  ( 2017-11-14 06:47:47 -0600 )edit