I maintain 3 static libraries: a
, b
and c
, that use CMake. They are located in separate git repositories and can be standalone.
a
is a very basic library, that has no dependencies.b
depends ona
. It includesa
as a submodule and adds it withadd_subdirectory
.c
depends on both ofa
andb
. It includes them as submodules and adds withadd_subdirectory
.
So, the structure of c
repo is as follows:
/c (repository root)
|
+-- CMakeLists.txt (add_subdirectory(a), add_subdirectory(b))
|
+--/a (git submodule)
| |
| +-- CMakeLists.txt (add_library(a))
|
+--/b (git submodule)
|
+-- CMakeLists.txt (add_subdirectory(a))
|
+--/a (git submodule added recursively)
|
+-- CMakeLists.txt (add_library(a))
We can see, that a
is duplicated: add_library(a)
is called twice and cause an error about duplicated target.
I can wrap every add_subdirectory
into if (NOT TARGET ...)
, but it seems to be improper solution to the problem. I could also just make c
use a
from b
, but I don't want c
to "know about" b
's internals and dependencies.
I saw here on SO some advice not to use add_subdirectory
on projects, that can be included into another ones, but can't figure out alternative way.
To sum up: what is the right way to deal with such dependencies using CMake? The main restriction is that b
must be standalone, i.e. I want to build it without building c
.
Moreover, I don't want to "install" these libs. If I would build b
, then a
must be built once just before b
. If I would build c
, then a
and b
must be built once just before c
.