user1930502
user1930502

Reputation:

How to add already built rust library into cargo build?

I have a project, let's call this project Master. This Master projects depends on other library I've already written and that is separated in different project, let's call this project Library.

Adding git dependencies or unbuilt project dependencies into rust is pretty straightforward. But what if I want to pick already built Library.librs, tell cargo to use it while compiling Master (output Master.exe) so I can ship the whole program with hierarchy like this:

-Master
--lib
---Library.librs
--Master.exe

Can this be done with cargo, or do I have to use rustc with -L parameter ? If so, how exactly ? Thank you for any helpful ideas.

Upvotes: 11

Views: 6901

Answers (1)

Gilles
Gilles

Reputation: 5407

Cargo usually puts the main src folder as src, so I will assume that is where it is.

Your Cargo.toml file in Master folder will look like (it will be in the root above /src):

[package]
name = "Master"
version = "0.0.1"
authors = ["You"]

[dependencies.Library_lib]
path = "src/lib"

Then in /src you can have the source code for your project Master. In /src/lib put another Cargo.toml file for your Library:

[package]
name  = "Library_lib"
version = "0.0.1"
authors = ["You"]

[lib]
name = "Library_lib"
path = "lib.rs"

Finally in your /src and in /src/lib put a lib.rs file (in each place).

In src/main.rs you can reference your library likewise:

extern crate Library_lib;

use Library_lib::something_to_import_from_lib;

This is where you reference the crate and have your use statement so you can use the stuff from lib.

In /src/lib/lib.rs you declare with the pub keyword the units you want exposed.

Upvotes: 10

Related Questions