ROS编译包时OpenCV函数未定义,如何为catkin build添加pkg-config参数?
I’ve run into this exact issue before when working with ROS and custom OpenCV versions—let’s break down how to get pkg-config opencv --cflags --libs integrated into your catkin build pipeline, and why you’re seeing those frustrating undefined reference errors.
The Root Cause
When you compile with g++ test.cpp $(pkg-config opencv --cflags --libs), you’re explicitly telling the compiler where to find OpenCV’s header files (--cflags) and which libraries to link against (--libs). Catkin uses CMake under the hood, so it won’t automatically pick up these settings unless you configure your CMakeLists.txt properly. That’s why you get the undefined reference to cv::imread(...) error—CMake isn’t linking the OpenCV libraries to your executable.
Solution 1: Integrate pkg-config into CMakeLists.txt
This method directly mirrors the pkg-config command you’re already using successfully with g++. Here’s how to update your package’s CMakeLists.txt:
Find the pkg-config module and OpenCV
Add this after yourfind_package(catkin REQUIRED COMPONENTS ...)block:# Ensure pkg-config is available for dependency lookup find_package(PkgConfig REQUIRED) # Use pkg-config to locate your OpenCV installation pkg_check_modules(OpenCV REQUIRED opencv)Note: If your system has multiple OpenCV versions, replace
opencvwithopencv3in thepkg_check_modulesline if your .pc file is namedopencv3.pc.Add OpenCV header directories
Update yourinclude_directoriessection to include OpenCV’s headers alongside ROS/catkin headers:include_directories( ${catkin_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS} )Link OpenCV libraries to your executable
When defining your executable, add${OpenCV_LIBRARIES}to thetarget_link_librariescommand. For example:add_executable(your_ros_node src/your_node.cpp) target_link_libraries(your_ros_node ${catkin_LIBRARIES} ${OpenCV_LIBRARIES} )
Solution 2: Use ROS-native OpenCV Find Package
If you prefer a more ROS-idiomatic approach (which often plays nicer with other ROS packages), you can use CMake’s built-in find_package for OpenCV. This works well if you have OpenCV 3.1 properly installed on your system:
# Explicitly find OpenCV 3.1 to avoid version conflicts find_package(OpenCV 3.1 REQUIRED) # Add include directories and link libraries the same way as above include_directories( ${catkin_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS} ) target_link_libraries(your_ros_node ${catkin_LIBRARIES} ${OpenCV_LIBRARIES} )
Additional Tips for TIAGo Simulation Workspace
- Verify pkg-config can find OpenCV: Run
pkg-config opencv --cflags --libsin your terminal—if it returns valid paths, you’re good. If not, add your OpenCV 3.1pkgconfigdirectory to thePKG_CONFIG_PATHenvironment variable:
Make this permanent by adding it to yourexport PKG_CONFIG_PATH=/path/to/your/opencv-3.1.0/lib/pkgconfig:$PKG_CONFIG_PATH~/.bashrcfile if needed. - Update package.xml: Add OpenCV as a dependency to your package’s
package.xmlto ensure catkin handles it correctly during builds:<build_depend>opencv</build_depend> <exec_depend>opencv</exec_depend>
After making these changes, clean your catkin workspace and rebuild to apply the fixes:
catkin clean catkin build
This should resolve the undefined reference errors and get your OpenCV-dependent ROS nodes compiling correctly with the TIAGo simulation workspace.
内容的提问来源于stack exchange,提问作者Daan van O

