[CMake] Does target order matter for dependencies?

Robert Dailey rcdailey.lists at gmail.com
Thu Dec 8 10:01:53 EST 2016


When I define my targets (custom, library, executable), does the order
of their declaration matter when it comes to using those targets in
dependencies? It's difficult to explain, so I'll show an example:


add_executable(foo main1.cpp)
target_link_libraries(foo abc)

add_library(abc main2.cpp)

In the example above, the add_library() happens after
target_link_libraries() for the executable. I've seen this work, but I
was using static libraries. Would this work for shared libraries too?
Does CMake do a multi-pass parse so that all targets are known prior
to them being referenced?

The target_link_libraries() [1] documentation states:

"The named target must be created by add_library() within the project
or as an IMPORTED library. If it is created within the project an
ordering dependency will automatically be added in the build system to
make sure the named library target is up-to-date before the <target>
links."

I'm not sure what "up-to-date" here means, or if this specific
documentation is talking about compile-time dependencies or cmake
dependencies.

[1]: https://cmake.org/cmake/help/latest/command/target_link_libraries.html


More information about the CMake mailing list