Search code examples
cmakedependencieslibrariesdependency-managementtarget-link-libraries

What's the difference between add_dependencies and target_link_libraries for library targets?


In CMake, what is the difference between:

add_dependencies(tgt1 tgt2)

and

target_link_libraries(tgt1 tgt2)

when tgt2 is a library target?

Note: I don't mean difference in implementation, I mean difference in semantics.


Solution

  • add_dependencies just makes the second target's build get brought up to date before the first target if both targets need to be brought up to date. It's a build order control mechanism.

    target_link_libraries makes the second target a link dependency of the first target- they will be linked by the generated buildsystem- and makes it so that if the depended-upon target needs to be rebuilt, the first target will also be rebuilt if need by. When using PRIVATE or PUBLIC visibility, it's to express and instruct that things that are in the interface of the second target will be made available to the first target. When using INTERFACE or PUBLIC visibility, it's to express and instruct that things that are in the interface of the second target will be made available to targets that express link dependencies on the first target ("transitive" link dependencies).