In the first step you should choose "main" crate that would be the bridge between Rust and other language. It should have crate-type cdylib or staticlib, see suitable Rust book's section for a detailed description.

For example:

name = "cpp_example_rust_part"
crate-type = ["cdylib"]

Then you should create inside this crate. For example:

use flapigen::{CppConfig, CppOptional, CppStrView, CppVariant, LanguageConfig};
use std::{env, path::Path};

fn main() {
    let out_dir = env::var("OUT_DIR").expect("no OUT_DIR, but cargo should provide it");
    //ANCHOR: cpp_config
    let cpp_cfg = CppConfig::new(
        // ANCHOR: cpp_output
        // ANCHOR_END: cpp_output
    //ANCHOR_END: cpp_config
    let swig_gen = flapigen::Generator::new(LanguageConfig::CppConfig(cpp_cfg));
        // ANCHOR: rust_input
        // ANCHOR_END: rust_input

        // ANCHOR: rust_output
        // ANCHOR_END: rust_output

Here you instruct flapigen to generate C++/Rust code from "src/" as input:


and we specify our output, Rust file:


directory for C++ files:


You can find a detailed description of code generation in the suitable Cargo book section.

Then you should create a file for the "foreign" language API description. For example:

use super::{f2, Foo};

foreign_class!(class Foo {
    self_type Foo;
    constructor Foo::new(_: i32) -> Foo;
    fn Foo::set_field(&mut self, _: i32);
    fn Foo::f(&self, _: i32, _: i32) -> i32;
    fn f2(_: i32) -> i32;

And connect the generated code with your crate exactly how as in the Cargo book :

// src/
mod cpp_glue;
pub use crate::cpp_glue::*;
// src/
include!(concat!(env!("OUT_DIR"), "/"));

Do not forget to add flapigen as a dependency into [build-dependencies] section of your crate's Cargo.toml file and you are ready to go.