Feature.
target_link_libraries(myExe myLib)
tells CMake to link the executable myExe
to the library myLib
, which is a CMake target built by this project. This tells CMake to ensure target dependencies and build order are correctly maintained etc. You don’t want to have to change the target_link_libraries
call just because you later decide to turn myLib
from a static library into a shared one (see also BUILD_SHARED_LIBS
), or to accommodate the differences between Windows, Linux, MacOSX, etc.
In contrast, target_link_libraries(myExe libmyLib.a)
tells CMake to link the executable myExe
against a pre-existing library libmyLib.a
, which is totally independent of the current buildsystem. You’d use this to link to a system library or a 3rd-party one (and you’d want to use an absolute path for the latter to ensure correct rebuilds when the library changes).
By correctly encoding the dependencies in the generated buildsystem. When generating Makefiles, it will encode the dependency in the build rule, listing the library in the exeutable’s rule’s dependencies. When generating a Visual Studio solution, it will populate the ProjectSection(ProjectDependencies)
section of the .sln
file accordingly. And so on for other generators.