在我项目的目录结构中使用Cargo [英] Using Cargo with my project's own directory structure
问题描述
我可以使用Cargo来构建Rust代码而不使用其标准项目布局?
Can I use Cargo to build Rust code without using its standard project layout?
我的源文件不在名为 src
的目录中,并且不会改变。我的二进制文件必须最终位于当前目录(或在其他一些项目中,位于另一个未称为 target / SOMETHING
的目录中)。我可以告诉Cargo可执行文件 foo
必须由 foo.rs
和 bar.rs构建
与 Cargo.toml
在同一目录中,以及 qux
来自 foo.rs
?
My source files are not in a directory called src
and this will not change. My binaries must end up in the current directory (or, in some other projects, in a different directory that is not called target/SOMETHING
). Can I tell Cargo that executable foo
must be built from foo.rs
and bar.rs
in the same directory as Cargo.toml
, and qux
from foo.rs
?
我不在乎Cargo是构建系统还是部署系统。我只对它作为图书馆管理系统感兴趣。 显然,货运是鲁斯镇唯一的游戏。
I don't care about Cargo as a build system or as a deployment system. I'm only interested in it as a library management system. Apparently Cargo is the only game in the Rust town for this.
推荐答案
不是真的 。您可以控制源文件的位置通过在清单中明确指定它们:
Not really. You can control where the source files are by explicitly specifying them in the manifest:
[[bin]]
name = "foo"
src = "foo.rs"
[[bin]]
name = "qux"
src = "splong.rs"
我不知道您是用 foo
是什么意思> foo.rs 和 bar.rs
,而 qux
仅由 foo.rs
构建。您不能随便将源文件放在一起: foo.rs
使用 bar.rs
,否则它不会。 t。
I don't know what you mean by foo
being built from foo.rs
and bar.rs
, whilst qux
is built only from foo.rs
. You can't just arbitrarily glob source files together: either foo.rs
uses bar.rs
, or it doesn't.
但是您不能从清单中控制目标目录。有 build.target-dir
设置(在货物的配置中,而不是清单),但这只允许您更改 target
目录,而不是其中的第二级目录。您可以使用 CARGO_TARGET_DIR
环境变量,尽管您也不能从清单中设置环境变量。
But you can't control the target directory from within the manifest. There's the build.target-dir
setting (in Cargo's configuration, not the manifest), but that only lets you change the target
directory, not the second level inside of it. You can change it using the CARGO_TARGET_DIR
environment variable, though you can't set environment variables from within the manifest, either.
也就是说,设置 CARGO_TARGET_DIR
到项目根目录也会 更改所有中间文件的位置, 这意味着每次您在调试之间切换和发行版本,则必须进行完全重新编译。
That said, setting CARGO_TARGET_DIR
to the root of the project will also change where all the intermediate files go, and it'll mean every time you switch between debug and release builds, you'll have to do a full recompile.
您可能要考虑在货运问题跟踪器有关将此选项设为可选项。
You may want to consider opening an issue on the Cargo issue tracker about making this an option.
这篇关于在我项目的目录结构中使用Cargo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!