Korchkidu
Korchkidu

Reputation: 4946

How to model transitive dependencies between static libraries in CMake?

Given an executable myExe, and 2 static libraries myLib1 and myLib2. Given the following dependencies myExe -> myLib1 -> myLib2, how should you model transitive dependency between myLib2 and myLib1?

It seems that the correct way to do it may be:

target_link_libraries(myLib2 myLib1)

But, according to the documentation:

Specify libraries or flags to use when linking a given target and/or its dependents

Also, add_dependencies does not seem to be transitive.

So I find this confusing to use target_link_libraries and I am wondering if there is another "cleaner" way.

Upvotes: 1

Views: 1564

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 65870

For express usage dependency myLib1 -> myLib2 (that is, library myLib1 uses functions defined in myLib2), use

target_link_libraries(myLib2 myLib1)

While target_link_libraries doesn't affect on the file myLib2.a (because static libraries are never linked), an effect will be seen when myLib2 will be linked into shared library or executable:

target_link_libraries(myExe myLib2)

will automatically link myExe with myLib1.


Note again, that such linkage propagation for static libraries works only when myLib2 is used in the same project which calls target_link_libraries(myLib2 myLib1).

Attempt to target_link_libraries(myExe myLib2) from another project will link just with myLib2.a file, which doesn't contain information about myLib2.

Upvotes: 3

Related Questions