Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the official Rust guidance for interoperability with C++, in particular passing and returning structs as arguments?

Tags:

c++

c

rust

I'm trying to adapt some layers of existing C++ code to be used by Rust and apparently the way is through a C API.

For example, one function might return a struct as an object

#pragma pack(push,4)
struct Result {
    char ch;
    int32_t sum1;
    int32_t sum2;
};
#pragma pack(pop)

extern "C"
Result
muladd(int32_t a, int32_t b, int32_t c) {
    return Result{1, a+b*c, a*b+c};
}

And from Rust I'm doing

#[repr(C,align(4))]
pub struct Result {
    pub ch: i8,
    pub sum1: i32,
    pub sum2: i32,
}

extern "C" {
    pub fn muladd( a:i32, b:i32, c:i32 ) -> Result;
}

pub fn usemuladd( val: i32 ) -> i32 {
    unsafe {
       let res = muladd( val, val, val );
       return res.sum1;
    }
}

I'm seeing odd results with respect to alignment and packing of structures. I have read that Rust can play around with structs, and neither ordering or packing are guaranteed.

It seems that using #[repr(C)] and extern "C" is the key to a happy compatibility layer. My question is then: can I trust that these two will get me solid through or there will always be unannounced edge cases that I have to worry about?

https://godbolt.org/z/aEh4jKxxf


2 Answers

extern "C" on both sides + #[repr(C)] on the Rust side + only using C-compatible types for interfacing between C++ and Rust, should work.

Alternatively, see cxx and autocxx.

like image 66
Solomon Ucko Avatar answered Feb 01 '26 06:02

Solomon Ucko


If you're looking to get a translation between the codes, you could use cbindgen and specify the output as C++ which is already default, or you could follow Solomon Ucko's suggestion.

like image 41
Matheus C. França Avatar answered Feb 01 '26 05:02

Matheus C. França