Ticket #14531: dl.c

File dl.c, 504 bytes (added by leorize, 6 years ago)
Line 
1#include <dlfcn.h>
2#include <stdio.h>
3
4#define LIBRARY_NAME "dl.so"
5
6void printSomething() {
7#ifdef LIBRARY
8 puts("from library");
9#else
10 puts("from program");
11#endif
12}
13
14#ifdef LIBRARY
15void doSomething() {
16 printSomething();
17}
18#endif
19
20#ifndef LIBRARY
21int main() {
22 void* handle = dlopen(LIBRARY_NAME, RTLD_NOW);
23 if (handle == NULL) {
24 puts(dlerror());
25 return 1;
26 }
27 void (*doSmt)() = dlsym(handle, "doSomething");
28 if (doSmt == NULL) {
29 puts(dlerror());
30 return 1;
31 }
32 doSmt();
33 return 0;
34}
35#endif