【发布时间】:2021-03-08 18:27:56
【问题描述】:
我想从我不维护的 Rust 项目生成一个静态库。该项目允许构建一个动态库——Cargo.toml 指定crate-type = ["cdylib"]。
修改文件中的 crate 类型有效,但如果可能的话,我想将未修改的原始项目作为 git 子模块保留在我的项目中。
是否有任何标志可以传递给cargo build 命令来覆盖此设置?
【问题讨论】:
标签: rust rust-cargo
我想从我不维护的 Rust 项目生成一个静态库。该项目允许构建一个动态库——Cargo.toml 指定crate-type = ["cdylib"]。
修改文件中的 crate 类型有效,但如果可能的话,我想将未修改的原始项目作为 git 子模块保留在我的项目中。
是否有任何标志可以传递给cargo build 命令来覆盖此设置?
【问题讨论】:
标签: rust rust-cargo
你不能覆盖它,但你可以补充它。使用cargo rustc 并将--crate-type=staticlib 直接传递给编译器:
% cargo build
Compiling example v0.1.0 (/private/tmp/example)
Finished dev [unoptimized + debuginfo] target(s) in 0.31s
% find target -name '*.a'
% cargo rustc -- --crate-type=staticlib
Compiling example v0.1.0 (/private/tmp/example)
Finished dev [unoptimized + debuginfo] target(s) in 0.29s
% find target -name '*.a'
target/debug/deps/libexample.a
【讨论】:
cargo rustc --release -- --crate-type=staticlib),我在target/release/deps 中找到了静态库。我仍然不得不承认,作为一个几乎没有 rust 经验的 C++ 程序员,我不确定这个调用与cargo build 相比实际上在做什么或不做什么?除了尊重我传递给它的 crate-type 之外,它还会使用Cargo.toml 吗?找出我现在可能或可能不完全理解的依赖项和其他设置?为什么我现在在不同的目标文件夹中找到生成的库?
cargo rustc 添加了指向文档的链接。 TL;DR — 您可以使用它来将更多标志传递给 Cargo 将进行的 rustc 的最终调用。是的,依赖项仍然会受到尊重。我不完全理解为什么输出保留在deps 目录中,但我认为 Cargo 总是在其中构建,然后链接/复制到“预期的”输出位置。此处跳过该副本。
您可以提供crate-type,但不能覆盖Cargo.toml 中指定的那个:
$ cargo rustc -- --crate-type=staticlib
Compiling example v0.1.0 (/dev/tmp)
Finished dev [unoptimized + debuginfo] target(s) in 0.34s
有一个 tracking issue 可以添加 --crate-type 覆盖。同时,一种解决方法是使用cargo-crate-type:
$ cargo install cargo-crate-type
$ cargo crate-type static
$ cargo build
请注意,此命令将更改您的 Cargo.toml
【讨论】: