ROS2-4001 ros2 build_error ai_generated true

CMake 错误在 CMakeLists.txt:25 (ament_target_dependencies):ament_target_dependencies() 调用了未知的目标 'my_library'

CMake Error at CMakeLists.txt:25 (ament_target_dependencies): ament_target_dependencies() called with unknown target 'my_library'

ID: ros2/ros2-ament-cmake-export-dependencies-missing

其他格式: JSON · Markdown 中文 · English
95%修复率
90%置信度
1证据数
2023-07-22首次发现

版本兼容性

版本状态引入弃用备注
ros2:humble active
ros2:iron active
ament_cmake:1.5.0 active
cmake:3.22.0 active

根因分析

ament_target_dependencies() 引用了一个在调用前未通过 add_library() 或 add_executable() 定义的目标,通常由于缺少或顺序错误的 CMake 命令。

English

ament_target_dependencies() references a target that was not defined with add_library() or add_executable() before the call, typically due to missing or misordered CMake commands.

generic

官方文档

https://docs.ros.org/en/humble/How-To-Guides/Ament-CMake-Documentation.html

解决方案

  1. 确保在 ament_target_dependencies 之前定义目标。正确顺序示例:
    add_library(my_library SHARED src/my_library.cpp)
    ament_target_dependencies(my_library rclcpp std_msgs)
    
    如果目标在子目录中,请在引用前使用 add_subdirectory。
  2. 如果目标通过 add_subdirectory 在另一个 CMakeLists.txt 中定义,请确保目标名称完全匹配。验证:
    cmake --build . --target my_library  # 检查目标是否存在

无效尝试

常见但无效的做法:

  1. Adding ament_target_dependencies before add_library in the same CMakeLists.txt 90% 失败

    The target must be defined before dependencies can be added; reordering does not fix the missing target definition.

  2. Removing all ament_target_dependencies calls and using target_link_libraries directly 60% 失败

    This bypasses ament's dependency handling and may cause missing include paths or compile errors.