CMake: FetchContent_Declare leads to target name conflict

Viewed 223

I am trying to automate the fetch a few dependencies in CMakeList.txt, using FetchContent_Declare and FetchContent_MakeAvailable.

However I found the following error when running cmake

CMake Error at build/_deps/hermes-src/cmake/modules/Hermes.cmake:123 (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
  "/path/to/build/_deps/googletest-src/googletest".  See
  documentation for policy CMP0002 for more details.
Call Stack (most recent call first):
  build/_deps/hermes-src/external/llvh/utils/unittest/CMakeLists.txt:53 (add_hermes_library)


CMake Error at build/_deps/hermes-src/cmake/modules/Hermes.cmake:123 (add_library):
  add_library cannot create target "gtest_main" because another target with
  the same name already exists.  The existing target is a static library
  created in source directory
  "path/to/build/_deps/googletest-src/googletest".  See
  documentation for policy CMP0002 for more details.
Call Stack (most recent call first):
  build/_deps/hermes-src/external/llvh/utils/unittest/UnitTestMain/CMakeLists.txt:1 (add_hermes_library)

This is the CMakeLists.txt to reproduce

cmake_minimum_required(VERSION 3.13)
project(pytorch_live_cxx)

set(CMAKE_CXX_STANDARD 14)

include(FetchContent)

FetchContent_Declare(
  googletest
  URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
FetchContent_MakeAvailable(googletest)

FetchContent_Declare(
  hermes
  URL https://github.com/facebook/hermes/archive/d63feeb46d26fe0ca7e789fc793f409e5158b27f.zip
)
FetchContent_MakeAvailable(hermes)

My questions:

  1. does that mean hermes has googletest as dependencies? If so, I can just call target_link_libraries(myexec, gtest) without calling fetching googletest?

  2. Is there any better practice to avoid target name conflict like this? This is an example in my imagination: we have package A which builds target a and b, and package B which builds targets b and c. What should I do if I want to links to all a, b and c?

Thanks

0 Answers
Related