【问题标题】:Is it possible to include protobuf using cmake's FetchContent?是否可以使用 cmake 的 FetchContent 包含 protobuf?
【发布时间】:2022-11-23 18:21:28
【问题描述】:

我想在我的 C++ 库中使用 protobuf。到目前为止,所有依赖项都使用 cmake 的 FetchContent 模块包含在内。我想对protobuf 做同样的事情。但是,我遇到了以下问题:Unknown CMake command "protobuf_generate_cpp". 关于如何解决这个问题的任何提示?

我的CMakeLists.txt 的摘录:

FetchContent_Declare(fmt
        GIT_REPOSITORY https://github.com/fmtlib/fmt.git
        GIT_TAG 9.0.0)

FetchContent_Declare(protobuf
        GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git
        GIT_TAG v21.4)

FetchContent_MakeAvailable(fmt protobuf)

include_directories(${Protobuf_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_BINARY_DIR})

protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS message.proto)

【问题讨论】:

    标签: cmake protocol-buffers fetchcontent


    【解决方案1】:

    这对我有用:

    FetchContent_Declare(protobuf
      GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git
      GIT_TAG        v21.4
      SOURCE_SUBDIR  cmake
      FIND_PACKAGE_ARGS NAMES protobuf
    )
    FetchContent_MakeAvailable(protobuf)
    

    在消费者端这样做

    include(FindProtobuf)
    find_package(protobuf CONFIG REQUIRED)
    

    注意:这仅在 CMake v3.25 上测试过

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-12
      • 2019-09-08
      • 1970-01-01
      • 2012-08-11
      • 1970-01-01
      • 1970-01-01
      • 2011-12-20
      • 2012-03-29
      相关资源
      最近更新 更多