Linking rust ffi library to cc_library_static

Hello people,
I am trying to call rust functions from cpp. I am using soong build system. I have a cpp file callToRust.cpp as follows:

extern "C"
{
 int in_rust();
}
void foo()
{
  int ret = in_rust();
}

and I am building it as a static library as follows:
In the Android.bp file:

cc_library_static{
    name:"clibrary",
    vendor_available:true,
    srcs:["callToRust.cpp"],
    shared_libs:[]
}

i have a rust file mylib.rs having the function:

#[no_mangle]
 pub extern fn in_rust()->i32{
     todo!()
 }

I am building this file as a rust_ffi.

rust_ffi{
    name:librust_fii_call,
    vendor:true,
    crate_name:rust_ffi_call,
    srcs:["mylib.rs"],
},

building this way , gives the error as undefined symbol in_rust() in callToRust.cpp.
So , i added the rust_ffi library : "librust_fii_call" , in the shared_libs of the clibrary , but it also gives error as missing dependencies librust_fii_call.

That's only a declaration, not a definition. It looks like you never actually implemented the Rust function.


By the way, your code is full of syntax errors and is very hard to read in the absence of proper formatting. If you want to get useful advice, help those trying to help you. Dumping a wall of unformatted code does not make a great question.

sorry for the confusion, i made the required changes please have a look at it.

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.