44

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
  • Small note: extern mod has been recently renamed to extern crate.
    – barjak
    Commented Feb 18, 2014 at 8:29

5 Answers 5

45

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.

4
  • extern crate is still useful in modern Rust when combined with macro_use: doc.rust-lang.org/reference/…
    – hkBst
    Commented Oct 10, 2022 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
    Commented Oct 10, 2022 at 14:54
  • Ah yes, I see. Might it be useful to add something about this in the macro_use reference?
    – hkBst
    Commented Oct 11, 2022 at 10:24
  • Nowadays, would you instead use cargo add old-http to add a crate to a project?
    – idbrii
    Commented Mar 26 at 4:57
24

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
  • 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
    Commented May 25, 2015 at 14:58
  • 2
    This answer is correct, but almost no modern user of Rust will follow these instructions. Instead, prefer to use Cargo.
    – Shepmaster
    Commented May 12, 2018 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.

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.

1
  • how to run rustc with rand pls? rustc -L rust-rand doesn't work
    – Dan D.
    Commented May 22, 2020 at 0:25
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;
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.