# Best Practices for Replacing ament\_target\_dependencies in Kilted While Maintaining Compatibility

**URL:** <https://discourse.openrobotics.org/t/best-practices-for-replacing-ament-target-dependencies-in-kilted-while-maintaining-compatibility/43938>\
**Category:** Packaging and Release Management\
**Tags:** cmake, kilted, ament\_cmake\
**Created:** [May 26, 2025, 11:18pm UTC](https://discourse.openrobotics.org/t/best-practices-for-replacing-ament-target-dependencies-in-kilted-while-maintaining-compatibility/43938 "2025-05-26T23:18:49Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![sloretz](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/sloretz/32/23113_2.png) [@sloretz](https://discourse.openrobotics.org/u/sloretz)\
**Post date:** [May 28, 2025, 12:04am UTC](https://discourse.openrobotics.org/t/best-practices-for-replacing-ament-target-dependencies-in-kilted-while-maintaining-compatibility/43938/4 "2025-05-28T00:04:38Z")

</div>

In short:

> Is it advisable to replace ament\_target\_dependencies with target\_link\_libraries in patch releases for Kilted?

Yes it is advisable.

> Or should we leave existing Kilted packages as-is and only apply the fix in rolling or future releases?

You should apply fixes to Kilted. It’s probably fine to apply fixes as far back as ROS Foxy.

> Does replacing ament\_target\_dependencies with target\_link\_libraries impact downstream users in a way that might break their builds or linkage?

AFAIK, your use of `ament_target_dependencies` versus `target_link_libraries` does not impact downstream users.

> Since which distro is it safe to substitute ament\_target\_dependencies?

Foxy-ish

* * *

In long…

## The problem: build some software

Let me start with a short story.  
Some dude has a top-secret high-impact C++ project. It’s is going to change the world. Here it is:

```c++
# include <tinyxml2.h>
# include <iostream>

int main (int argc, char **argv) {
  if (argc != 2) {
    std::cerr << "Usage " << argv[0] << " some_world.sdf\n";
    return 1;
  }
  tinyxml2::XMLDocument doc;
  tinyxml2::XMLError result = doc.LoadFile(argv[1]);
  tinyxml2::XMLElement* root = doc.RootElement();
  if (!root) {
    std::cerr << "Invalid SDFormat\n";
    return 1;
  }
  tinyxml2::XMLElement* world = root->FirstChildElement("world");
  if (!world) {
    std::cerr << "Invalid SDFormat world\n";
    return 1;
  }
  world->SetAttribute("name", "Shane's world");
  doc->Print();
}

```

Some dude compiles it with:

```auto
g++ world_domination.cpp -I /usr/include -L/usr/lib/x86_64-linux-gnu -ltinyxml2 -std=c++17 -oworld_domination

```

However, this only builds on Ubuntu. Any serious world domination effort is going to need cross platform support, at least until the year of the linux desktop arrives.

Some dude wonders if world domination would be easier with CMake.

```CMake
cmake_minimum_required(VERSION 3.15)
project(world_domination VERSION 1.0.0)

find_package(tinyxml2 REQUIRED)

add_executable(world_domination world_domination.cpp)
target_link_libraries(world_domination tinyxml2::tinyxml2)

```

```console
mkdir build
cd build
cmake ..
cmake --build .

```

Great. It’s a bit more complex to use CMake, but cross-platfom world domination makes it worth it.  
The world is conquered and no one ever has any problems again.

**The end**

My point is that CMake’s job is compile your software.  
CMake needs to give the compiler:

- Include directories, like `/usr/include`
- Link directories, like `/usr/lib/x86_64-linux-gnu`
- Libraries to link against, like `tinyxml2`
- Misc compiler flags, like `-std=c++17`

So, how does CMake know what to give to the compiler?

## The solution: modern CMake targets

This line tells CMake `world_domination` depends on `tinyxml2`.

```CMake
target_link_libraries(world_domination tinyxml2::tinyxml2)

```

The text `tinyxml2::tinyxml2` is the name of a CMake target. CMake targets have [target properties](https://cmake.org/cmake/help/latest/manual/cmake-properties.7.html). Some of `tinyxml2::tinyxml2`’s properties tell CMake what it needs to build `world_domination`. For example:

- [INTERFACE\_INCLUDE\_DIRECTORIES](https://cmake.org/cmake/help/latest/prop_tgt/INTERFACE_INCLUDE_DIRECTORIES.html) tells CMake what directories should be searched for headers, like `/usr/include`
- [INTERFACE\_LINK\_DIRECTORIES](https://cmake.org/cmake/help/latest/prop_tgt/INTERFACE_LINK_DIRECTORIES.html) tells CMake what directories should be searched for libraries, like `/usr/lib/x86_64-linux-gnu`
- [INTERFACE\_LINK\_LIBRARIES](https://cmake.org/cmake/help/latest/prop_tgt/INTERFACE_LINK_LIBRARIES.html) tells CMake what libraries should be linked against, like `tinyxml2`
- [INTERFACE\_COMPILE\_OPTIONS](https://cmake.org/cmake/help/latest/prop_tgt/INTERFACE_COMPILE_OPTIONS.html) tells CMake what compiler flags should be passed, like `-std=c++17` (I think you should use [compile features](https://cmake.org/cmake/help/latest/prop_gbl/CMAKE_CXX_KNOWN_FEATURES.html#prop_gbl:CMAKE_CXX_KNOWN_FEATURES) for the C++ standard, but that’s not relevant to this post)

When you call `target_link_libraries()` with CMake targets, you’re telling CMake to use all the information provided by those targets to build your software.

## The old solution: CMake variables

Long ago in a galaxy far far away, CMake expected you store this information in CMake variables.  
These variables had [conventional names that most-ish packages used](https://cmake.org/cmake/help/book/mastering-cmake/chapter/Finding%20Packages.html#built-in-find-modules). tinyxml2 might have put that info into variables like:

- `tinyxml2_INCLUDE_DIRS` for include directories, like `/usr/include`
- `tinyxml2_LIBRARIES` with full paths to libraries like `/usr/lib/x86_64-linux-gnu/libtinyxml2.so`
- `tinyxml2_HECK_IF_I_KNOW` for misc compiler flags, like `-std=c++17`

You might tell CMake about those variables with calls like

```CMake
target_include_directories(world_domination PRIVATE ${tinyxml2_INCLUDE_DIRS})
target_link_libraries(world_domination PRIVATE ${tinyxml2_LIBRARIES})
target_compile_options(world_domination PRIVATE ${tinyxml2_HECK_IF_I_KNOW})

```

Or, maybe you’d give CMake that information with even older calls like:

- [include\_directories()](https://cmake.org/cmake/help/latest/command/include_directories.html)
- [link\_libraries()](https://cmake.org/cmake/help/latest/command/link_libraries.html)
- [setting CMAKE\_CXX\_FLAGS](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS.html#variable:CMAKE_%3CLANG%3E_FLAGS)

Old-style CMake variables have some problems, but I’m going to resist explaining them.

## Weren’t we talking about ament\_target\_dependencies?

So `ament_target_dependencies()` did two things:

- It tried to make it convenient to depend on packages that export old-style CMake variables
- It tried to support [overriding packages like Catkin](https://colcon.readthedocs.io/en/released/user/overriding-packages.html)

It was supposed to be convenient to use `ament_target_dependencies()`. However, not all packages used the same “conventional names”, and so couldn’t be used with `ament_target_dependencies()`. Then it got worse when our dependencies stopped providing old-style CMake variables. We tried to work around this, [even creating our own wrappers to export old-style CMake variables](https://github.com/ros2/eigen3_cmake_module). Then we made `ament_target_dependencies()` [support modern CMake targets if the targets were put into a CMake variable called `*_INTERFACES`](https://github.com/ament/ament_cmake/pull/232). However, the `_INTERFACES` was just a ROS convention. Our dependencies didn’t have to provide a variable with that name. By ROS Foxy we made most ROS packages export modern CMake targets too.

Catkin tried to make it possible to [override packages](https://colcon.readthedocs.io/en/released/user/overriding-packages.html). If the installed version of `roscpp` had a bug, then you could put a newer version of `roscpp` into your workspace and use that. Catkin made this possible by carefully sorting include directories before giving them to the compiler. All you had to do was find those dependencies with `find_package(catkin REQUIRED COMPONENTS foo bar etc)`. **Frankly overriding packages doesn’t work, not even in ROS 1, except in very fragile circumstances**. Still, `ament_target_dependencies()` tried to support it by re-ordering include directories according to ament and colcon workspaces. Then we discovered [modern CMake targets took away our ability to control the order of information passed to the compiler from transitive dependencies](https://github.com/ros2/ros2/issues/1150). We solved that in ROS Humble by installing headers into unique include directories. This made the sorting in `ament_target_dependencies()` unnecessary.

In summary, chances are the `target_link_libraries()` call in `ament_target_dependencies()` has been the only thing doing useful work for some time now. The rest was just making your build slower.

---

_[View the full topic](https://discourse.openrobotics.org/t/best-practices-for-replacing-ament-target-dependencies-in-kilted-while-maintaining-compatibility/43938)._
