ROS Cmake 'libmylib.so undefined reference to 'pthread_create' - c++

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
)

Related

How to use C++ and Matlab Engine under Kdevelop with CMakeList

I'm trying to use Matlab Engine with C++, but I have some problems.
This is my CMakeList.txt
cmake_minimum_required(VERSION 2.8)
project(test)
SET(PROJECT_PATH "/home/bruno/projects/Test")
SET(CMAKE_CXX_FLAGS "-std=c++0x")
SET(CMAKE_BUILD_TYPE "Debug")
SET(CMAKE_VERBOSE_MAKEFILE ON)
SET(CMAKE_USE_RELATIVE_PATHS ON)
find_package(Eigen3 REQUIRED)
include_directories(
${CMAKE_SOURCE_DIR}
)
include_directories (/home/bruno/MATLAB/R2017a/extern/include/)
link_directories(/home/bruno/MATLAB/R2017a/bin/glnxa64/)
include_directories(${EIGEN3_INCLUDE_DIR})
add_executable(${CMAKE_PROJECT_NAME} test.cpp)
target_link_libraries(${CMAKE_PROJECT_NAME} -pthread )
target_link_libraries( ${CMAKE_PROJECT_NAME} libeng.so )
target_link_libraries( ${CMAKE_PROJECT_NAME} libmx.so )
When I try to execute test.cpp, I get
/home/bruno/projects/test/test.cpp:44: undefined reference to `matOpen'
and other undefined reference. What's wrong?
You aren't linking to libmat.so:
target_link_libraries( ${CMAKE_PROJECT_NAME} libmx libeng libmat )

Cmake shared library - include doesn't exist

I am trying to write a cmake file to create a shared library and use it in the executable file in my project. In the main I include heades as <pattern_follower/header_file_name.h>, but when I run make install it says pattern_follower/header_file_name.h does not exist.
cmake_minimum_required(VERSION 2.8)
project(pattern_follower)
add_definitions(-std=c++14)
file(GLOB SOURCES src/*)
file(GLOB HEADERS include/*)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_RELEASE} -Wall -O4 -march=native -Wfatal-errors")
find_package(OpenCV REQUIRED)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
add_library(${PROJECT_NAME} SHARED ${SOURCES})
target_link_libraries( ${PROJECT_NAME} ${OpenCV_LIBS})
add_executable(${PROJECT_NAME}-main main.cpp)
set_target_properties(${PROJECT_NAME}-main PROPERTIES OUTPUT_NAME ${PROJECT_NAME})
target_link_libraries( ${PROJECT_NAME}-main ${PROJECT_NAME})
install(TARGETS ${PROJECT_NAME} DESTINATION lib)
install(TARGETS ${PROJECT_NAME}-main DESTINATION bin)
install(DIRECTORY include DESTINATION include/${PROJECT_NAME})

include_directories and CMAKE_INSTALL_FULL_INCLUDEDIR

I am building two C++ libraries(2 CMake projects). library2 depends on library1. I am installing my first library header files at ${CMAKE_INSTALL_INCLUDEDIR}/mylibraries. Now to include library1 header files in my library2, I am doing include_directories(${CMAKE_INSTALL_FULL_INCLUDEDIR}/mylibraries) in the library2's CMakeLists.txt.
But the make is failing with no such file or directory error.
library1.h no such file or directory from library2.cpp
library1's CmakeLists.txt
cmake_minimum_required (VERSION 3.5)
include(GNUInstallDirs)
project (logger)
set (VERSION_MAJOR 1)
set (VERSION_MINOR 0)
set (Umbrella "ferryfair")
# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
"${PROJECT_SOURCE_DIR}/config.h.in"
"${PROJECT_BINARY_DIR}/config.h"
)
configure_file(
"${PROJECT_SOURCE_DIR}/pkgconfig.pc.in"
"${PROJECT_BINARY_DIR}/pkgconfig.pc"
)
set (GCC_COVERAGE_COMPILE_FLAGS "-std=c++14")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}")
file(GLOB HEADERS *.h)
file(GLOB SOURCES *.cpp)
include_directories(${PROJECT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_INSTALL_FULL_INCLUDEDIR}/${Umbrella})
add_library(${PROJECT_NAME}Static STATIC ${SOURCES} ${HEADERS})
add_library(${PROJECT_NAME} SHARED ${SOURCES} ${HEADERS})
set_target_properties(${PROJECT_NAME}Static PROPERTIES OUTPUT_NAME ${PROJECT_NAME})
set_target_properties(${PROJECT_NAME}
PROPERTIES
VERSION ${VERSION_MAJOR}.${VERSION_MINOR}
SOVERSION ${VERSION_MAJOR}
)
set_target_properties(${PROJECT_NAME}Static
PROPERTIES
VERSION ${VERSION_MAJOR}.${VERSION_MINOR}
SOVERSION ${VERSION_MAJOR}
)
target_link_libraries(${PROJECT_NAME}Static ferrybase)
target_link_libraries(${PROJECT_NAME} ferrybase)
install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(TARGETS ${PROJECT_NAME}Static DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(FILES ${HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${Umbrella})
install(FILES "${PROJECT_BINARY_DIR}/config.h"
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${Umbrella}
RENAME ${PROJECT_NAME}Config.h)
install(FILES "${PROJECT_BINARY_DIR}/pkgconfig.pc"
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
RENAME "${PROJECT_NAME}.pc")
install(FILES "${PROJECT_BINARY_DIR}/pkgconfig.pc"
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig
RENAME "${PROJECT_NAME}.pc")
library2's CMakeLists.txt
cmake_minimum_required (VERSION 3.5)
include(GNUInstallDirs)
project (FFJSON)
IF (DEFINED _DEBUG)
ADD_DEFINITIONS(-D_DEBUG=${_DEBUG})
ENDIF()
set (VERSION_MAJOR 1)
set (VERSION_MINOR 0)
set (Umbrella "ferryfair")
IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
# Mac OS X specific code
SET(macOS ON)
ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
"${PROJECT_SOURCE_DIR}/config.h.in"
"${PROJECT_BINARY_DIR}/config.h"
)
configure_file(
"${PROJECT_SOURCE_DIR}/pkgconfig.pc.in"
"${PROJECT_BINARY_DIR}/pkgconfig.pc"
)
set (GCC_COVERAGE_COMPILE_FLAGS "-std=c++14")
set (GCC_COVERAGE_LINK_FLAGS "-Wl,-unresolved-symbols=ignore-in-shared-libs")
IF (DEFINED _DEBUG)
set (GCC_COVERAGE_COMPILE_FLAGS "${GCC_COVERAGE_COMPILE_FLAGS} -g -O0")
set (GCC_COVERAGE_LINK_FLAGS "${GCC_COVERAGE_LINK_FLAGS} -g -O0")
ENDIF()
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}")
file(GLOB HEADERS *.h)
file(GLOB SOURCES *.cpp)
include_directories(${PROJECT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_INSTALL_FULL_INCLUDEDIR}/${Umbrella})
add_library(${PROJECT_NAME}Static STATIC ${SOURCES} ${HEADERS})
add_library(${PROJECT_NAME} SHARED ${SOURCES} ${HEADERS})
set_target_properties(${PROJECT_NAME}Static PROPERTIES OUTPUT_NAME ${PROJECT_NAME})
set_target_properties(${PROJECT_NAME}
PROPERTIES
VERSION ${VERSION_MAJOR}.${VERSION_MINOR}
SOVERSION ${VERSION_MAJOR}
)
set_target_properties(${PROJECT_NAME}Static
PROPERTIES
VERSION ${VERSION_MAJOR}.${VERSION_MINOR}
SOVERSION ${VERSION_MAJOR}
)
target_link_libraries(${PROJECT_NAME}Static logger ferrybase)
target_include_directories(TestFFJSON PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include/${Umbrella}>
)
add_executable(IteratorIncrement tests/iteratorincrement.cpp)
target_link_libraries(IteratorIncrement FFJSON logger ferrybase)
target_include_directories(IteratorIncrement PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include/${Umbrella}>
)
add_test(test1 TestFFJSON)
add_test(test2 IteratorIncrement)
ENDIF()
Its weird! ridiculous! pathetic!
executing cmake twice fixed the issue. Its an another entry into accidental discoveries.

Can't make cgal and hdf5 work together

I am using these steps (line 42 in the 2nd sourcecode place). However, I reading/writing to files with .h5 extension, where the code needs surely this flag: -lhdf5.
In order to compile the functions for hdf5, I would do something like this:
g++ -std=c++0x main.cpp -lhdf5
Notice that the flag must be placed at the end of the compilation command, as stated in this answer.
I updated my question, due to a comment.
So, I modified the CMakeLists.txt and what I changed was this part:
add_definitions(${CMAKE_CXX_FLAGS} "-std=c++0x")
set(CMAKE_EXE_LINKER_FLAGS "-lhdf5 -lhdf5_hl -lhdf5_cpp")
However, when I execute make, it seems that hdf5 is not found.
EDIT
With Marc's suggestion, I got:
Linking CXX executable exe
/usr/bin/cmake -E cmake_link_script CMakeFiles/exe.dir/link.txt --verbose=1
/usr/bin/c++ -frounding-math -O3 -DNDEBUG -lhdf5 -lhdf5_hl -lhdf5_cpp CMakeFiles/exe.dir/match.cpp.o -o exe -rdynamic -L/home/samaras/code/CGAL-4.3/lib -L/usr/local/lib -lmpfr -lgmp /home/samaras/code/CGAL-4.3/lib/libCGAL.so -lboost_thread-mt -lpthread /usr/local/lib/libboost_system.so /home/samaras/code/CGAL-4.3/lib/libCGAL.so -lboost_thread-mt -lpthread /usr/local/lib/libboost_system.so -Wl,-rpath,/home/samaras/code/CGAL-4.3/lib:/usr/local/lib
and here is the problem, I think (see the answer I linked too). The linker flag of hdf5 is NOT at the end.
How to put it at the end? Maybe I am using the wrong set()?
EDIT - solution
Here is the working CMakeLists.txt:
# Created by the script cgal_create_cmake_script_with_options
# This is the CMake script for compiling a set of CGAL applications.
project( exe )
cmake_minimum_required(VERSION 2.6.2)
if("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" VERSION_GREATER 2.6)
if("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}.${CMAKE_PATCH_VERSION}" VERSION_GREATER 2.8.3)
cmake_policy(VERSION 2.8.4)
else()
cmake_policy(VERSION 2.6)
endif()
endif()
set( CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true )
if ( COMMAND cmake_policy )
cmake_policy( SET CMP0003 NEW )
endif()
# CGAL and its components
add_definitions(${CMAKE_CXX_FLAGS} "-std=c++0x")
find_package( CGAL QUIET COMPONENTS )
if ( NOT CGAL_FOUND )
message(STATUS "This project requires the CGAL library, and will not be compiled.")
return()
endif()
# include helper file
include( ${CGAL_USE_FILE} )
find_package (CGAL)
include (${CGAL_USE_FILE})
add_definitions (${CGAL_CXX_FLAGS_INIT})
include_directories (${CGAL_INCLUDE_DIRS})
set (libraries ${libraries} ${CGAL_LIBRARY} ${CGAL_3RD_PARTY_LIBRARIES})
set (CMAKE_EXE_LINKER_FLAGS "-dynamic ${CMAKE_EXE_LINKER_FLAGS}")
find_package (HDF5 QUIET COMPONENTS CXX)
if (HDF5_FOUND)
include_directories (SYSTEM ${HDF5_CXX_INCLUDE_DIR})
set (HDF5_libraries ${HDF5_hdf5_LIBRARY} ${HDF5_hdf5_cpp_LIBRARY})
set (HDF5_libraries hdf5 hdf5_cpp)
endif (HDF5_FOUND)
# Boost and its components
find_package( Boost REQUIRED )
if ( NOT Boost_FOUND )
message(STATUS "This project requires the Boost library, and will not be compiled.")
return()
endif()
# include for local directory
# include for local package
# Creating entries for target: exe
# ############################
add_executable( exe match.cpp )
add_to_cached_list( CGAL_EXECUTABLE_TARGETS exe )
# Link the executable to CGAL and third-party libraries
target_link_libraries(exe ${CGAL_LIBRARIES} ${CGAL_3RD_PARTY_LIBRARIES} ${libraries} ${HDF5_libraries})
I use CGAL and HDF5 together. Here's the relevant bit from my CMakeLists.txt:
find_package (HDF5 QUIET COMPONENTS CXX)
if (HDF5_FOUND)
include_directories (SYSTEM ${HDF5_CXX_INCLUDE_DIR})
add_library (hdf5 STATIC IMPORTED)
set_target_properties (hdf5 PROPERTIES IMPORTED_LOCATION ${HDF5_hdf5_LIBRARY})
add_library (hdf5_cpp STATIC IMPORTED)
set_target_properties (hdf5_cpp PROPERTIES IMPORTED_LOCATION ${HDF5_hdf5_cpp_LIBRARY})
set (HDF5_libraries hdf5 hdf5_cpp)
add_executable (exe match.cpp)
target_link_libraries (exe ${libraries} ${HDF5_libraries})
endif (HDF5_FOUND)
Earlier in CMakeLists.txt, there is:
find_package (CGAL)
include (${CGAL_USE_FILE})
add_definitions (${CGAL_CXX_FLAGS_INIT})
include_directories (${CGAL_INCLUDE_DIRS})
set (libraries ${libraries} ${CGAL_LIBRARY} ${CGAL_3RD_PARTY_LIBRARIES})
set (CMAKE_EXE_LINKER_FLAGS "-dynamic ${CMAKE_EXE_LINKER_FLAGS}")

Compiling a static executable with CMake

for a project I need to create an executable that includes all the libraries that I used (opencv, cgal) in order to execute it on a computer that has not those libraries. Currently, this is my CMakeLists.txt (I use linux).
cmake_minimum_required(VERSION 2.8)
#set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall -O2")
project( labeling )
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
add_library(OpenCV STATIC IMPORTED)
add_library(CGAL STATIC IMPORTED COMPONENTS Core)
add_library(GMP STATIC IMPORTED)
find_package(OpenCV REQUIRED)
find_package(CGAL QUIET COMPONENTS Core )
find_library(GMP_LIBRARY gmp /usr/lib)
include(src)
include( ${CGAL_USE_FILE} )
include( CGAL_CreateSingleSourceCGALProgram )
set(EXECUTABLE_OUTPUT_PATH ../bin)
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
include_directories( src )
include_directories( ${OpenCV_INCLUDE_DIRS} )
file(GLOB_RECURSE nei_SRC "src/*.cpp")
add_executable( nei_segmentation ${nei_SRC})
target_link_libraries( nei_segmentation ${OpenCV_LIBS} ${GMP_LIBRARY})
In such a way only GMP and some other c++ libraries are included in my executable. My question is: How can I create a makefile in order to automatically including all the libraries in a static manner and creating only a "big" executable that contains all the libraries? Can you help me?
As global CMake settings, add these lines before add_executable, valid for gcc/clang:
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
set(BUILD_SHARED_LIBS OFF)
set(CMAKE_EXE_LINKER_FLAGS "-static")
On Modern CMake (3.x+ - target_link_libraries doc), you can apply the flag to specific targets, in this way:
target_link_libraries(your_target_name -static)
If you're using MSVC, you have to set the compiler and linker flags:
set(CMAKE_FIND_LIBRARY_SUFFIXES ".lib")
target_compile_options(your_target_name [PUBLIC|PRIVATE] /MT)
target_link_options(your_target_name [PUBLIC|PRIVATE] /INCREMENTAL:NO /NODEFAULTLIB:MSVCRT)
or alternatively also:
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
and if you are using MFC, you need to specify the flag to 1 see here:
set(CMAKE_MFC_FLAG 1)
Add these lines after add_executable(MyExec "main.c") (for example) :
target_link_libraries(MyExec PUBLIC "-static")
or before: link_libraries("-static")