I'm used to defining CMake target dependencies using the target_link_libraries()
command. Recently, though, I've noticed the existence of another command, add_dependencies()
. When should I use the latter rather than the former?
Note: I tried reading this question: target_link_libraries and add_dependencies - but the answers confused me. I understand I should not use both. But it's not entirely clear to me when I should use which.
add_dependencies
should only be used where you need to establish a dependency in the build process that would otherwise not be present.
If a you want to influence the linker options, you use target_link_libraries
.
For INTERFACE
targets target_link_libraries
should be used, even if it doesn't have an effect on the linker options. An example would be "linking" a header only library: This usually only results in include directories being inherited; you use target_link_libraries
for this nonetheless.
A notable example of using add_dependencies
would be to make sure MODULE
targets dynamically loaded by an executable target get built when building the executable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With