use libloading;
fn call_dynamic() -> Result<u32, Box<dyn std::error::Error>> {
unsafe {
let lib = libloading::Library::new("/home/com/dev/cpp/libhello.so")?;
let func: libloading::Symbol<unsafe extern "C" fn() -> u32> = lib.get(b"hello")?;
Ok(func())
}
}
fn main() {
let x = call_dynamic();
println!("{:?}", x);
}
#ifndef hello_h__
#define hello_h__
#include <stdio.h>
#include <stdlib.h>
void hello();
#endif
#include "hello.h"
void hello(){
printf("hello from C \n");
}
cd /home/com/dev/cpp
gcc -shared -fPIC hello.c -o libhello.so
hello from C
Ok(14)