Add lib*.so to cmake - c++

I've my package with CMakeLists.txt and I have a libNewLib.so compiled from another package from another library (NewLib). I've also the header files of that library that I include like so:
set(LIB_MANUALLY "${CMAKE_CURRENT_SOURCE_DIR}/lib/") #I put the file libNewLib.so here
include_directories(
include/NewLib
)
LINK_DIRECTORIES(${LIB_MANUALLY})
target_link_libraries(estimation libNewLib.so)
But I still get the error:
/usr/bin/ld: cannot find -lNewLib
Is it the correct way to do it?
I tried a couple of solutions but it didn't work.
I'm using Ros kinetic catkin package
make VERSION 2.8.3
Full CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.3)
project(localization)
add_compile_options(-std=c++11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Ofast -march=native")
set(LIB_MANUALLY "${CMAKE_CURRENT_SOURCE_DIR}/lib/")
message(STATUS "LIB_MANUALLY : ${LIB_MANUALLY}")
find_package(catkin REQUIRED COMPONENTS
cv_bridge
image_transport
roscpp
rospy
std_msgs
)
find_package(OpenCV REQUIRED
NO_MODULE # should be optional, tells CMake to use config mode
PATHS /usr/local # look here
NO_DEFAULT_PATH) # and don't look anywhere else
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES localization
CATKIN_DEPENDS cv_bridge image_transport roscpp rospy std_msgs
# DEPENDS system_lib
)
include_directories(
include
include/NewLib
${catkin_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS}
)
add_executable(estimation
src/pose_map_estimation.cpp
src/pose_map_estimation_main.cpp
)
target_link_libraries(estimation ${catkin_LIBRARIES} ${OpenCV_LIBRARIES} libNewLib.so)
LINK_DIRECTORIES(${LIB_MANUALLY})

In https://cmake.org/cmake/help/v3.0/command/link_directories.html it says:
Specify the paths in which the linker should search for libraries. The command will apply only to targets created after it is called.
So, try if it works if you create the target AFTER the link_directories command.
You could also consider to write a proper cmake file for locating your library and use find_package to find it.

Related

I want to link my project with the dynamic library "aravis" in the CMakeLists.txt file, but it always fails

Although I tried some methods, all the libraries involved in my project were not linked, even the most basic rclcpp library.
And there is also the problem that the header file cannot be found in vscode, although I think I have given the path to the header file in CMake and the .so file, but there is still no way to solve the problem.
cmake_minimum_required(VERSION 3.5)
project(camera_aravis)
# Default to C99
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
INCLUDE_DIRECTORIES(/usr/local/include/aravis-0.8
/home/nian/Documents/Dep/glib-2.45.2/glib
/home/nian/Documents/Dep/glib-2.45.2
/home/nian/Documents/Dep/glib-2.45.2/gmodule
/opt/ros/foxy/include
/home/nian/Documents/Dep/xmlrpcpp-master/include/xmlrpcpp
/home/nian/Documents/Dep/xmlrpcpp-master/include
)
LINK_DIRECTORIES(/usr/lib /home/nian/Documents/Dep/glib-2.45.2/glib
)
ADD_EXECUTABLE(camnode src/new.cpp)
TARGET_LINK_LIBRARIES(camnode libaravis-0.8.so libglib-2.0.so libgio-2.0.so
libgobject-2.0.so)
include_directories(
include_directories
${fcl_INCLUDE_DIRS}
)
# find other ROS2 packages
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_components REQUIRED)
find_package(std_msgs REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(camera_info_manager REQUIRED)
find_package(image_transport REQUIRED)
add_executable(test_params_rclcpp src/new.cpp)
ament_target_dependencies(test_params_rclcpp
rclcpp
rclcpp_components
sensor_msgs
std_msgs
camera_info_manager
image_transport
camera_calibration_parsers)
install(TARGETS
test_params_rclcpp
DESTINATION lib/${PROJECT_NAME}
)
add_executable(camera_param_edit src/new.cpp)
ament_target_dependencies(camera_param_edit
rclcpp
rclcpp_components
sensor_msgs
std_msgs
camera_info_manager
image_transport
camera_calibration_parsers)
install(TARGETS
camera_param_edit
DESTINATION lib/${PROJECT_NAME}
)
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# uncomment the line when a copyright and license is not present in all source files
#set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# uncomment the line when this package is not in a git repo
#set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
ament_package()
This is my CMakeLists, but the error in the terminal is always "undefined reference to".
How can I solve this?
Below is part of my error messagea lot of errors about function references in the library

Creation of a cmake module for using a library

I am new to C++ and have just started to use Cmake for linking the libraries to my project. I need to use a library:
https://github.com/Gnimuc/FastPD
Fortunately, I managed to build the library using Cmake (in my build there is no *.lib file at all), but I don't know how to link it to my project. I mean that I don't know how to add it to my cmakelists.txt :
(PS. I'm also using two other libraries ITK and VTK; but I can't link the above mentioned library to my project or main.cpp.)
################################################
cmake_minimum_required(VERSION 2.8)
project(My_project)
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
if (ITKVtkGlue_LOADED)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
else()
find_package(ItkVtkGlue REQUIRED)
include(${ItkVtkGlue_USE_FILE})
set(Glue ItkVtkGlue)
endif()
add_executable(My_project MACOSX_BUNDLE main.cpp)
target_link_libraries(My_project
${Glue} ${VTK_LIBRARIES} ${ITK_LIBRARIES})
################################################
Thanks in advance for your helps,
Assuming you installed the library and its header files, you can then search for the header files using find_path, and add the found path to the include directories. Then you can search for the library by using find_library, and add the library with the target_link_libraries command.
I tried both shared and static types for creation of the library. In the case of shared, no *.lib file was created, and in the case of static, my project couldn't link to the library. Therefore, in my project's CmakeLists.txt, I decided to add all the *.cpp and headers as libraries and then link them together (as I didn't know the dependency among them!!!), and finally, I linked them to my project. Perhaps it does not make sense but it works; hope it helps you:
CmakeLists.txt
##############################################
cmake_minimum_required(VERSION 2.6)
set(PROJ_NAME PROJECT46)
PROJECT(${PROJ_NAME})
# Prevent compilation in-source
if( ${CMAKE_BINARY_DIR} STREQUAL ${PROJECT_SOURCE_DIR} )
Message( " " )
Message( FATAL_ERROR "Source and build directories are the same.
Create an empty build directory,
change into it and re-invoke cmake")
endif()
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support.
Please use a different C++ compiler.")
endif()
##############################################
## External libraries
##############################################
list( APPEND CMAKE_MODULE_PATH
${PROJECT_SOURCE_DIR}/cmake
)
# Blitz
find_package( Blitz++ REQUIRED )
list( APPEND PROJ_INCLUDE_DIRS
${Blitz++_INCLUDE_DIR}
)
list( APPEND
PROJ_LIB
${Blitz++_LIBRARIES}
)
# ITK and VTK
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
if (ITKVtkGlue_LOADED)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
else()
find_package(ItkVtkGlue REQUIRED)
include(${ItkVtkGlue_USE_FILE})
set(Glue ItkVtkGlue)
endif()
##############################################
# FASTPD
add_library(FASTPD Fast_PD.cpp Fast_PD.h common.h block.h)
add_library(GRAPH graph.h graph.cpp)
add_library(linked LinkedBlockList.h LinkedBlockList.cpp)
add_library(MAXFLOW maxflow.cpp)
include_directories(${PROJ_INCLUDE_DIRS})
add_executable(${PROJ_NAME} main.cpp)
target_link_libraries(linked MAXFLOW)
target_link_libraries(GRAPH linked)
target_link_libraries(FASTPD GRAPH)
target_link_libraries(${PROJ_NAME} FASTPD)
target_link_libraries(${PROJ_NAME}
${PROJ_LIB} ${Glue} ${VTK_LIBRARIES} ${ITK_LIBRARIES}
)

Linking to boost

I am trying to link to the boost libraries for a ros node, in connection I found the following links: "How to include external library (boost) into CLion C++ project with CMake?" and "How to link C++ program with Boost using CMake" and "problem building in groovy with catkin and boost".
As a result I create my CMakelists.txt as follows which needs to use OpenCV and Boost:
cmake_minimum_required(VERSION 2.8.3)
project(image_listener_rosbag)
#set(Boost_USE_STATIC_LIBS OFF)
#set(Boost_USE_MULTITHREADED ON)
#set(Boost_USE_STATIC_RUNTIME OFF)
## Find catkin macros and libraries
find_package(catkin REQUIRED COMPONENTS
cv_bridge
image_transport
message_generation
roscpp
sensor_msgs
std_msgs
)
add_message_files(
FILES
)
generate_messages(
DEPENDENCIES
std_msgs
sensor_msgs
)
catkin_package(
CATKIN_DEPENDS message_runtime
)
find_package(catkin REQUIRED)
find_package(OpenCV REQUIRED)
find_package( Boost REQUIRED COMPONENTS serialization archive ) # is this correct?
# The components included are chosen because I need to use the following usings:
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/split_free.hpp>
include_directories(
${OpenCV_INCLUDE_DIRS} ${catkin_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS}
)
link_directories(${Boost_LIBRARY_DIR})
add_library(image_listener_rosbag src/image_listener.cpp)
add_dependencies(image_listener_rosbag ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
add_executable(image_listener_rosbag_node src/image_listener.cpp)
target_link_libraries(image_listener_rosbag_node ${OpenCV_LIBRARIES} ${catkin_LIBRARIES} ${Boost_LIBRARIES})#
When I try to build it suddenly another package relying on PCL crashes with:
-- Configuring incomplete, errors occurred!
See also "/home/johann/catkin_ws/build/CMakeFiles/CMakeOutput.log".
See also "/home/johann/catkin_ws/build/CMakeFiles/CMakeError.log".
make: *** [cmake_check_build_system] Error 1
Looking into the CMakeError.log Complains about not finding pthread_create. The PCL package that crashes has no reference to boost other than what PCL has inside. Should I add boost explicitly in the failing package as above? Tried: adding the information about boost from above to the PCL node, I get the same error log referencing the missing pthread_create.
As a test I have tried editing the code above in several ways if remove all the boost references from the code above, both packages are build and runs with no issue.
If I remove find_package(Boost ... I receive a lot of issues like: undefined reference to boost::archive::detail::basic_oarchive::~basic_oarchive()` But the PCL node compiles fine with no issue.
Question: Am I adding boost correctly and the libraries?
If so is there some issue when including PCL in another package that you need a special reference?
System: For reference I am implementing it using ros indigo full, with the included opencv(2.4) and pcl(looks like 1.54 libraries), everything is on Ubuntu 14.04.
Extra
This question, "PCL install links directly to boost installation directory somehow", seems to have some of the same issues I have however not been able to transfer the suggestions yet.
PCL node
The below node is the one exhibiting the error when the boost includes are added above.
cmake_minimum_required(VERSION 2.8.3)
project(pointcloudlistener)
find_package(PCL REQUIRED)
find_package(catkin REQUIRED COMPONENTS
cv_bridge
image_transport
PCL
pcl_ros
roscpp
sensor_msgs
std_msgs
)
add_message_files(
FILES
)
generate_messages(
DEPENDENCIES
std_msgs
sensor_msgs
)
catkin_package(
CATKIN_DEPENDS message_runtime
)
find_package(catkin REQUIRED)
find_package(OpenCV REQUIRED)
find_package(PCL REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS} ${catkin_INCLUDE_DIRS} ${PCL_INCLUDE_DIRS})
link_directories(
${PCL_LIBRARY_DIRS}
)
add_library(pointcloudlistener src/pcl_listener.cpp src/matPclConverter.cpp src/Image_Safety.cpp)
add_dependencies(pointcloudlistener ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
add_executable(pointcloudlistener_node src/pcl_listener.cpp src/matPclConverter.cpp src/Image_Safety.cpp)
target_link_libraries(pointcloudlistener_node ${OpenCV_LIBRARIES} ${catkin_LIBRARIES} ${PCL_LIBRARIES})

ROS C++ catkin not finding my custom messages

The compiler is telling that the messages (.../message.h) being not found. See my CMakeLists.txt below
cmake_minimum_required(VERSION 2.8.3)
project(my_package)
add_compile_options(-std=c++11)
find_package(catkin REQUIRED
COMPONENTS cv_bridge
image_transport
roscpp
#rospy
sensor_msgs
std_msgs
message_generation
genmsg
external_package
)
find_package(nodelet REQUIRED)
#----
add_message_files( FILES
my_message1.msg
my_message2.msg
)
generate_messages( DEPENDENCIES
std_msgs sensor_msgs
)
catkin_package(
CATKIN_DEPENDS message_runtime std_msgs sensor_msgs roscpp cv_bridge image_transport
)
#****** EXTRA PACKAGES
find_package(LAPACK REQUIRED)
find_package(BLAS REQUIRED)
find_package( PkgConfig REQUIRED)
FIND_PACKAGE(Boost)
find_package( OpenCV )
include_directories(${catkin_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS} )
SET(MY_FLAG ok) # OR no!!
if(MY_FLAG)
#**** exe files
set(exefiles
file1
file2
)
foreach(file ${exefiles})
add_executable(${file} ${CMAKE_CURRENT_SOURCE_DIR}/pathToExeFiles/${file}.cpp )
endforeach(file)
#****** Lib & link
include_directories(${SRC}/pathToMyLib)
set(MY_LIB
lib1
lib2
libn
)
endif(MY_FLAG)
foreach(file ${exefiles})
target_link_libraries(${file2link}
${MY_LIB}
${MY_LIB}
${MY_LIB}
${catkin_LIBRARIES}
${Boost_LIBRARIES}
${gsl_LIBRARIES} ${LAPACK_LIBRARIES} ${BLAS_LIBRARIES}
${OpenCV_LIBRARIES}
)
add_dependencies(${file} external_package_generate_messages_cpp ${${my_package}_EXPORTED_TARGETS})
endforeach(file)
ADD_SUBDIRECTORY(src)
However, am able to compile with the following trick. I first set My_FLAG to flase and compile. Next, I set it back to true and compile again. By doing so it works fine. But... I could guess there should be a more elegant/professional/straightforward solution. Am so far not able to detect the cause of this problem. Any solution please?
I think the issue is related to the order of the dependencies... Which? No idea...
It looks like your EXPORTED_TARGETS dependency is wrong.
add_dependencies(${file} external_package_generate_messages_cpp ${${my_package}_EXPORTED_TARGETS})
The ${${my_package}_EXPORTED_TARGETS} should be ${${PROJECT_NAME}_EXPORTED_TARGETS} or ${my_package_EXPORTED_TARGETS}
so:
add_dependencies(${file} external_package_generate_messages_cpp ${${PROJECT_NAME}_EXPORTED_TARGETS})
Failing that a dependency on ${catkin_EXPORTED_TARGETS} may help.
It looks like you are not adding message generation dependency when adding executable targets within the loop
if(MY_FLAG)
#**** exe files
set(exefiles
file1
file2
)
foreach(file ${exefiles})
add_executable(${file} ${CMAKE_CURRENT_SOURCE_DIR}/pathToExeFiles/${file}.cpp )
add_dependencies(${file} external_package_generate_messages_cpp ${${my_package}_EXPORTED_TARGETS})
endforeach(file)
....
What is most likely happening in your case is, the first run with MY_FLAG=Off you generate the messages in the second foreach where you actually add the dependencies. The second run with MY_FLAG=On works because now the messages have been already generated.

undefined reference to ... when compiling ros node

I'm trying to write a ROS-Node which uses OpenCV nonfree components (SURF). I have trouble compiling the package using catkin_make:
//usr/local/lib/libopencv_nonfree.so: undefined reference to `cv::ocl::integral(cv::ocl::oclMat const&, cv::ocl::oclMat&)'
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.3)
project(homography_test)
## Find catkin macros and libraries
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
sensor_msgs
cv_bridge
image_transport
)
catkin_package(
)
###########
## Build ##
###########
## Specify additional locations of header files
## Your package locations should be listed before other locations
# include_directories(include)
include_directories(
${catkin_INCLUDE_DIRS}
/usr/local/include
/usr/include
/usr/include/gstreamer-1.0
/usr/include/glib-2.0
/usr/lib/x86_64-linux-gnu/glib-2.0/include
)
## Declare a cpp executable
#add_executable(display_image src/display_image_node.cpp)
add_executable(homography src/sample.cpp)
## Add cmake target dependencies of the executable/library
## as an example, message headers may need to be generated before nodes
#add_dependencies(homography_test homography_test_generate_messages_cpp)
## Specify libraries to link a library or executable target against
target_link_libraries(
homography
${catkin_LIBRARIES}
gstreamer-1.0
gobject-2.0
glib-2.0
opencv_nonfree
opencv_calib3d
opencv_contrib
opencv_core
opencv_features2d
opencv_flann
opencv_gpu
opencv_highgui
opencv_imgproc
opencv_legacy
opencv_ml
opencv_objdetect
opencv_ocl
opencv_photo
opencv_stitching
opencv_superres
opencv_ts
opencv_video
opencv_videostab
rt
pthread
m
dl
)
link_directories(/usr/local/lib)
But if I manually compile the same code, but not as ROS-node, everything works.
It seems that the opencv_ocl library is not being linked properly. Also, you can simplify your CMakeLists.txt file a lot by using find_package to get your OpenCV configuration. Try adding those lines to your CMakeLists.txt file:
find_package(OpenCV REQUIRED core ocl)
include_directories(SYSTEM ${OpenCV_INCLUDE_DIRS})
target_link_libraries(homography ${catkin_LIBRARIES} ${OpenCV_LIBRARIES} )
You need to specify which OpenCV libraries you want to include in the find_package. I listed only core and ocl since ocl seems to be what you need, but others might be needed as well.