Add only library headers during target_link_libraries()

$<COMPILE_ONLY> has been added in CMake 3.27 [1] [2] and can now be used like this:

target_link_libraries(y $<COMPILE_ONLY:x>)
target_link_libraries(z PRIVATE y)
target_link_libraries(executable z)

In the resuling build, the executable will be linked as c++ -o executable liz.a liby.a without linking against libx.a as well. Include directories of x, if any, will me mentioned only in compilation commands for y and x itself.

Huge thanks to you, Ben, for finally implementing it; I remember quite a few places among my codebases where this might be of service :slight_smile:

1 Like