We have a number of Git repositories, some containing our own code and some containing slightly modified third-party library code. A simplified dependency graph looks like this:
executable_A
| |
| v
| library_B
| |
v v
library_C
So the executable has two dependencies on library_C
, one direct and one transitive. I am hoping to tie this all together using Git submodules and CMake, so a simplified directory structure looks like this:
executable_A/
CMakeListst.txt
library_B/
CMakeLists.txt
library_C/
CMakeLists.txt
library_C/
CMakeLists.txt
As you can see, the library_C
repository is included as a submodule twice. Let's assume that both submodules are pointing at the same commit (any ideas about how to enforce that would be welcome, but are not the topic of this question).
We're using add_subdirectory
, target_link_libraries
and target_include_directories
to manage these interdependencies. Pretty standard.
The problem is that CMake doesn't like it if you create a target with the same name twice, so it complains:
CMake Error at library_C/CMakeLists.txt:13 (add_library):
add_library cannot create target "library_C" because another target with the same name already exists. The existing target is a static library created in source directory ".../library_B/library_C".
See documentation for policy CMP0002 for more details.
I'd rather not remove the direct dependency of executable_A
on library_C
, because the fact that it is pulled in via library_B
is an implementation detail of library_B
that should not be relied on. Moreover, this approach will break down as soon as we add another dependency like executable_A --> library_D --> library_C
.
(This question is the closest I could find, but is a bit more general and remains unanswered anyway.)
if(TARGET library_C)
) before stepping into the project withadd_subdirectory()
. – Sweptbackif(NOT TARGET library_c)
then, of course. Sounds like a viable approach. Care to post it as an answer? – Beira