Skip to content

Library tries to link to private static dependency

I have three libraries, A, B & C. A depends on B, and B privately depends on C. All are statically linked. They are also all in separate projects, so they are installed separately and find_package() each other.

C is a simple static library with no dependencies.

In B's CMakeLists.txt I create it like this:

find_package(C)
add_library(B ...)

target_link_libraries(B PRIVATE C)
install(TARGET B ...)

And then I use B from A in basically the same way:

find_package(B)
add_library(A ...)
target_link_libraries(A PRIVATE B)

Weirdly though, it fails when linking A because it adds -lC to the link, and it can't find C because it never did find_package(C) (and I don't want it to). C is an entirely private dependency of B and is completely statically linked. But it still ends up in B's config file (as $<LINK_ONLY:C>):

set_target_properties(B PROPERTIES
  INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
  INTERFACE_LINK_LIBRARIES "\$<LINK_ONLY:C>"
)

This may be related to #18049 (closed), in which it was suggested that this is necessary for some reason that I don't quite follow. Perhaps because C might link to dynamic libraries, and then when linking A you need to add those libraries to the link command. But I don't see why it would ever need to link against C itself, if C is a static library.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information