Michael Day
Michael Day

Reputation: 1015

How to compile multiple Rust files into a single C library?

You can compile a Rust file to a C library like this:

rustc --crate-type=staticlib file.rs

But what if you have multiple Rust files, can you compile them into a single C library, or does each one have to be a different library? How does the Rust module system interact with building staticlibs?

Upvotes: 0

Views: 2436

Answers (1)

Jorge Israel Peña
Jorge Israel Peña

Reputation: 38626

Here, file.rs is your crate root, which is what results in the static library. So to add other files, you have to reference them from the crate root. That is, assuming you have a file other.rs, you could do something like mod other; in file.rs to effectively 'bring in' the contents of other.rs into file.rs.

See the guide for more information.

Upvotes: 4

Related Questions