'Cargo install Cargo-php' 给出 - 与 `cc` 链接失败:退出状态:1

问题描述 投票:0回答:1

Rustc --版本 rustc 1.78.0 (9b00956e5 2024-04-29)

货物--版本 货物 1.78.0 (54d8815d0 2024-03-26)

操作系统 - 在 WSL 上 - Ubuntu 22.4

我正在运行命令

Cargo install cargo-php
它会抛出错误。 我尝试了不同版本的 Rust 并运行了 rustup 工具链。

rustup toolchain uninstall stable && rustup toolchain install stable
,

已安装

sudo apt install libxcb-xfixes0-dev
>

我仍然看到同样的错误。

Compiling ext-php-rs v0.12.0
error: linking with `cc` failed: exit status: 1

|
 = note: LC_ALL="C" PATH="/home/ssuresh/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/ssuresh/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/ssuresh/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:

now" "-Wl,-O1" "-Wl,--strip-debug" "-nodefaultlibs" "-rdynamic"
 = note: /usr/bin/ld: /tmp/cargo-installQbAhTj/release/deps/libext_php_rs-b94e59a004df3e03.rlib(2e40c9e35e9506f4-wrapper.o): in function ext_php_rs_process_globals:
         wrapper.c:(.text.ext_php_rs_process_globals+0x7): undefined reference to `core_globals'
         /usr/bin/ld: /tmp/cargo-installQbAhTj/release/deps/libext_php_rs-b94e59a004df3e03.rlib(2e40c9e35e9506f4-wrapper.o): in function ext_php_rs_sapi_globals:
   i_globals':
         wrapper.c:(.text.ext_php_rs_sapi_globals+0x7): undefined reference to `sapi_globals'
         /usr/bin/ld: /tmp/cargo-installrkMtEp/release/deps/libext_php_rs-31b0a681f1d7129b.rlib(2e40c9e35e9506f4-wrapper.o): in function `ext_php_rs_file_globals':
         wrapper.c:(.text.ext_php_rs_file_globals+0x7): undefined reference to `file_globals'
         /usr/bin/ld: /tmp/cargo-installrkMtEp/release/deps/libext_php_rs-31b0a681f1d7129b.rlib(2e40c9e35e9506f4-wrapper.o): in function `ext_php_rs_sapi_module':
         wrapper.c:(.text.ext_php_rs_sapi_module+0x7): undefined reference to `sapi_module'
         /usr/bin/ld: /tmp/cargo-installrkMtEp/release/deps/libext_php_rs-31b0a681f1d7129b.rlib(2e40c9e35e9506f4-wrapper.o): in function `ext_php_rs_zend_bailout':



 = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
 = note: use the `-l` flag to specify native libraries to link
 = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#rustc-link-lib)

error: could not compile `cargo-php` (bin "cargo-php") due to 1 previous error
error: failed to compile `cargo-php v0.1.9`, intermediate artifacts can be found at `/tmp/cargo-installQbAhTj`.
To reuse those artifacts with a future compilation, set the environment variable `CARGO_TARGET_DIR` to that path```






php rust ld rust-cargo
1个回答
0
投票

您遇到的错误似乎与

cargo-php
包所需的本机库丢失或链接不正确有关。以下是排查和解决问题的分步指南:

1. 确保安装了所需的 PHP 开发库

由于您正在为 PHP 构建 Rust 绑定,因此您需要确保安装了 PHP 开发标头和库。您可以使用以下命令在 Ubuntu 上安装它们:

sudo apt install php-dev

这将安装构建 PHP 扩展所需的必要标头和库。

2. 安装构建依赖项

确保您拥有 Rust 和 C 编译所需的构建依赖项。您可能已经安装了一些,但运行它以确保:

sudo apt install build-essential pkg-config libssl-dev

3. 检查是否缺少本机库

错误消息显示缺少

core_globals
sapi_globals
file_globals
符号,这表明 PHP SAPI(服务器 API)和核心库未正确链接。您可能需要明确告诉
cargo-php
链接正确的 PHP 库。

编辑您的

Cargo.toml
或添加包含以下内容的
build.rs
文件:

[build-dependencies]
pkg-config = "0.3"

然后,在

build.rs
中添加:

fn main() {
    println!("cargo:rustc-link-lib=php"); // This links the PHP library
}

4. 设置适当的环境变量

确保环境可以找到 PHP 库和标头。您可能需要显式设置

PHP_INCLUDE_DIR
PHP_LIB_DIR
环境变量以指向 PHP 的安装位置。

export PHP_INCLUDE_DIR=/usr/include/php/20210902
export PHP_LIB_DIR=/usr/lib/x86_64-linux-gnu

(根据系统上安装 PHP 的位置调整路径。)

5. 重试安装
cargo-php

确保安装了依赖项并设置了路径后,尝试再次运行安装命令:

cargo install cargo-php

6. 检查 PHP 和 Rust 兼容性

如果问题仍然存在,请确保您尝试安装的

cargo-php
版本与您的 PHP 和 Rust 版本兼容。您可能需要尝试旧版本的
cargo-php
板条箱或更新您的 Rust 工具链。

© www.soinside.com 2019 - 2024. All rights reserved.