PPP
PPP

Reputation: 1850

How to compile and link .cpp file in Rust?

How do I compile and link a .cpp file in a Rust project?

Suppose that I have a Rust project and I want to call some extern "C" functions, from Rust, that are C++ or C functions.

What should be the simplest way of doing it?

Upvotes: 4

Views: 1513

Answers (1)

PPP
PPP

Reputation: 1850

For complicated projects, cmake crate should be useful, but for small projects, just use Rust's build.rs.

In the build.rs, do

fn main() {
    cc::Build::new()
        .cpp(true)
        .file("src/my_lib.cpp")
        .compile("lib_my_lib.a");
}

which should be placed in the root of the project. Then simply run cargo build. For C files, take off the .cpp(true)

Upvotes: 6

Related Questions