问题
Can I use Cargo to build Rust code without using its standard project layout?
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
?
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.
回答1:
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"
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.
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.
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.
来源:https://stackoverflow.com/questions/39186795/using-cargo-with-my-projects-own-directory-structure