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

使用Cross交叉编译Rust项目时libgpg-error-sys构建失败求助

交叉编译Rust项目时libgpg-error-sys构建失败问题

问题场景

在Arch Linux机器上,使用Cross工具针对x86_64-unknown-linux-gnu架构交叉编译依赖gpgme = "0.11.0"的Rust项目时,触发如下错误:

$ cross build --target x86_64-unknown-linux-gnu                                                                                                                         
   Compiling tracing v0.1.37
   Compiling parking_lot_core v0.9.7
   Compiling rand_core v0.5.1
   Compiling libgpg-error-sys v0.6.0
error: failed to run custom build command for `libgpg-error-sys v0.6.0`

Caused by:
  process didn't exit successfully: `/target/debug/build/libgpg-error-sys-49b8c1a22a8cb06e/build-script-build` (exit status: 1)
  --- stdout
  cargo:rerun-if-env-changed=GPG_ERROR_NO_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-gnu
  cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_gnu
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64-unknown-linux-gnu
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64_unknown_linux_gnu
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64-unknown-linux-gnu
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64_unknown_linux_gnu
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-gnu
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_gnu
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR

  --- stderr
  Error: PkgConfig(`PKG_CONFIG_ALLOW_SYSTEM_CFLAGS="1" "pkg-config" "--libs" "--cflags" "gpg-error" "gpg-error >= 1.12"` did not exit successfully: exit status: 1
  error: could not find system library 'gpg-error' required by the 'libgpg-error-sys' crate

  --- stderr
  Package gpg-error was not found in the pkg-config search path.
  Perhaps you should add the directory containing `gpg-error.pc'
  to the PKG_CONFIG_PATH environment variable
  No package 'gpg-error' found
  Package gpg-error was not found in the pkg-config search path.
  Perhaps you should add the directory containing `gpg-error.pc'
  to the PKG_CONFIG_PATH environment variable
  No package 'gpg-error' found
  )
warning: build failed, waiting for other jobs to finish...

已尝试无效的操作

  • 重装libgpg-error包
  • 将pkg-config --path gpg-error输出的/usr/lib/pkgconfig/gpg-error.pc所在目录添加到PKG_CONFIG_PATH环境变量

解决方案

Cross在容器内执行编译,主机的库和环境变量不会自动传递到容器,可通过以下两种方式解决:

方法一:在Cross容器内安装依赖

  1. 在项目根目录创建.cross/config.toml文件
  2. 写入配置:
    [target.x86_64-unknown-linux-gnu]
    pre-build = ["apt-get update && apt-get install -y libgpg-error-dev"]
    
    Cross默认使用Debian/Ubuntu镜像,因此用apt安装对应开发包。
  3. 重新执行编译命令:
    cross build --target x86_64-unknown-linux-gnu
    

方法二:让libgpg-error-sys从源码编译

修改项目的Cargo.toml,添加patch启用vendored特性,让crate自行编译内置的libgpg-error源码,无需依赖系统库:

[patch.crates-io]
libgpg-error-sys = { version = "0.6.0", features = ["vendored"] }

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 15:33:15