【问题标题】:cmake issue with FetchContent same protobuf dependency between grpc and or-toolsgrpc和or-tools之间的FetchContent相同protobuf依赖的cmake问题
【发布时间】:2020-11-10 14:02:07
【问题描述】:

我使用来自 cmake 的 FetchContent 来管理我的依赖项

FetchContent_Declare(
        protobuf
        GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git
        GIT_TAG        v3.13.0
        SOURCE_SUBDIR  cmake
)
## END PROTOBUF ##

## GRPC ##
FetchContent_Declare(
        grpc
        GIT_REPOSITORY https://github.com/grpc/grpc.git
        GIT_TAG        v1.33.x
        GIT_PROGRESS   TRUE
)
## END GRPC ##

## OR-TOOLS ##
FetchContent_Declare(
        or-tools
        GIT_REPOSITORY https://github.com/google/or-tools.git
        GIT_TAG        master
        GIT_PROGRESS   TRUE
        PATCH_COMMAND git apply "${PROJECT_SOURCE_DIR}/patches/or-tools-protobuf.patch"
)
FetchContent_MakeAvailable(protobuf grpc or-tools)

问题是 or-tools 和 grpc 在内部也使用 protobuf 作为 FetchContent_Declare,所以 OR-TOOLS 抱怨 protobuf 已经存在

CMake Error at build/_deps/protobuf-src/cmake/libprotobuf-lite.cmake:64 (add_library):
  add_library cannot create target "libprotobuf-lite" because another target
  with the same name already exists.  The existing target is a static library
  created in source directory
  "/Users/samuaz/Projects/itbit/securities-settlement/cpp/third_party/grpc-src/third_party/protobuf/cmake".
  See documentation for policy CMP0002 for more details.
Call Stack (most recent call first):
  build/_deps/protobuf-src/cmake/CMakeLists.txt:250 (include) 

如果我尝试禁用 or-tools 集的 protobuf (BUILD_Protobuf OFF),则找不到 or-tools 程序包 protobuf

--   No package 'protobuf' found
CMake Error at /Users/samuaz/Library/Application Support/JetBrains/Toolbox/apps/CLion/ch-0/202.6948.80/CLion.app/Contents/bin/cmake/mac/share/cmake-3.17/Modules/FindPkgConfig.cmake:497 (message):
  A required package was not found

如何避免此问题或使包 protobuf 可从以前的 protobuf FetchContent_Declare 获得?

谢谢!

【问题讨论】:

  • Hooboy 我也使用 cmake fetchcontent,但之前不必处理这个复杂的依赖问题。我真的无法回答这个问题。我肯定会在 cmake 讨论中问这个问题,因为实际的 cmake 开发人员实际上是在那里看的:discourse.cmake.org

标签: c++ cmake protocol-buffers grpc or-tools


【解决方案1】:

交叉发布... 已经在这里回答了:https://github.com/google/or-tools/issues/2219

将 deps.cmake(或稳定 v8.0 上的 cpp.cmake)修改为

if(NOT BUILD_Protobuf AND NOT TARGET protobuf::libprotobuf)
  find_package(Protobuf REQUIRED)
endif()

相关:https://gitlab.kitware.com/cmake/cmake/-/issues/17735

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 2022-11-23
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多