You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ROS编译包时OpenCV函数未定义,如何为catkin build添加pkg-config参数?

Fixing OpenCV Undefined Reference Errors in Catkin Build

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:

  1. Find the pkg-config module and OpenCV
    Add this after your find_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 opencv with opencv3 in the pkg_check_modules line if your .pc file is named opencv3.pc.

  2. Add OpenCV header directories
    Update your include_directories section to include OpenCV’s headers alongside ROS/catkin headers:

    include_directories(
      ${catkin_INCLUDE_DIRS}
      ${OpenCV_INCLUDE_DIRS}
    )
    
  3. Link OpenCV libraries to your executable
    When defining your executable, add ${OpenCV_LIBRARIES} to the target_link_libraries command. 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 --libs in your terminal—if it returns valid paths, you’re good. If not, add your OpenCV 3.1 pkgconfig directory to the PKG_CONFIG_PATH environment variable:
    export PKG_CONFIG_PATH=/path/to/your/opencv-3.1.0/lib/pkgconfig:$PKG_CONFIG_PATH
    
    Make this permanent by adding it to your ~/.bashrc file if needed.
  • Update package.xml: Add OpenCV as a dependency to your package’s package.xml to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:09:10