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

Implementing Rust method in C++ results in "not FFI-safe" error #1410

Open
topjohnwu opened this issue Dec 3, 2024 · 1 comment
Open

Implementing Rust method in C++ results in "not FFI-safe" error #1410

topjohnwu opened this issue Dec 3, 2024 · 1 comment

Comments

@topjohnwu
Copy link

I'm trying to expose a Rust defined type and implement one of its method in C++

#[cxx::bridge]
pub mod ffi {
    extern "Rust" {
        type Test;
    }

    unsafe extern "C++" {
        fn bar(self: &Test);
    }
}

struct Test {
    foo: String,
}

Results in:

error: `extern` block uses type `Test`, which is not FFI-safe
  --> lib.rs:36:22
   |
36 |         fn bar(self: &Test);
   |                      ^^^^^ not FFI-safe
   |
   = help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
   = note: this struct has unspecified layout

The generated C++ bindings are correct and can be successfully compiled.

@hunger
Copy link

hunger commented Dec 20, 2024

Read the error message, it explains what you need to do... slap a #[repr(C)] on the struct Test.

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

2 participants