问题
I'm trying to work with the rust-http library, and I'd like to use it as the basis for a small project.
I have no idea how to use something that I can't install via rustpkg install <remote_url>
. In fact, I found out today that rustpkg
is now deprecated.
If I git clone
the library and run the appropriate make
commands to get it built, how do I use it elsewhere? I.e. how do I actually use extern crate http
?
回答1:
Update
For modern Rust, see this answer.
Original answer
You need to pass the -L
flag to rustc
to add the directory which contains the compiled http library to the search path. Something like rustc -L path-to-cloned-rust-http-repo/build your-source-file.rs
should do.
Tutorial reference
回答2:
Since Rust 1.0, 99% of all users will use Cargo to manage the dependencies of a project. The TL;DR of the documentation is:
- Create a project using
cargo new
Edit the generated
Cargo.toml
file to add dependencies:[dependencies] old-http = "0.1.0-pre"
Access the crate in your code:
Rust 2015
extern crate old_http; use old_http::SomeType;
Rust 2018
use old_http::SomeType;
Build the project with
cargo build
Cargo will take care of managing the versions, building the dependencies when needed, and passing the correct arguments to the compiler.
Read The Rust Programming Language for further details on getting started with Cargo.
回答3:
Once you've built it, you can use the normal extern crate http;
in your code. The only trick is that you need to pass the appropriate -L
flag to rustc
to tell it where to find libhttp
.
If you have a submodule in your project in the rust-http
directory, and if it builds into its root (I don't actually know where make
in rust-http
deposits the resulting library), then you can build your own project with rustc -L rust-http pkg.rs
. With that -L
flag, the extern crate http;
line in your pkg.rs
will be able to find libhttp
in the rust-http
subfolder.
来源:https://stackoverflow.com/questions/21655032/how-do-i-use-external-crates-in-rust