Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

c++ - 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?

question from:https://stackoverflow.com/questions/65879489/how-to-compile-and-link-cpp-file-in-rust

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...