--- Makefile all main foobar so main main gcc main -export-dynamic -ld

 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
34
35
36
37
--- Makefile
all: main foobar.so
main: main.c
gcc main.c -export-dynamic -ldl -o main
foobar.so: foobar.c
gcc foobar.c -shared -o foobar.so
--- foobar.c
#include <stdio.h>
void foobar_run()
{
puts("foobar_run");
exported();
}
--- main.c
#include <stdio.h>
#include <dlfcn.h>
void exported()
{
puts("expoted");
}
typedef void (*foonction)(void);
int main(int argc, char **argv)
{
puts("main");
void *lib = dlopen("./foobar.so", RTLD_LOCAL | RTLD_NOW);
printf("lib: %p, error: %s\n", lib, dlerror());
foonction run = dlsym(lib, "foobar_run");
printf("run: %p\n", run);
run();
return 0;
}