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

ROS环境下CMake编写单元测试遇链接错误求助

Fixing Linker Errors (-lprime_tables and -lsample1 Not Found) in ROS Catkin Unit Tests

Hey there! I’ve run into this exact kind of linker issue when setting up unit tests in ROS projects, so let’s break down what’s going wrong and how to fix it.

Why You’re Seeing This Error

The linker (/usr/bin/ld) can’t find libprime_tables.so or libsample1.so (or their static equivalents) because:

  • These libraries aren’t being compiled from your src directory code at all.
  • Your CMake configuration isn’t telling the linker where to look for the libraries, or isn’t properly associating your test targets with the libraries.
  • The test targets aren’t inheriting the necessary dependencies from the library builds.

Step-by-Step Solutions

Let’s walk through fixing this with proper Catkin/CMake practices:

1. Compile Your Core Code as Libraries

First, make sure the code in your src directory (the logic for prime_tables and sample1) is being compiled into reusable libraries. Open your project’s root CMakeLists.txt and add this:

# Compile prime_tables into a library (static or shared)
add_library(prime_tables src/prime_tables.cpp)
# Link any required ROS dependencies (e.g., roscpp)
target_link_libraries(prime_tables ${catkin_LIBRARIES})
# Export include paths so tests can find your headers
target_include_directories(prime_tables PUBLIC
  ${PROJECT_SOURCE_DIR}/include
  ${catkin_INCLUDE_DIRS}
)

# Repeat for sample1
add_library(sample1 src/sample1.cpp)
target_link_libraries(sample1 ${catkin_LIBRARIES})
target_include_directories(sample1 PUBLIC
  ${PROJECT_SOURCE_DIR}/include
  ${catkin_INCLUDE_DIRS}
)

This tells CMake to build your core logic into libraries that your tests can link against.

2. Hook Up Your Tests Directory

Make sure your root CMakeLists.txt enables testing and includes the tests directory:

# Enable Catkin testing framework
if(CATKIN_ENABLE_TESTING)
  # Add the tests subdirectory so its CMake rules are processed
  add_subdirectory(tests)
endif()

Now open tests/CMakeLists.txt and set up your test executable to link against the libraries you just built. For example, if your test file is test_sample1.cpp:

# Create the test executable
add_executable(test_sample1 test_sample1.cpp)

# Link your custom libraries, ROS dependencies, and testing frameworks (like GTest)
target_link_libraries(test_sample1
  prime_tables
  sample1
  ${catkin_LIBRARIES}
  ${GTEST_LIBRARIES}
)

# Register the test with Catkin's rostest system (if using launch files)
add_rostest_gtest(test_sample1 test_sample1.launch test_sample1.cpp)

The key here is explicitly listing prime_tables and sample1 in target_link_libraries—this tells the linker exactly which libraries to use.

4. Final Checks

  • Source Your Workspace: After running catkin_make or catkin build, don’t forget to source the devel setup file:
    source ~/catkin_ws/devel/setup.bash
    
    This ensures your system can find the compiled libraries when running tests.
  • Avoid Old CMake Habits: Skip commands like link_libraries or link_directories—modern CMake (and Catkin) prefers target_link_libraries and target_include_directories because they properly handle dependency inheritance.

Quick Troubleshooting

If you still get errors:

  • Double-check that your src directory has the correct .cpp files for prime_tables and sample1.
  • Verify that your test code includes the headers correctly (e.g., #include "project1/prime_tables.h").
  • Check the catkin_ws/devel/lib/project1 directory after building—you should see libprime_tables.so and libsample1.so there.

内容的提问来源于stack exchange,提问作者Mat

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 22:32:48