I am trying to set up cmake build for one of my libraries. Let say I want library B depending on A. Both libraries using CMake as build system. Both has an external dependency on gtest via git submodule
.
.
+-libA
|-CMakeLists.txt
|...
+-external/gtest
+-CMakeLists.txt
+-libB
|-CMakeLists.txt
|...
+-external/gtest
+-CMakeLists.txt
What would be the best project structure to be able to build projects independently and as part of a larger project?
With the above layout I get an errors that gtest is defined by another project:
CMake Error at libstyxe/external/gtest/googletest/cmake/internal_utils.cmake:161 (add_library):
add_library cannot create target "gtest" because another target with the
same name already exists. The existing target is a static library created
in source directory
"libsolace/external/gtest/googletest". See
documentation for policy CMP0002 for more details.
Options I tried: 1. Have libA symlinked from libB/external and including it via add_directory. Builind libB fails with the similar error to the one above. 2. Creating a root CMakeLists.txt adding both libA and libB as add_subdirectory. Same problem.
In both libA/CMakeLists.txt and libB/CMakeLists.txt gtest is included as:
add_subdirectory(external/gtest/googletest EXCLUDE_FROM_ALL)