user3053231
user3053231

Reputation:

Compilation of OpenCV using CMakeList under ROS

I am trying to compile openCV code using CmakeList.txt (under ROS (Robot operating system)), my CmakeList is working, because on another PC it is working well. I installed OpenCV and in openCV examples directory I compiled examples using g++ and some flags, compilation was successful. But when I want to compile my other code using CmakeList, I get this error:

CMakeFiles/aupark_node.dir/src/wrapper.cpp.o: In function `Wrapper::set_head_image(std::string)':
wrapper.cpp:(.text+0x2ef1): undefined reference to `cv::imread(cv::String const&, int)'

in wrapper.cpp I had all appropriate includes.

#include <opencv2/opencv.hpp>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>

And my CMakeList.txt is here:

cmake_minimum_required(VERSION 2.8.3)
project(aupark)

set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
find_package(catkin REQUIRED COMPONENTS cmake_modules roscpp gencpp moveit_ros_planning_interface moveit_msgs cv_bridge)
find_package(Eigen REQUIRED)
include_directories(${EIGEN_INCLUDE_DIRS})
add_definitions(${EIGEN_DEFINITIONS})

catkin_package()

add_executable(aupark_node src/aupark_node.cpp src/wrapper.cpp src/wrapper.h)
target_link_libraries(aupark_node ${catkin_LIBRARIES})

What can be wrong?

output of pkg-config opencv --libs:

-L/usr/local/lib
-lopencv_shape 
-lopencv_stitching 
-lopencv_objdetect 
-lopencv_superres  
-lopencv_videostab 
-lopencv_calib3d 
-lopencv_features2d  
-lopencv_highgui 
-lopencv_videoio 
-lopencv_imgcodecs 
-lopencv_video 
-lopencv_photo 
-lopencv_ml 
-lopencv_imgproc 
-lopencv_flann 
-lopencv_viz 
-lopencv_core 
-lopencv_hal 

Upvotes: 3

Views: 6964

Answers (1)

luator
luator

Reputation: 5019

You are not linking against OpenCV in your CMakeLists.txt.

To do so add find_package(OpenCV REQUIRED) at the top and then link your target against it:

target_link_libraries(aupark_node ${catkin_LIBRARIES} ${OpenCV_LIBS})

A minimal example on how to use OpenCV with cmake can be found in the OpenCV documentation.

Upvotes: 7

Related Questions