我可以阻止货物在每个新项目中重建库吗? [英] Can I prevent cargo from rebuilding libraries with every new project?
问题描述
假设我执行 cargo new one --bin
和 cargo new two --bin
然后将相同的依赖添加到每个项目的 Cargo.toml
> 并构建它们.
Suppose I execute cargo new one --bin
and cargo new two --bin
then add the same dependency to each project's Cargo.toml
and build them.
现在有两组完全相同的库:
Now there are two absolutely identical sets of libraries:
/one/target/debug/deps/*.rlib
/one/target/debug/deps/ *.rlib
/two/target/debug/deps/*.rlib
/two/target/debug/deps/ *.rlib
它们是相同的文件,浪费存储空间,但真正的问题是我必须为每个项目再次编译这些库.这需要很长时间.cargo install
也有同样的问题.
They are same files and waste storage space, but really the problem is that I have to compile these libraries again for every project. It takes a very much time. There is the same problem with cargo install
.
我可以指定一个存放已编译库的位置以避免重新编译吗?
Can I specify a place to store compiled libraries to avoid recompilation?
推荐答案
几个 Cargo 项目可能会使用相同的目标目录共享库.
Several Cargo projects might share the libraries by using the same target dir.
在项目中放置一个.cargo"文件夹并在其中创建一个config"文件,其中包含:
Place a ".cargo" folder in a project and create a "config" file there containing:
[build]
target-dir = "/path/to/your/shared/target/dir"
在 Unix 上,这可能如下所示:
On Unix this might look like:
mkdir ~/shared_rust_target
mkdir .cargo
echo "[build]" > .cargo/config
echo "target-dir = \"$HOME/shared_rust_target\"" >> .cargo/config
CARGO_TARGET_DIR
设置CARGO_TARGET_DIR
环境变量.
在 Unix 上,这可能如下所示:
On Unix this might look like:
export CARGO_TARGET_DIR = "$HOME/shared_rust_target"
<小时>
请参阅此提交以获得一些额外的target-dir
文档.
See this commit for some extra target-dir
documentation.
特别是,在 Cargo 1.9 之前,您不应该同时将多个项目构建到同一个目标目录中.(这里有更多关于 Cargo 1.9 如何支持并发构建).
In particular, prior to Cargo 1.9 you shouldn't build several projects into the same target dir concurrently. (Here's more on how Cargo 1.9 supports concurrent builds).
target-dir
在 中也有提到货物文件.
请注意,我个人仅使用 target-dir
功能将构建重定向到不同的位置,因此我从未尝试进行共享构建.但根据这个问题,它应该可以工作.
Note that personally I'm only using the target-dir
feature to redirect the builds into a different place, so I never tried to do the shared builds. But it should work, according to this issue.
附言现在还可以使用 工作区.
P.S. It is now also possible to achieve crate reuse with workspaces.
这篇关于我可以阻止货物在每个新项目中重建库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!