gpt4 book ai didi

rust - 取决于 -sys 包装器库?

转载 作者:行者123 更新时间:2023-11-29 08:09:55 28 4
gpt4 key购买 nike

我有一个 Rust 库,它基本上是一些 C 源代码和一个 bindgen 包装器。它有一个进入 C 源目录的 build.rs 脚本,运行 make,并将 .a 静态库复制到 $OUT_DIR。当我运行 cargo build 时,一切正常。

但是,当我将该库添加为另一个项目 foo-rs 的依赖项时,make 似乎永远不会运行。我最终遇到的错误是

   Compiling foo-sys v0.1.6
Running `rustc <LOTS OF STUFF> -L /home/paul/projects/foo-rs/target/build/foo-sys-936b7a25940b4bf0/out -l foo:static`
error: could not find native static library `foo`, perhaps an -L flag is missing?
Could not compile `foo-sys`.

这是我的 foo-sys 包中的 build.rs:

use std::old_io::{fs, Command};
use std::os;
use std::old_io::process::InheritFd;

fn main() {
let manifest_dir = Path::new(os::getenv("PWD").unwrap());
let out_dir = Path::new(os::getenv("OUT_DIR").unwrap());
let src_dir = manifest_dir.join("src");
let foo_dir = src_dir.join("foo");
let foo_lib = foo_dir.join("libfoo.a");

let mut make = Command::new("make");

assert!(make.cwd(&foo_dir)
.arg("release")
.stdout(InheritFd(1))
.stderr(InheritFd(2))
.status()
.unwrap()
.success());

if let Err(_) = fs::copy(&foo_lib, &out_dir.join("libfoo.a")) {
println!("ERROR COPYING libfoo.a");
}

println!("cargo:rustc-flags=-L {} -l foo:static", out_dir.display());
}

让我感到困惑的是,如果您查看上面的 rustc 行,它有 -L-l foo:static 行从 build.rs 的末尾开始,但是找不到 libfoo.a

不知何故,make 命令没有运行,但 build.rs 脚本仍在运行?

最佳答案

尝试使用 CARGO_MANIFEST_DIR,而不是使用 PWD。文档说:

CARGO_MANIFEST_DIR - The directory containing the manifest for the package being built (the package containing the build script). Also note that this is the value of the current working directory of the build script when it starts.

我认为这里有一个很好的区别 - 程序可能在正确的目录中运行,但是 PWD 变量没有更新以匹配它。如果这是真的,这可能是 Cargo 错误。

但是,没有理由到处复制 libfoo.a。相反,使用带 -L 的 foo_dir 而不是 out_dir,只需链接到库所在的位置。

关于rust - 取决于 -sys 包装器库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28597751/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com