#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
本文展示了一个使用 C 语言编写的程序如何通过动态链接库(DLL)来调用一个简单的加法函数。该示例包括了库文件的创建、加载及函数的调用过程,并演示了如何正确地关闭动态库。
3140

被折叠的 条评论
为什么被折叠?



