微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

FetchContent的cmake问题,grpc和or-tools之间具有相同的protobuf依赖关系

如何解决FetchContent的cmake问题,grpc和or-tools之间具有相同的protobuf依赖关系

使用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 complan aboud包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 FetchContent_Declare中的protobuf包可用?

谢谢!

解决方法

交叉发布... 已经在这里回答: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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。