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

使用setuptools构建带GRPC依赖的C++库Python绑定出现链接错误

问题:Python加载带gRPC依赖的C++库时出现未定义符号错误

背景

我正在为一个依赖gRPC的C++库扩展功能,通过vcpkg管理gRPC依赖,对应的CMakeLists.txt配置如下:

find_package(gRPC CONFIG REQUIRED)

target_link_libraries(
  mylib PRIVATE
  gRPC::grpc++)

该库提供Python绑定,使用setuptools进行构建,构建过程初期正常,但加载库时出现问题。

加载报错信息

在Python中导入库时触发如下错误:

❯ python3
Python 3.8.10 (default, Nov 14 2022, 12:59:47)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mylib
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: /usr/local/lib/python3.8/dist-packages/mylib-0.0.0-py3.8-linux-x86_64.egg/mylib.cpython-38-x86_64-linux-gnu.so: undefined symbol: _ZTIN6google8protobuf2io20ZeroCopyOutputStreamE

缺失的符号_ZTIN6google8protobuf2io20ZeroCopyOutputStreamE明确来自gRPC依赖的Protobuf库。

尝试的解决步骤及新问题

  • 修改setup.py添加gRPC库目录(注:此处-I是头文件路径参数,指定库路径应使用-L):
    toolchain_args += ['-I/home/atomic/vcpkg/installed/x64-linux/lib']
    
  • 扩展setup.py中的依赖库列表:
    libraries += ['libgrpc++', 'libprotobuf']
    

但以上操作均无效,添加库时出现链接错误:

/usr/bin/ld: cannot find -llibgrpc++
/usr/bin/ld: cannot find -llibprotobuf

目前gRPC同时通过vcpkg安装在/home/atomic/vcpkg/installed/x64-linux/,也在/usr/local/lib/下有安装,但两种路径的尝试都无法解决问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 04:00:46