-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathdlopen.c
35 lines (31 loc) · 830 Bytes
/
dlopen.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
/* Used by: https://unix.stackexchange.com/questions/226524/what-system-call-is-used-to-load-libraries-in-linux/462710#462710 */
#define _XOPEN_SOURCE 700
#include <assert.h>
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include "a.h"
#include "b.h"
int main(void) {
void *handle;
int (*a)(void);
int (*b)(void);
char *error;
handle = dlopen("libcirosantilli_ab.so", RTLD_LAZY);
if (!handle) {
fprintf(stderr, "%s\n", dlerror());
exit(EXIT_FAILURE);
}
dlerror();
a = (int (*)(void)) dlsym(handle, "a");
b = (int (*)(void)) dlsym(handle, "b");
error = dlerror();
if (error != NULL) {
fprintf(stderr, "%s\n", error);
exit(EXIT_FAILURE);
}
assert(a() == 1);
assert(b() == 2);
dlclose(handle);
return EXIT_SUCCESS;
}