rust - 用 cargo 构建子模块二进制箱

标签 rust rust-cargo

我有以下 crate 结构:

|── proj/
    └── src/
        └── bin/
            └── foo-bin-rs/
                └── src/
                    └── main.rs
                └── Cargo.toml
        └── main.rs
    └── Cargo.toml
    └── build.rs

foo-bin-rs 是一个子模块。我想找到一种干净的方式来发布构建 通过 cargo 命令构建 foo-bin-rs 作为构建的一部分 用于 proj 的命令。我没有找到任何使用 src/bin 目录中的二进制文件是它们自己单独的箱子,只是 单个文件。我的第一个想法是拥有一个发布自己的 build.rs cargo 命令,但我找不到允许通过 用作 root 的目录。对此的规范解决方案是什么?

最佳答案

My first thought was to have a build.rs that issued its own cargo command, but I couldn't find a flag for cargo that allowed passing a directory to use as root.

有一个命令行参数,但它不需要目录;相反,它需要 Cargo.toml 文件的完整路径。该参数名为 --manifest-path,它可用于许多子命令,例如 buildrun。它是这样使用的(注意相对路径也是有效的):

$ cargo build --manifest-path=/path/to/proj/src/bin/foo-bin-rs/Cargo.toml

如果您需要从构建脚本运行可执行文件,您可以像往常一样简单地使用 cargo run 一次性构建和运行。

关于rust - 用 cargo 构建子模块二进制箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43882955/

相关文章:

rust - 当赋值发生在for循环中时,使用引用分配变量如何与rust一起使用?

rust - Rust 中相对向量大小调整的一行代码

rust - `cargo package` 错误 : extern location for foo does not exist

rust - 为什么 `std::mem::drop` 与高级特征边界中的闭包 |_|() 不完全相同?

rust - 如何使用可选的内部标签反序列化枚举?

rust - Rust 中的 MaybeUninit 和 ZST/uninhabbited/one-representation 类型的初始化

dependencies - 如果相关功能被禁用,如何跳过依赖项

testing - Rust:带有 lib 和二进制目标的 crate 中的属性 #[cfg(test)]

rust - 当库名称与包名称不同时如何导入 crate 依赖项?

testing - cargo test --release 导致堆栈溢出。为什么没有货台?