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_dependenciesshould 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
INTERFACEtargetstarget_link_librariesshould 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 usetarget_link_librariesfor this nonetheless.A notable example of using
add_dependencieswould be to make sureMODULEtargets dynamically loaded by an executable target get built when building the executable.