forked from RedHatOfficial/GoCourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample1.c
More file actions
44 lines (36 loc) · 944 Bytes
/
Copy pathexample1.c
File metadata and controls
44 lines (36 loc) · 944 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include "example1.h"
int main()
{
void *library;
void (*hello)();
library = dlopen("./so1.so", RTLD_LAZY);
if (library != NULL) {
printf("dynamic library loaded: %p\n", library);
} else {
puts("unable to load dynamic library");
return 1;
}
hello = dlsym(library, "hello");
hello = dlsym(library, "hello");
if (hello != NULL) {
printf("address for 'hello' retrieved: %p\n", (void*)hello);
puts("Calling 'hello'...");
hello();
puts("...called");
} else {
puts("unable to retrieve address for 'hello'");
}
if (library != NULL) {
int err = dlclose(library);
if (err != 0) {
puts("unable to close dynamic library");
return 1;
} else {
puts("dynamic library closed");
}
}
return EXIT_SUCCESS;
}