In CMake, what is the difference between:
add_dependencies(tgt1 tgt2)
and
target_link_libraries(tgt1 tgt2)
when tgt2 being a library target?
Note: I don't mean difference in implementation, I mean difference in semantics.
In CMake, what is the difference between:
add_dependencies(tgt1 tgt2)
and
target_link_libraries(tgt1 tgt2)
when tgt2 being a library target?
Note: I don't mean difference in implementation, I mean difference in semantics.
On
add_dependencies(A B) makes sure the generated project will make sure B is up to date before building A. It's the equivalent to adding the following rule to a makefile
A: B
target_link_libraries(A B) has the same effect plus:
B among the files linked when generating the binary for AINTERFACE properties of B take effect on A. Many of those correspond to cmake commands you can pass INTERFACE/PUBLIC to, e.g.
target_compile_definitionstarget_compile_featurestarget_compile_optionstarget_include_directoriestarget_link_directoriestarget_link_librariestarget_link_optionstarget_precompile_headerstarget_sourcesA is a library and you're adding PUBLIC/INTERFACE as visibility, targets linking A get access to INTERFACE properties in a similar mannerinstall(EXPORT) and install(TARGETS) may change slightly. (Export dependency information, rpath update.)LINK_LIBRARIES target property for A is changed
add_dependenciesjust 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_librariesmakes 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).