-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdyndl-main.c
59 lines (43 loc) · 1.07 KB
/
dyndl-main.c
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <dlfcn.h>
#include <stdio.h>
typedef void (*fancy_printing_fun) (char *str);
/* kind of awkward to place these in main
but let's do that for now and be a bit
better in the coming chat example */
/* pinter to the print function the so file */
fancy_printing_fun fancy_printing;
/* hande to the so file */
void *handle;
int load_lib(char *libname)
{
char *error;
handle = dlopen(libname, RTLD_LAZY);
if (!handle) {
fputs (dlerror(), stderr);
return 1;
}
fancy_printing = dlsym(handle, "fancy_printing");
if ((error = dlerror()) != NULL) {
fputs(error, stderr);
return 2;
}
return 0;
}
int main(int argc, char **argv)
{
int ret;
if (argc<2)
{
fprintf(stderr, "\nMissing arguments... you must supply the name of a shared object file\n");
return 1;
}
ret = load_lib(argv[1]);
if (ret!=0)
{
fprintf(stderr, "\nFailed reading file '%s'\n", argv[1]);
return 2;
}
fancy_printing("Wow, we called a function in a file this program had no idea existed\n");
dlclose(handle);
return 0;
}