Yulong Ao
Yulong Ao

Reputation: 1261

Cmake executable can not link with the new interface library depending on another library

I have the following dependencies:

add_library(lib)
add_library(ilib INTERFACE)
add_dependencies(ilib lib)
target_link_libraries(ilib INTERFACE
      "-Wl,--whole-archive $<TARGET_FILE:lib> Wl,--no-whole-archive")
add_executable(exe ilib)

When I changed some source codes of lib, the lib as expected was compiled and built again. However, exe did not link the new lib. If I use add_executable(exe lib), then exe will always link the new lib. (The reason why I use the ilib is that I need to process lib before using it.)

Upvotes: 1

Views: 610

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 65976

You expect lib to be propagated when one links with ilib.

But command add_dependencies doesn't add properties for propagation. You need

# Linking with `ilib` will transitively link with a `lib`
target_link_libraries(ilib INTERFACE lib)

When need to use --whole-archive option for linker, it could be done in the following way:

target_link_libraries(ilib INTERFACE "-Wl,--whole-archive" lib "Wl,--no-whole-archive")

When parse arguments for given function, CMake will finds argument lib to be a target name, and will add proper file-level dependency. With that dependency the executable will be relinked whenever the library file has been changed.

Upvotes: 1

Related Questions