You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Conan安装C++版gRPC时Protobuf依赖查找失败求助

C++ gRPC安装问题:Conan下Protobuf找不到的解决方法

问题描述

尝试用Bazel和CMake安装C++版gRPC均失败,转而使用Conan,但出现Protobuf相关错误。已将protobuf/3.21.4添加到依赖项中仍无法解决,相关文件如下:

Conanfile.py

from conans import ConanFile, CMake, tools

GRPC_VERSION = "v1.48.0"

class GRPCExample(ConanFile):
    name = "grpc-example"
    version = GRPC_VERSION
    url = "https://github.com/grpc/grpc"
    description = "Built from grpcs example code in Github"
    topics = ("grpc", "example")
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False]}
    default_options = {"shared": False}
    generators = "cmake"

    requires = [("protobuf/3.21.4")]

    def source(self):
        self.run("git clone --depth 1 --branch " + GRPC_VERSION + " https://github.com/grpc/grpc")

    def build(self):
        cmake = CMake(self)
        cmake.configure(source_folder="grpc/examples/cpp/helloworld")
        cmake.build()

    def package(self):
        self.copy("*.h", dst="include", src="grpc/examples/cpp/helloworld")
        self.copy("*.lib", dst="lib", keep_path=False)
        self.copy("*.dll", dst="bin", keep_path=False)
        self.copy("*.so", dst="lib", keep_path=False)
        self.copy("*.dylib", dst="lib", keep_path=False)
        self.copy("*.a", dst="lib", keep_path=False)

    def package_info(self):
        self.cpp_info.libs = ["grpc-example"]

错误日志

-- Found Threads: TRUE  
CMake Error at /home/emcp/.conan/data/grpc-example/v1.48.0/stonks/dev/build/1df012f840559ffd3600baf4353bf1a2db1635ac/grpc/examples/cpp/cmake/common.cmake:101 (find_package):
  Could not find a package configuration file provided by "Protobuf" with any
  of the following names:

    ProtobufConfig.cmake
    protobuf-config.cmake

  Add the installation prefix of "Protobuf" to CMAKE_PREFIX_PATH or set
  "Protobuf_DIR" to a directory containing one of the above files.  If
  "Protobuf" provides a separate development package or SDK, be sure it has
  been installed.
Call Stack (most recent call first):
  CMakeLists.txt:24 (include)


-- Configuring incomplete, errors occurred!
See also "/home/emcp/.conan/data/grpc-example/v1.48.0/stonks/dev/build/1df012f840559ffd3600baf4353bf1a2db1635ac/CMakeFiles/CMakeOutput.log".
grpc-example/v1.48.0@stonks/dev: 
grpc-example/v1.48.0@stonks/dev: ERROR: Package '1df012f840559ffd3600baf4353bf1a2db1635ac' build failed
grpc-example/v1.48.0@stonks/dev: WARN: Build folder /home/emcp/.conan/data/grpc-example/v1.48.0/stonks/dev/build/1df012f840559ffd3600baf4353bf1a2db1635ac
ERROR: grpc-example/v1.48.0@stonks/dev: Error in build() method, line 25
        cmake.configure(source_folder="grpc/examples/cpp/helloworld")
        ConanException: Error 1 while executing cd '/home/emcp/.conan/data/grpc-example/v1.48.0/stonks/dev/build/1df012f840559ffd3600baf4353bf1a2db1635ac' && cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE="Release" -DCONAN_IN_LOCAL_CACHE="ON" -DCONAN_COMPILER="gcc" -DCONAN_COMPILER_VERSION="11" -DCONAN_CXX_FLAGS="-m64" -DCONAN_SHARED_LINKER_FLAGS="-m64" -DCONAN_C_FLAGS="-m64" -DCONAN_LIBCXX="libstdc++11" -DBUILD_SHARED_LIBS="OFF" -DCMAKE_INSTALL_PREFIX="/home/emcp/.conan/data/grpc-example/v1.48.0/stonks/dev/package/1df012f840559ffd3600baf4353bf1a2db1635ac" -DCMAKE_INSTALL_BINDIR="bin" -DCMAKE_INSTALL_SBINDIR="bin" -DCMAKE_INSTALL_LIBEXECDIR="bin" -DCMAKE_INSTALL_LIBDIR="lib" -DCMAKE_INSTALL_INCLUDEDIR="include" -DCMAKE_INSTALL_OLDINCLUDEDIR="include" -DCMAKE_INSTALL_DATAROOTDIR="share" -DCMAKE_EXPORT_NO_PACKAGE_REGISTRY="ON" -DCONAN_EXPORTED="1" -Wno-dev '/home/emcp/.conan/data/grpc-example/v1.48.0/stonks/dev/build/1df012f840559ffd3600baf4353bf1a2db1635ac/grpc/examples/cpp/helloworld'

问题根源

Conan默认的cmake生成器仅生成conanbuildinfo.cmake,但gRPC示例的CMake脚本直接调用find_package(Protobuf),无法自动识别Conan安装的Protobuf路径,导致找不到配置文件。

解决方法

方法一:修改Conan生成器并传递Protobuf路径

  1. 更换CMake生成器
    将generators = "cmake"替换为generators = "cmake_find_package_multi",这个生成器会生成符合CMake标准的包配置文件,让find_package能正常识别。

  2. 在build方法中指定Protobuf_DIR
    修改build函数,手动将Conan安装的Protobuf路径传递给CMake:

def build(self):
    cmake = CMake(self)
    # 获取Protobuf的安装根目录
    protobuf_root = self.deps_cpp_info["protobuf"].rootpath
    cmake.definitions["Protobuf_DIR"] = protobuf_root
    cmake.configure(source_folder="grpc/examples/cpp/helloworld")
    cmake.build()

方法二:直接依赖Conan的gRPC包(更简洁)

无需手动克隆gRPC源码编译,Conan中心提供了预编译的gRPC包,它会自动处理Protobuf等依赖:

from conans import ConanFile, CMake

class GRPCExample(ConanFile):
    name = "grpc-example"
    version = "v1.48.0"
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False]}
    default_options = {"shared": False}
    generators = "cmake_find_package_multi"

    # 直接依赖gRPC,自动拉取兼容的Protobuf版本
    requires = ["grpc/1.48.0"]

    def build(self):
        cmake = CMake(self)
        # 这里可以使用自己的helloworld代码,或从gRPC示例中拷贝
        cmake.configure()
        cmake.build()

额外注意事项

  • 如果你使用的是Conan 2.x版本,需要调整代码语法,比如导入改为from conan import ConanFile,依赖写法改为requires = "grpc/1.48.0"。
  • 运行conan install .时,加上--build=missing参数,确保缺失的依赖包被正确编译安装。

内容的提问来源于stack exchange,提问作者Erik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 22:07:49