# How to traverse \`target\_link\_libraries\` dependencies in the file api?

**URL:** https://discourse.cmake.org/t/how-to-traverse-target-link-libraries-dependencies-in-the-file-api/14384
**Category:** Code
**Created:** [July 16, 2025, 11:27pm UTC](https://discourse.cmake.org/t/how-to-traverse-target-link-libraries-dependencies-in-the-file-api/14384 "2025-07-16T23:27:23Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Zachary\_Turner](https://discourse.cmake.org/user_avatar/discourse.cmake.org/zachary_turner/32/1551_2.png) [@Zachary\_Turner](https://discourse.cmake.org/u/Zachary_Turner)
#### Post date: [July 16, 2025, 11:27pm UTC](https://discourse.cmake.org/t/how-to-traverse-target-link-libraries-dependencies-in-the-file-api/14384/1 "2025-07-16T23:27:23Z")

</div>

I’m using cmake 3.26.3. I have three targets A, B, and C. And I have `target_link_libraries(A PRIVATE B PUBLIC C)`. I use the file api and traverse to the codemodel object for target `A`, and it’s `dependencies` field is completely missing. It’s not even set to an empty list, the key just isn’t present in the codemodel target object. If I look at the `includes` field for the compileGroups, then I do see lots of entries like:

```auto
{
    "backtrace" : 41,
    "path" : "some-include-dir"
},

```

And backtrace 41 points to a call to `target_link_libraries`. How do I get a target id from this? Reading the documentation it looks like it’s supposed to be in the `dependencies` field, but it isn’t.

---

<div class="post-metadata">

### Author: ![Zachary\_Turner](https://discourse.cmake.org/user_avatar/discourse.cmake.org/zachary_turner/32/1551_2.png) [@Zachary\_Turner](https://discourse.cmake.org/u/Zachary_Turner)
#### Post date: [July 17, 2025, 6:55pm UTC](https://discourse.cmake.org/t/how-to-traverse-target-link-libraries-dependencies-in-the-file-api/14384/2 "2025-07-17T18:55:36Z")

</div>

Solved my own problem. If you have `CMAKE_OPTIMIZE_DEPENDENCIES=ON`, then dependencies are not represented in the cmake file api output. I feel like this should be documented, it’s pretty subtle and definitely not obvious.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [July 18, 2025, 6:31am UTC](https://discourse.cmake.org/t/how-to-traverse-target-link-libraries-dependencies-in-the-file-api/14384/3 "2025-07-18T06:31:20Z")

</div>

That sounds like a bug to me. Would you mind reporting it in [CMake’s issue tracker](https://gitlab.kitware.com/cmake/cmake/-/issues/new)?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [February 20, 2026, 4:48am UTC](https://discourse.cmake.org/t/how-to-traverse-target-link-libraries-dependencies-in-the-file-api/14384/4 "2026-02-20T04:48:32Z")

</div>

I reported it here: [https://gitlab.kitware.com/cmake/cmake/-/issues/27623](https://gitlab.kitware.com/cmake/cmake/-/issues/27623)

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [February 21, 2026, 12:16am UTC](https://discourse.cmake.org/t/how-to-traverse-target-link-libraries-dependencies-in-the-file-api/14384/5 "2026-02-21T00:16:35Z")

</div>

The problem is that the `dependencies` array isn’t what most people expect it to be. It is not a complete view of all the different types of dependencies. It is not suitable for representing all the ways targets can depend on each other.

CMake 4.2 added file API codemodel version 2.9, which added new `linkLibraries`, `interfaceLinkLibraries`, `compileDependencies`, `interfaceCompileDependencies`, `objectDependencies`, and `orderDependencies` fields. Those new fields do properly capture the dependency relationships.
