0

我正在使用具有以下依赖项的项目:

grpc

  • 谷歌测试
  • 基准

同时我的项目有基准。

当我使用声明以下依赖项时,FetchContent出现以下错误:

add_library cannot create target "benchmark" because another target
  with the same name already exists.  The existing target is an executable
  created in source directory

CMakeLists.txt的是:

cmake_minimum_required(VERSION 3.15)
project(dahj)

set(CMAKE_CXX_STANDARD 14)

include(FetchContent)

FetchContent_Declare(
        grpc
        GIT_REPOSITORY https://github.com/grpc/grpc.git
        GIT_TAG v1.17.2
)
FetchContent_MakeAvailable(grpc)

FetchContent_Declare(
        benchmark
        GIT_REPOSITORY https://github.com/google/benchmark.git
        GIT_TAG v1.5.0
)
FetchContent_MakeAvailable(benchmark)

add_executable(dahj main.cpp grpc++)

我知道我可以使用基准而不获取它,而只需链接它。但是,如果grpc决定取消基准作为依赖项,它会破坏我的构建。有一个更好的方法吗?

4

1 回答 1

1

仅在尚未创建“基准”目标时调用FetchContent_DeclareFetchContent_MakeAvailablefor :benchmark

if (NOT TARGET benchmark)
    FetchContent_Declare(
            benchmark
            GIT_REPOSITORY https://github.com/google/benchmark.git
            GIT_TAG v1.5.0
    )
    FetchContent_MakeAvailable(benchmark)
endif()

这将安全地防止benchmark项目被包含两次。

另请参阅该问题:CMake : multiple subprojects using the same static library

于 2020-04-04T14:46:54.477 回答