include stdio include dlfcn typedef int export_fn int main void lib_ha

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <dlfcn.h>
typedef int(*export_fn)();
int main()
{
void* lib_handle = dlopen("./shared.so", RTLD_LAZY);
if(!lib_handle){
printf("ERROR: %s\n", dlerror());
return 1;
}
export_fn foo = dlsym(lib_handle, "shared_foo");
if(!foo){
printf("ERROR: %s\n", dlerror());
return 1;
}
export_fn bar = dlsym(lib_handle, "shared_bar");
if(!bar){
printf("ERROR: %s\n", dlerror());
return 1;
}
puts("Calling foo\n");
foo();
puts("Calling bar\n");
bar();
puts("All success\n");
dlclose(lib_handle);
return 0;
}