# 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`
- **Domain:** ros2
- **Category:** build_error
- **Error Code:** `ROS2-4001`
- **Verification:** ai_generated
- **Fix Rate:** 95%

## 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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| ros2:humble | active | — | — |
| ros2:iron | active | — | — |
| ament_cmake:1.5.0 | active | — | — |
| cmake:3.22.0 | active | — | — |

## Workarounds

1. **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.** (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.
   ```
2. **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** (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
   ```

## Dead Ends

- **Adding ament_target_dependencies before add_library in the same CMakeLists.txt** — The target must be defined before dependencies can be added; reordering does not fix the missing target definition. (90% fail)
- **Removing all ament_target_dependencies calls and using target_link_libraries directly** — This bypasses ament's dependency handling and may cause missing include paths or compile errors. (60% fail)
