dtolnay / cxx

Safe interop between Rust and C++
https://cxx.rs
Apache License 2.0
5.68k stars 320 forks source link

Including bridge from another crate #1347

Open HectorMRC opened 1 month ago

HectorMRC commented 1 month ago

Hi, I am trying to share a bunch of shared types between crates in the same workspace. However, when running the build command it complains of two things:

The full example can be found here: cxx bridge example But in a nutshell my "core" crate is exposing a dummy type like this:

// core/src/lib.rs

#[cxx::bridge]
pub mod cxx_bridge {
    struct MySharedStruct {
        value: Vec<f32>,
    }
}

And the consumer is trying to use it like the following:

The consumer is declared with crate-type = ["staticlib", "rlib"]


// consumer/src/lib.rs

[cxx::bridge]

mod cxx_bridge { unsafe extern "C++" { // include!("core/src/lib.rs.h");

    type MySharedStruct = core::cxx_bridge::MySharedStruct;

    fn do_something(t: MySharedStruct) -> MySharedStruct;
}

extern "Rust" {
    fn do_another_thing(t: MySharedStruct) -> MySharedStruct;
}

}

use core::cxx_bridge::MySharedStruct;

fn do_another_thing(t: MySharedStruct) -> MySharedStruct { cxx_bridge::do_something(t) }


Finally, everything is meant to get build together using the following `build.rs` inside the consumer crate:
``` rust
// consumer/build.rs

fn main() {
    cxx_build::bridge("src/lib.rs")
        .std("c++11")
        .compile("client");
}

What am I missing here? Using cxx_build::bridges the same thing happens. I also tried to generate first the headers files for the core crate, and then import them via include! to the consumer one, but it does not seams to find them out.