0
votes

I have two separate projects, each builds a shared lib that links with a common static lib. In each of the shared libs' CMakeLists.txt I'll have:

add_dependencies(my_shared1 my_common_lib)

and

add_dependencies(my_shared2 my_common_lib)

The static lib has its own sources and resides in its own folder. What is the best way to define the common static cmake script?

1
Shouldn't add_dependencies be replaced with target_link_libraries? (That would mean, that you my_shared1 library not only depends on my_common_lib one, but is linked with it). - Tsyvarev
Could be! In any case the main point of my question remains... - gil_mo
If you mean target_link_libraries, then the answer is here: stackoverflow.com/questions/50292879/… (in short: use link_libraries). - Tsyvarev
I actually want to know how to cause target_link_libraries to invoke the building process for my_common_lib. - gil_mo
target_link_libraries implies target-level dependencies. That is, building of my_shared1 library starts only after my_common_lib library will be built. - Tsyvarev

1 Answers

1
votes

If your "projects" are tightly coupled (e.g. live in the same repository / have the same parent directory), just write a CMakeLists.txt above all three that calls add_subdirectory for each project's directory. If you use project in each, they should show up as separate entities in VS/XCode.

If not, just write your CMakeLists.txt for the static library like you normally would, have it export itself (check out export() and install(EXPORTS)) and use find_package in the consumers to find it, then just target_link_libraries the imported target.

If these need to live in separate repositories, but you also need somewhere to create a build that builds all three, then you'll want to look at External Projects.