| 1 | #include <stdio.h> |
| 2 | #include <dlfcn.h> |
| 3 | |
| 4 | typedef int (*func)(int, int); |
| 5 | func add, sub, mul, div; |
| 6 | |
| 7 | int main() |
| 8 | { |
| 9 | int a = 5, b = 4; |
| 10 | void *arithHandle = NULL; |
| 11 | |
| 12 | arithHandle = dlopen("libarithmetic.so", RTLD_NOW); |
| 13 | if ( NULL == arithHandle ) |
| 14 | { |
| 15 | printf("Arithmetic library cant be opened\n"); |
| 16 | } |
| 17 | |
| 18 | add = (func) dlsym (arithHandle, "add"); |
| 19 | sub = (func) dlsym (arithHandle, "sub"); |
| 20 | mul = (func) dlsym (arithHandle, "mul"); |
| 21 | div = (func) dlsym (arithHandle, "div"); |
| 22 | |
| 23 | printf("Add :: %d\n", add(a, b)); |
| 24 | printf("Sub :: %d\n", sub(a, b)); |
| 25 | printf("Mul :: %d\n", mul(a, b)); |
| 26 | printf("Div :: %d\n", div(a, b)); |
| 27 | |
| 28 | dlclose(arithHandle); //Its important to call dlclose() when you are done |
| 29 | return 0; |
| 30 | } |