# Object File Propragating: CMake Debug Help

**URL:** https://discourse.cmake.org/t/object-file-propragating-cmake-debug-help/6874
**Category:** Usage
**Tags:** os:linux, gen:makefiles, gen:ninja
**Created:** [November 15, 2022, 8:31pm UTC](https://discourse.cmake.org/t/object-file-propragating-cmake-debug-help/6874 "2022-11-15T20:31:32Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Drue\_Peters](https://discourse.cmake.org/user_avatar/discourse.cmake.org/drue_peters/32/2629_2.png) [@Drue\_Peters](https://discourse.cmake.org/u/Drue_Peters)
#### Post date: [November 15, 2022, 8:31pm UTC](https://discourse.cmake.org/t/object-file-propragating-cmake-debug-help/6874/1 "2022-11-15T20:31:32Z")

</div>

We have a few core libraries that are really large as they’re used for modeling. If we need to add support for a new thing to model, then a small team will build modeling support for that thing. This was a great use for object libraries. But, there are three hierarchies.

Let’s say we’re modeling vehicles for a game simulation:

```auto
add_library(Vehicle ${vehicle_sources})
add_library(Ford OBJECT ${ford_sources})
add_library(FordFocus OBJECT ${fordfocus_sources})

target_sources(Ford PRIVATE $<TARGET_OBJECTS:FordFocus >)

target_link_library(Vehicle PUBLIC Ford)

```

With this approach, I expect the object files from FordFocus and Ford to both be in the archive for `Vehicle`. For some reason that I can’t figure out, these object file sources are being propagated above `Vehicle`. It doesn’t happen on every target that links to `Vehicle` but on some. I would imagine the likely cause is a messed up rule I’m having a hard time tracking down.

What is a good method for trying to debug something like this?  
The issue isn’t duplicate symbols, but incredibly long linking commands where some targets are trying to link the object files that shouldn’t be available in that form after `Vehicle`. Ninja and Make both are failing on this. Eventually, the command becomes `&& clang ++ -g ...`,

In a similar note, I’ve tried printing the LINKED\_LIBRARIES properties from a few different targets, but it fails in Ninja. Some libraries have generator expressions as part of their `LINKED_LIBRARIES` which has two effects in both Ninja and Make. The common failure is that some expressions aren’t evaluated. So you might get `TARGET_PROPERTY:FordFocus,custom_proeprty>>;FordFocus.../`. In these cases, Ninja complains about $ not being correctly escaped: `ninja: error: build.ninja:35038: bad $-escape (literal $ must be written as $$)`

```auto
function(print_target_linked_libraries Target)
    set(targetName "print_target_linked_libraries_${Target}")
    set(space " ")
    set(linked_libraries "$<TARGET_PROPERTY:${Target},LINK_LIBRARIES>") 
    set(linked_librraies "$<GENEX_EVAL:${linked_libraries}>")
    # set(linked_libraries "$<JOIN:${linked_libraries}, ${space}>")

    add_custom_target(${targetName} 
        COMMAND ${CMAKE_COMMAND} -E cmake_echo_color --cyan ${Target}:libraries: "${linked_libraries}"
        COMMAND_EXPAND_LISTS
    )

endfunction() 

```

---

<div class="post-metadata">

### Author: ![Drue\_Peters](https://discourse.cmake.org/user_avatar/discourse.cmake.org/drue_peters/32/2629_2.png) [@Drue\_Peters](https://discourse.cmake.org/u/Drue_Peters)
#### Post date: [November 15, 2022, 8:49pm UTC](https://discourse.cmake.org/t/object-file-propragating-cmake-debug-help/6874/2 "2022-11-15T20:49:05Z")

</div>

While typing I realized I had used `target_sources` instead of `target_link_libraries`. Not sure why this made a difference, but I’m not encountering any issues when properly linking these object files using the generator expressions and not adding to target sources.

If someone could still shed light on the Ninja issue and handling properties that have a mix of generator expressions and non-generator expressions, that would be great.

---

<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: [November 15, 2022, 9:54pm UTC](https://discourse.cmake.org/t/object-file-propragating-cmake-debug-help/6874/3 "2022-11-15T21:54:54Z")

</div>

`OBJECT` libraries only give their objects to targets directly linking to them. If the target is found through a transitive dependency, they are _not_ added (otherwise they’d duplicate every time a new target finds them in their transitive closure).

However, in your case, I think you “know” that `Ford` will only be directly used by `Vehicle` (though I think you’d need `target_link_libraires(Vehicle PRIVATE Ford)` to avoid duplicate symbols). You can then do `target_sources(Ford INTERFACE $<TARGET_OBJECTS:FordFocus>)`. I _think_ this will work, but you might need something like [this feature](https://gitlab.kitware.com/cmake/cmake/-/issues/22679) which would allow you to then just model them as `STATIC` libraries instead.
