ROS2-4001 ros2 build_error ai_generated true

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

Also available as: JSON · Markdown · 中文
95%Fix Rate
90%Confidence
1Evidence
2023-07-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
ros2:humble active
ros2:iron active
ament_cmake:1.5.0 active
cmake:3.22.0 active

Root Cause

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

中文

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

Official Documentation

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

Workarounds

  1. 95% success Ensure the target is defined before ament_target_dependencies. Example correct order: add_library(my_library SHARED src/my_library.cpp) ament_target_dependencies(my_library rclcpp std_msgs) If the target is in a subdirectory, use add_subdirectory before referencing it.
    Ensure the target is defined before ament_target_dependencies. Example correct order:
    add_library(my_library SHARED src/my_library.cpp)
    ament_target_dependencies(my_library rclcpp std_msgs)
    
    If the target is in a subdirectory, use add_subdirectory before referencing it.
  2. 90% success If the target is defined in another CMakeLists.txt via add_subdirectory, ensure the target name matches exactly. Verify with: cmake --build . --target my_library # Check if target exists
    If the target is defined in another CMakeLists.txt via add_subdirectory, ensure the target name matches exactly. Verify with:
    cmake --build . --target my_library  # Check if target exists

中文步骤

  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  # 检查目标是否存在

Dead Ends

Common approaches that don't work:

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

    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% fail

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