Search code examples
cgccrustdynamic-linkingdlsym

How to dynamically load a Rust library in C?


I'm trying to dynamically link a Rust .so library with a C program in Linux with gcc. Using only dlopen and dlclose results in no errors, but I get a segmentation fault with the following code:

char * (*my_function)(char *);
char *my_input_string = "foo";

//...abbreviated code...

my_function = dlsym(handle, "my_function");
char *my_output_string = (*my_function)(my_input_string);

I'm using gcc -g foo.c -ldl -o foo to compile, if that matters.

I know that it's finding the symbol because I used #[no_mangle] in the rust library and the my_function pointer is not null


Solution

  • I ran it through gdb and it turns out the library was trying to call a function in the calling program that didn't exist.