1 #include <stdio.h>
2 #include <dlfcn.h>
3 
4 typedef void (*test_func_t)(int *px);
5 int  x;
6 
main(void)7 int main(void)
8 {
9     void*  lib = dlopen("libtest1.so", RTLD_NOW);
10     test_func_t test_func;
11 
12     if (lib == NULL) {
13         fprintf(stderr, "Can't load library: %s\n", dlerror());
14         return 1;
15     }
16 
17     printf("Loaded !\n");
18 
19     test_func = dlsym(lib, "test1_set");
20     if (test_func == NULL) {
21         fprintf(stderr, "Can't find test function\n");
22         return 2;
23     }
24 
25     x = 0;
26     test_func(&x);
27 
28     if (x == 1) {
29         printf("Test function called !\n");
30     } else {
31         fprintf(stderr, "Test function failed to set variable !\n");
32         return 3;
33     }
34 
35     dlclose(lib);
36     printf("Unloaded !\n");
37 
38     if (x == 2) {
39         printf("Test destructor called !\n");
40     } else if (x == 1) {
41         fprintf(stderr, "Test destructor was *not* called !\n");
42         return 4;
43     } else {
44         fprintf(stderr, "Test destructor called but returned invalid value (%d)\n", x);
45         return 5;
46     }
47     return 0;
48 }
49