0

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?

gil_mo
  • 575
  • 1
  • 6
  • 27
  • 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 Jun 03 '18 at 15:17
  • Could be! In any case the main point of my question remains... – gil_mo Jun 03 '18 at 16:06
  • If you mean `target_link_libraries`, then the answer is here: https://stackoverflow.com/questions/50292879/cmake-add-library-to-all-targets (in short: use `link_libraries`). – Tsyvarev Jun 03 '18 at 16:15
  • I actually want to know how to cause target_link_libraries to invoke the building process for my_common_lib. – gil_mo Jun 03 '18 at 16:51
  • `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 Jun 03 '18 at 19:19
  • But what would be written in the `my_common_lib` script? If I write `add_library` and `include` it from the `my_shared` CMakeLists, then it would always attempt to build it? – gil_mo Jun 03 '18 at 20:50
  • Yes, it should work. What about just trying? – Tsyvarev Jun 03 '18 at 21:44
  • The problem with this approach is that the included cmake would build `my_common_lib` in the same directory as the including cmake, resulting in two different builds of the common. And specifying the build directory for the common would also set it for the shared, since it is the same build unit... help! – gil_mo Jun 04 '18 at 07:29
  • Do shared libraries belongs to **different** projects? You have never said that before. If so, you need to build the project with `my_common_lib` as *standalone*. E.g., projects with shared libraries may use `ExternalProject_Add` for build the project with `my_common_lib`. If provide the same parameters to `ExternalProject_Add` in both project, common project will be built only once. – Tsyvarev Jun 04 '18 at 07:46
  • Correct, I've edited my Q to make it clear. I've looked at ExternalProject_Add and it seems overwhelming. My initial thinking was to have something similar to Visual Studio (or Xcode) projects. The shared library would be a regular project, and the two others would also be regular projects and would specify the common one as a dependency. So, the common can build as a standalone if wanted, or, building any of the shared libs would invoke the build of the common. – gil_mo Jun 04 '18 at 07:57
  • @gil_mo How the projects are structured? Do they belong to the same source tree / repository? When building `my_shared*`, do you want that `my_common_lib` is built as well, or do you want it to find a pre-built and installed version of `my_common_lib`? – Kane Jun 05 '18 at 08:11

1 Answers1

2

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.

Matthew
  • 2,593
  • 22
  • 25