How can I solve it? something may error when I pass a struct to a FFI function

The problem is When I pass s struct to a C function , the struct is defined as

#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct dim3 {
    pub x: u32,
    pub y: u32,
    pub z: u32,
}

And I have a function like that
extern "c" pub fn foo(grid: dim3)
this func is impl by a C lib.

When I pass the grid to dim3 ,

the C func foo get the grid like that: ( I disassemble the func code)
foo( grid_xy: u64,grid:u32)

but the rust pass the grid like that
foo( grid_x: u32,grid_y:u32,grid:u32)

when we pass all param by using memory call stack ,there is nothing wrong happened.It's OK.
But X64 pass the param by reg (RSI,RDI etc), so it's a problem~

I solve it by define the struct like it

#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct dim3_foo {
    pub x_y: u64,
    pub z: u32,
}

What I want to know , can I get a config when compile to pass the dim3 like (xy:u64,z:u32)?

And how is it defined in C? And what platform are you using?

The signature must match on both sides of FFI. You can't have one side use a signature with a different number of function arguments than the other. It doesn't matter if it sometimes looks like it works; to mismatch signature like that is simply forbidden.

It smells like a calling convention mismatch. If you're relying on disassembly of the dylib instead of a header, knowing for sure is difficult. If this is Windows, it's worth noting DLLs often choose to export functions with extern "system" instead of extern "C" for assorted reasons.

The c lib is on Linux, and I think is the foo in C define maybe
int foo( dim3 para)
and I wiil test the extern "system"
Thx a lot

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.