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})
Related
I want to use opencv-4.4.0 in my ROS program, and I found that to do this I have to compile cv_bridge from source with current opencv version, since cv_bridge shipped from ROS only support opencv3. After some searching I found this which I think is customized to compatible with opencv4:
https://github.com/fizyr-forks/vision_opencv/tree/opencv4
after catkin build and changing include path in my program, this error occurred:
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/imgproc/src/color.cpp, line 9716
terminate called after throwing an instance of 'cv::Exception'
what(): /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/imgproc/src/color.cpp:9716: error: (-215) scn == 3 || scn == 4 in function cvtColor
This error is from cv::imshow() after cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::MONO8); It doesn't really matter because I found after changing the last
argument MONO8 to BGR8, imshow was able to work. So I think I can use BGR8 then change it to grey scale using opencv function later. However from this error message I realized cv_bridge is still trying to use opencv-3.2 instead of opencv-4.4.0. Please help me figure it out how to compile it with correct opencv version! Here's my CMakeLists.txt and package.xml. I changed them a little as original files didn't work for me properly(so as mine).
I'm sorry if they are hard to read.
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8) project(cv_bridge)
find_package(catkin REQUIRED COMPONENTS rosconsole sensor_msgs)
if(NOT ANDROID) find_package(PythonLibs)
if(PYTHONLIBS_VERSION_STRING VERSION_LESS 3)
find_package(Boost REQUIRED python) else()
find_package(Boost REQUIRED python3) endif() else() find_package(Boost REQUIRED) endif()
find_package(OpenCV 4.4.0 REQUIRED COMPONENTS
opencv_core
opencv_imgproc
opencv_imgcodecs CONFIG ) set(OpenCV_DIR=/usr/local/lib/cmake/opencv4)
catkin_package( INCLUDE_DIRS include LIBRARIES ${PROJECT_NAME}
CATKIN_DEPENDS rosconsole sensor_msgs DEPENDS OpenCV CFG_EXTRAS
cv_bridge-extras.cmake )
catkin_python_setup()
include_directories(include ${Boost_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS} ${catkin_INCLUDE_DIRS})
if(NOT ANDROID) add_subdirectory(python) endif() add_subdirectory(src)
if(CATKIN_ENABLE_TESTING) add_subdirectory(test) endif()
install( DIRECTORY include/${PROJECT_NAME}/ DESTINATION
${CATKIN_PACKAGE_INCLUDE_DESTINATION} )
package.xml:
<package format="2"> <name>cv_bridge</name>
<version>1.13.0</version> <description>
This contains CvBridge, which converts between ROS
Image messages and OpenCV images. </description> <author>Patrick Mihelich</author> <author>James Bowman</author>
<maintainer email="vincent.rabaud#gmail.com">Vincent
Rabaud</maintainer> <license>BSD</license> <url
type="website">http://www.ros.org/wiki/cv_bridge</url> <url
type="repository">https://github.com/ros-perception/vision_opencv</url>
<urlI'm sorry if it's hard to read.
type="bugtracker">https://github.com/ros-perception/vision_opencv/issues</url>
<export>
<rosdoc config="rosdoc.yaml" /> </export>
<buildtool_depend>catkin</buildtool_depend>
<build_depend>boost</build_depend>
<build_depend>opencv2</build_depend>
<build_depend>python</build_depend>
<build_depend>python-opencv</build_depend>
<build_depend>rosconsole</build_depend>
<build_depend>sensor_msgs</build_depend>
<exec_depend>boost</exec_depend>
<exec_depend>opencv2</exec_depend> <exec_depend>python</exec_depend>
<exec_depend>python-opencv</exec_depend>
<exec_depend>rosconsole</exec_depend>
<build_export_depend>opencv2</build_export_depend>
<build_export_depend>sensor_msgs</build_export_depend>
<test_depend>rostest</test_depend>
<test_depend>python-numpy</test_depend>
<doc_depend>dvipng</doc_depend> </package>
update:
I insert a line of code to print out opencv version in cv_bridge.cpp. And the result is 4.4.0. Does this mean it's using the correct version of opencv and the upper error message is from another package? I checked both cv_bridge and my own program that are both using opencv 4.4.0 as I included. I really run out of idea. Here is what I included:
#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include </home/robotics/catkin_ws/src/vision_opencv-opencv4/cv_bridge/include/cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>
#include <opencv4/opencv2/imgproc/imgproc.hpp>
#include <opencv4/opencv2/highgui/highgui.hpp>
#include <iostream>
I'm answering my own question cause I finally found a solution! According to this post,I found that my CMakeLists.txt of custmized cv_bridge didn't have set (CMAKE_CXX_STANDARD 11). So I added it and catkin build,then try my program and every thing fixed! Here's my whole CMakelists:
cmake_minimum_required(VERSION 2.8)
set (CMAKE_CXX_STANDARD 11)
project(cv_bridge)
find_package(catkin REQUIRED COMPONENTS rosconsole sensor_msgs)
if(NOT ANDROID)
find_package(PythonLibs)
if(PYTHONLIBS_VERSION_STRING VERSION_LESS 3)
find_package(Boost REQUIRED python)
else()
find_package(Boost REQUIRED python3)
endif()
else()
find_package(Boost REQUIRED)
endif()
set(CV_MAJOR_VERSION=4.4.0)
set(OpenCV_DIR=/usr/local/lib/cmake/opencv4)
find_package(OpenCV 4.4.0 REQUIRED
COMPONENTS
core
imgproc
imgcodecs
CONFIG
)
catkin_package(
INCLUDE_DIRS include
LIBRARIES ${PROJECT_NAME}
CATKIN_DEPENDS rosconsole sensor_msgs
DEPENDS OpenCV
CFG_EXTRAS cv_bridge-extras.cmake
)
catkin_python_setup()
include_directories(include ${Boost_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS} ${catkin_INCLUDE_DIRS})
if(NOT ANDROID)
add_subdirectory(python)
endif()
add_subdirectory(src)
if(CATKIN_ENABLE_TESTING)
add_subdirectory(test)
endif()
# install the include folder
install(
DIRECTORY include/${PROJECT_NAME}/
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
)
`I am simply trying to compile a program in ROS . I already linked all the necessary libraries but this one I can't find. All I am getting is the following error in codeblocks (also in eclipse).
Anybody know how to get rid of this error? Or even which library it require?
cmake_minimum_required(VERSION 2.8.3)
project(gpuvoxelgetpointcloud)
find_package(catkin REQUIRED COMPONENTS
cmake_modules
roscpp
rospy
std_msgs
sensor_msgs
cv_bridge
image_transport
pcl_conversions
pcl_ros
)
find_package(icl_core REQUIRED )
find_package(gpu_voxels REQUIRED)
find_package(Boost COMPONENTS system REQUIRED)
find_package(orocos_kdl REQUIRED)
find_package(CUDA REQUIRED)
find_package(PCL 1.7 REQUIRED)
#include <pcl_ros/transforms.h>
#include <pcl/conversions.h>
#include <pcl/PCLPointCloud2.h>
Error is:
undefined reference to symbol
'_ZN3pcl7console5printENS0_15VERBOSITY_LEVELEPKcz'
//use/lib/aarch64_linux-gnu/ libpcl-common.so.1.7: error adding symbols:DSO missing from command line collect2:ld returned 1 exit status.
I am using plc on TX2 and maybe there is something wrong with plc on arm.
Try adding the followings to your CMakeLists.txt:
find_package(PCL 1.7 REQUIRED)
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
include_directories(
${PCL_INCLUDE_DIRS}
)
Also, link ${PCL_LIBRARIES} to your targets. Include the necessary header files in your cpp codes.
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.
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.
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.