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?

有帮助吗?

解决方案 2

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

其他提示

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:

  1. Create a project using cargo new

  2. Edit the generated Cargo.toml file to add dependencies:

    [dependencies]
    old-http = "0.1.0-pre"
    
  3. Access the crate in your code:

    Rust 2021 and 2018

    use old_http::SomeType;
    

    Rust 2015

    extern crate old_http;
    use old_http::SomeType;
    
  4. 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 to link together all of the dependencies.

Read The Rust Programming Language for further details on getting started with Cargo. Specifying Dependencies in the Cargo book has details about what kinds of dependencies you can add.

Not related to your post, but it is to your title. Also, cargo based.

Best practice:

  1. external crate named foo
use ::foo;
  1. module (which is part of your code/crate) named foo
use crate::foo;

In both the cases, you can use use foo; instead, but it can lead to confusion.

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.

I ran into a similar issue. I ended up doing this in my Cargo.toml

[dependencies]
shell = { git = "https://github.com/google/rust-shell" }

Then in my main.rs I was able to add this and compile with success. Note that this cargo package is a macro in my case. Often you will not want to have the #[macro_use] before the extern call.

#[macro_use] extern crate shell;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top