Calling C++ functions from your Rust code

ghoulkingr

Chigozie Oduah

Posted on June 10, 2023

Calling C++ functions from your Rust code

C++ provides an extern keyword allowing you to specify functions other programming languages, including Rust, can interact with.

Calling this function requires you to provide parts you want other languages to interact with in your C++ code, compile it, and link it to your Rust project. And, In this article, I’ll go into the details of doing it.

Why would you want to do this?

You can import from one language to another for several reasons. These reasons may include the following:

  • Efficiency: Functions written in one language may be more efficient than if you write them in the other language.

  • Readability: Sometimes, reading and writing functions in one language are more manageable than in the other.

  • Availability of support: One of the languages may have the necessary libraries you need to carry out a task that the other may not have.

The list goes on, but let’s look at the next section for now and learn how to import and bind C++ functions in Rust.

Calling C++ functions from your Rust code

In this section, I’ll show you how to run C++ functions from your Rust code.

To start, you need to prepare your C++ file. Let’s imagine you have a main.cpp file in your src/, and it contains this code in it:

// src/main.cpp
extern "C" {
  bool UniqueFunction(int a, int b) {
    return true;
  }
}
Enter fullscreen mode Exit fullscreen mode

To prepare this C++ code, follow these steps:

  1. Create a build.rs file in src/.
  2. Copy the code below into build.rs:

    // src/build.rs
    fn main() {
    cc::Build::new()
    .cpp(true)
    .file("src/main.cpp")
    .compile("main.a");
    }
    
  3. Add a pointer to build.rs in Cargo.toml under [packages]

    [package]
    # ...
    build = "src/build.rs"
    

build.rs uses the cc library, so install it by adding the package name under [build-dependencies] in Cargo.toml.

[build-dependencies]
cc = "1.0"
Enter fullscreen mode Exit fullscreen mode

Now you can easily call UniqueFunction from your Rust code like below:

// src/main.rs
extern "C" {
  fn UniqueFunction(a: i32, b: i32) -> bool;
}

fn main() {
  unsafe {
    println!("Value: {}", UniqueFunction(23, 4));
  }
}
Enter fullscreen mode Exit fullscreen mode

Can you call Rust functions from C++?

A short answer to this question is “Not easily.”To get C++ to call your Rust function is always different. Calling Rust functions from C++ varies depending on how you are calling the function and what your function does.

I’m not covering that in this article, but I’ll give you resources that may help.

💖 💪 🙅 🚩
ghoulkingr
Chigozie Oduah

Posted on June 10, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related