#include <stdio.h>
#include <dlfcn.h>
int main(void){
int (*myadd)(int a,int b);//fuction pointer
void *handle;
handle=dlopen("./libmyadd.so",RTLD_LAZY);//open lib file
myadd=dlsym(handle,"output");//call dlsym function
int result=myadd(1,2);
dlclose(handle);
printf("%d\n",result);
}
?以上为调用程序test8.c,以下为库程序test7.c
int output(int a,int b){
int x=a+b;
return x;
}
?knoppix@Microknoppix:/mnt-system/deepfuture$ gcc -shared -o libmyadd.so test7.c
knoppix@Microknoppix:/mnt-system/deepfuture$ gcc -ldl -o test8 test8.c
knoppix@Microknoppix:/mnt-system/deepfuture$ ./test8
3