2
votes

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)

1
So basically the question is: is there a way to have something like: target_add_subdirectory - to add subdiretory 'visible' only to a given target?AbbysSoul

1 Answers

0
votes

You can disable the checks for duplicate names for the current project by adding the following line somewhere near the top of your main CMakeLists.txt:

cmake_policy(SET CMP0002 OLD)

It's possible that disabling these checks will cause you other problems, but this may be just the hacky workaround that you need.