0

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 :

Here the CmakeList.txt

cmake_minimum_required(VERSION 2.8.3)
project(mypackage)

find_package(catkin REQUIRED COMPONENTS
    rospy 
    roscpp 
    message_generation
)

FIND_PACKAGE(Threads REQUIRED)

catkin_package(
  CATKIN_DEPENDS
    roscpp
  
)

# Add CATKIN_INCLUDE_DIRS
include_directories(
  include
    ${catkin_INCLUDE_DIRS}
)

# Add hearder directory
include_directories(include)

# Set SOURCE and HEADER files

set(AA_HEARDERS
    include/mypackage/Class.h
)

set(AA_SOURCES
    src/Class.cpp
)

set(LIBRARY_PATH include/Libmylib/Lib)
FIND_LIBRARY(mylib libmylib.so PATHS ${LIBRARY_PATH})

add_executable(exe
  src/main.cpp
  src/Class.cpp
)
 
add_dependencies(exe ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS} )
target_link_libraries(exe
  ${catkin_LIBRARIES}
  ${mylib}
  Threads::Threads
)
  • Does ROS have pthread library? – user7860670 Nov 13 '17 at 11:26
  • Yes, I used pthread in another package without any problems. – Merle Melwyn Nov 13 '17 at 11:30
  • Test 2 got very close to the solution. But instead making your target dependent on `${CMAKE_THREAD_LIBS_INIT}` you should use the `Threads::Threads` dependency. See the documentation for FindThreads.cmake module. – vre Nov 13 '17 at 12:56
  • I also tested it but I get an `error: cannot find -lThreads::Threads` – Merle Melwyn Nov 13 '17 at 13:19
  • Can you provide the entire CMakeLists.txt file? I suppose you need to tweak a few places, not only this one. BTW on which OS are you trying to build your ROS package? – vre Nov 13 '17 at 13:26
  • And also important, which CMake version are you using? – vre Nov 13 '17 at 13:31
  • I am on Ubuntu 14.04, ROS Indigo and Cmake version : 2.8.12.2 – Merle Melwyn Nov 13 '17 at 13:40
  • Ok I see, Threads::Threads is supported from CMake 3.1.0+ onwards. So please first raise your minimum required cmake version to 2.8.12 and try `find_package(Threads REQUIRED) if(THREADS_HAVE_PTHREAD_ARG) target_compile_options(exe PUBLIC "-pthread") endif() if(CMAKE_THREAD_LIBS_INIT) target_link_libraries(exe "${CMAKE_THREAD_LIBS_INIT}") endif()` See https://stackoverflow.com/questions/1620918/cmake-and-libpthread for further explanations. Don't forget to empty the CMakeCache.txt before rerunning CMake. And try to switch the order of mylib and catkin_LIBRARIES linking. – vre Nov 13 '17 at 14:07
  • I have the same result. by switching mylib and catkin_libraries you mean : `target_link_libraries(exe ${mylib} ${catkin_LIBRARIES} )` – Merle Melwyn Nov 13 '17 at 15:54
  • Sorry I'm out of guesses whats going wrong. – vre Nov 13 '17 at 16:03

0 Answers0