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
)