[CMake] CMake link order

Fred Baksik fdk17 at ftml.net
Fri Oct 18 11:55:19 EDT 2019



On Fri, Oct 18, 2019, at 6:24 AM, Bon, William wrote:
> Hello,
> 
> we are facing an issue while using cmake and we have no idea how to solve or debug it.
> We have a complex and huge project (about 50 subdirectories and dependencies everywhere), and we are facing issue regarding the link order.
> There is a lot of dependencies between those projects, and to summarize, we have two libraries A and B
> A is an imported library from headers and shared lib .so declared like this
> ```
> add_library(A SHARED IMPORTED GLOBAL)
> set_target_properties(A PROPERTIES
>  IMPORTED_LINK_INTERFACE_LANGUAGES "C"
>  IMPORTED_LOCATION ${A_LIBRARY})
> ```
> 
> B is a system library part of a package.
> 
> We need to link A before B in every case.
> 
> In every project, we include A before B using
> ```
> find_package(B)
> target_link_libraries(${library_produced} PUBLIC A)
> target_link_libraries(${library_produced} PUBLIC B)
> ```
> 
> but from time to time, we don't know why, the library produced link in the wrong order (checked with ldd and make VERBOSE=1).
> it links B before A.
> 
> Is there a way to find out what happens and why cmake change the link order for some project and not all projects ?
> 
> Best regards,
> 
> Bill
> 

I'm going by memory when I saw something similar when updating the GHS Generator in CMake.

In target_link_libraries it states that "The library dependency graph is normally acyclic (a DAG)". I recall from my own experience that the DAG is not always created the same way when generating the project. It has to do with the way with this part of CMake is implemented (C++ containers and that sort of thing) so while all the inputs are the same the order is Not the same so it produces a different DAG which changed the ordering of the components build order which also altered the link order.

For the GHS Generator to ensure that the exact same project files are output we sorted the components by name into a vector first (instead of just using a set) before supplying it to this algorithm. Then the results were always the same.

But if I remember correctly an "add_dependency( B A )" or otherwise make B dependent on A does forces a dependency between the two libraries which forces the DAG to always put A before B.

I would bet that if you just did the project generation over and over again into different build directories you'll notice these kinds of differences in the different Makefiles (or whatever output that is expected by the generator).

This is how I noticed the issue with the GHS Generator because the ordering of some items were just changing in an unexpected fashion and I was always comparing what it generated against what I was expecting it to generate.

I never asked the main developers about this; and again this is just my own observations.

Best regards,
Fred
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20191018/acc4273f/attachment-0001.html>


More information about the CMake mailing list