Object library dependencies don't seem to propagate through target_link_libraries?

Having this setup:

add_library(libA OBJECT lib_a.c)

add_library(libB OBJECT lib_b.c)
target_link_libraries(libB PUBLIC libA)

add_executable(myExec main.c)
target_link_libraries(myExec 
    #libA ???
    libB
)

does not seem to work as expected, or at least what I would expect.

Linking myExec will complain about undefined symbols, which reside in libA. I would expect that since libB has a dependency on libA that that would propagate to myExec.

Am I missing something?

(CMake 3.28.3 on WSL 2)

Usage requirements of OBJECT libraries do propagate, but not the objects themselves. This is to avoid duplicating object files when linking more complex dependency graphs. Only a target that links directly to an OBJECT library will receive that library’s object files.

I see. But doesn’t the duplication problem exist also with archives? Or is it maybe the target linker that resolves the duplicates in this case?

Duplicate static libraries are not a problem, the linker only looks in those for symbols it needs. Libraries being included on the linker line multiple times is relatively common. Object files are not handled that way, they are added to the binary whether needed or not (a later optimisation pass may remove them, but that’s not relevant to this discussion).

So to follow the expected behaviour for static libraries, CMake could easily remove duplicates when adding dependencies to object libraries :slight_smile:

But yeah, I can see the point in having to explicitly adding the object library dependencies, although I can’t think of a particular use case for it right now.