Hi,
I have a lot of C++ static libraries that have to be called from Rust now.
Compiled recently using Clang 17, on Linux, for Linux only usage.
Luckily, there is only one entry point, for which I created a C binding.
It means that the other libraries are all invoked from this entry point. I don't call those other functions in Rust.
On a local test environment with a very simple test case (one c++ static lib with a c binding included), it worked.
Now, the issue is that I with all my static libs, I end up with many undefined references.
I also have the search path redefined before hand in the build.rs
println!("cargo:rustc-link-search=/my/path/to/statics/libs/");
println!("cargo:rustc-link-search=/usr/lib/");
println!("cargo:rustc-link-search=/usr/lib/x86_64-linux-gnu/");
println!("cargo:rustc-link-search=/usr/local/include/");
println!("cargo:rustc-link-search=/usr/include/x86_64-linux-gnu/");
...
println!("cargo:rustc-link-lib=static=some_lib");
println!("cargo:rustc-link-lib=static=opencv_core");
println!("cargo:rustc-link-lib=static=gmp");
...
All the libs belong to those paths.
The entry point in my main.rs, regarding the extern "C":
extern "C" {
fn BINDING_call_my_entry_point(
ptr: *const u8,
size: u32,
ptr_out: *mut u8,
max_length_in_exact_length_out: *mut u32,
);
}
in the main function:
fn main() {
...
unsafe {
BINDING_call_my_entry_point(
body.as_ptr(),
body.len() as u32,
v.as_mut_ptr(),
&mut len_v,
);
};
...
I took the same order of compilation than my already existing CMake (needless to say it works in a pure c++ environment).
I tried to link cc to clang-17, just in case, same result.
The issue seems to be that the linking process is pure C and I am linking some libs which are C++ without extern inside, except for the one with my entry point. Name mangling playing its role here.
I can't write extern everywhere otherwise the c++ part cannot compile anymore.
I can't rewrite everything in Rust either.
The alternative I immediately but which suck performance wise is to create a binary and call it from rust.
I don't want that.
Cxx-rs deals with source code. Mine is already in a static lib.
So I need to link using a linker that accepts C++ for the generation and present my extern "C" entry point to rust as pure C code.
How to do that ?