Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Including bridge from another crate #1347

Open
HectorMRC opened this issue May 13, 2024 · 0 comments
Open

Including bridge from another crate #1347

HectorMRC opened this issue May 13, 2024 · 0 comments

Comments

@HectorMRC
Copy link

HectorMRC commented May 13, 2024

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:

  • If I add the include!("core/src/lib.rs.h") line in the "consumer" crate the build fails because the file does not exists. Which make sense, there is no file with such a name. I just tried this based on issue Define shared struct in separate file  #1077 .
  • But if I don't put that line then the build process fails because of the following errors:
...
  cargo:warning=      |                                                    ^
  cargo:warning=/home/hector/git/hectormrc/cxx-bridge/target/debug/build/consumer-86186167f538f322/out/cxxbridge/sources/consumer/src/lib.rs.cc:86:57: error: ‘::MySharedStruct’ has not been declared
  cargo:warning=   86 | void cxxbridge1$do_another_thing(::MySharedStruct *t, ::MySharedStruct *return$) noexcept;
  cargo:warning=      |                                                         ^~~~~~~~~~~~~~
  cargo:warning=/home/hector/git/hectormrc/cxx-bridge/target/debug/build/consumer-86186167f538f322/out/cxxbridge/sources/consumer/src/lib.rs.cc:86:73: error: ‘return$’ was not declared in this scope
  cargo:warning=   86 | void cxxbridge1$do_another_thing(::MySharedStruct *t, ::MySharedStruct *return$) noexcept;
  cargo:warning=      |                                                                         ^~~~~~~
  cargo:warning=/home/hector/git/hectormrc/cxx-bridge/target/debug/build/consumer-86186167f538f322/out/cxxbridge/sources/consumer/src/lib.rs.cc:89:3: error: ‘MySharedStruct’ in namespace ‘::’ does not name a type
  cargo:warning=   89 | ::MySharedStruct do_another_thing(::MySharedStruct t) noexcept {
  cargo:warning=      |
... 
--- stderr

  CXX include path:
    /home/hector/git/hectormrc/cxx-bridge/target/debug/build/consumer-86186167f538f322/out/cxxbridge/include
    /home/hector/git/hectormrc/cxx-bridge/target/debug/build/consumer-86186167f538f322/out/cxxbridge/crate

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:

// 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant