41

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?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
lucperkins
  • 768
  • 1
  • 7
  • 15

5 Answers5

40

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.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • `extern crate` is still useful in modern Rust when combined with `macro_use`: https://doc.rust-lang.org/reference/macros-by-example.html?highlight=macro_use#the-macro_use-attribute – hkBst Oct 10 '22 at 13:50
  • 1
    @hkBst any reasonably maintained / updated crate will expose macros that can be imported using the normal `use ...` syntax. I haven't _needed_ to use `#[macro_use]` for several years now. – Shepmaster Oct 10 '22 at 14:54
  • Ah yes, I see. Might it be useful to add something about this in the `macro_use` reference? – hkBst Oct 11 '22 at 10:24
23

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

nameless
  • 1,969
  • 11
  • 34
  • Note: Add the **directory**, not the file! Say you have libhttp.dylib in the directory ../rust-http/target/debug, then build with `rustc -L ../rust-http/target/debug your-source-file.rs`. – nalply May 25 '15 at 14:58
  • 2
    This answer is correct, but almost no modern user of Rust will follow these instructions. Instead, prefer to [use Cargo](https://stackoverflow.com/a/50307802/155423). – Shepmaster May 12 '18 at 15:26
4

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.

nomad
  • 471
  • 6
  • 12
2

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.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
0

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;
Kuberchaun
  • 29,160
  • 7
  • 51
  • 59
  • See more info https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html. Your able to point to branches and tags as well instead of the latest current branch. – Kuberchaun Jul 07 '20 at 16:43