linux下动态链接问题(.so文件的编写与调用)
2008-09-27 21:39
573 查看
实例:deposit.c save.c dig.h dy.c 这是一个及其简陋的银行界面(姑且这么说吧)
1.(deposit.c文件,取款)
#include "dig.h"
void deposit(){
printf("you deposit 5000$ /n");
}
2.(save.c文件,存款)
#include "dig.h"
void save(){
printf("you saved 1000$ /n");
}
3.(dig.h文件)
#ifndef _DIG_H
#define _DIG_H
#ifdef SHARED
void (*save)();
#else
void save();
#endif
#ifdef SHARED
void (*deposit)();
#else
void deposit();
#endif
#endif
4.(dy.c主函数调用两项功能)
#include <stdio.h>
#include <dlfcn.h>
#define SOFILE "./my.so"
#define SHARED
#include "dig.h"
main(){
void *dp;
char *error;
void (*func)();
while(1){
/*以下为调用.so文件,用到了函数dlopen8/
dp =dlopen(SOFILE,RTLD_LAZY);//打开.so文件,RTLD_LAZY为参数
if(dp==NULL){
fputs(dlerror(),stderr);
exit(1);
}
showGUI();
char s[10];
scanf("%s",&s);
func = dlsym(dp,s);//把输入的字符串s与.so文件中的文件名匹配,看是否能找到此函数
error = dlerror();
if(error){
fputs(error,stderr);
exit(1);
}
(*func)();//找到了此函数如输入的是 save 则在此调用save函数
continue;
}
dlclose(dp);
exit(0);
}
int showGUI(){
FILE *login;
char c;
login = fopen("login.txt","r");
if(!login){
printf("file err:login/n");
return;
}
while(1){
c = fgetc(login);
if(c == EOF){
break;
}
printf("%c",c);
}
fclose(login);
return 0;
}
编译过程:
1.编译得到my.so文件
gcc save.c deposit.c -fPIC -shared -o my.so
2.编译生成执行文件 dy
gcc dy.c -L. -lmy -o dy
详细说明:
-fPIC:表示编译为位置独立的代码,不用此选项的话编译后的代码是位置相关的,所以动态载入时是通过代码拷贝的方式来满足不同进程的需要,而不能达到真正的代码段共享的目的
-L. :表示要连接的库在当前的目录中
-lmy:编译器查找动态链接库时有隐含的命名规则,即在给出的名字前面加上lib,后面加上.so来确定库的名字
1.(deposit.c文件,取款)
#include "dig.h"
void deposit(){
printf("you deposit 5000$ /n");
}
2.(save.c文件,存款)
#include "dig.h"
void save(){
printf("you saved 1000$ /n");
}
3.(dig.h文件)
#ifndef _DIG_H
#define _DIG_H
#ifdef SHARED
void (*save)();
#else
void save();
#endif
#ifdef SHARED
void (*deposit)();
#else
void deposit();
#endif
#endif
4.(dy.c主函数调用两项功能)
#include <stdio.h>
#include <dlfcn.h>
#define SOFILE "./my.so"
#define SHARED
#include "dig.h"
main(){
void *dp;
char *error;
void (*func)();
while(1){
/*以下为调用.so文件,用到了函数dlopen8/
dp =dlopen(SOFILE,RTLD_LAZY);//打开.so文件,RTLD_LAZY为参数
if(dp==NULL){
fputs(dlerror(),stderr);
exit(1);
}
showGUI();
char s[10];
scanf("%s",&s);
func = dlsym(dp,s);//把输入的字符串s与.so文件中的文件名匹配,看是否能找到此函数
error = dlerror();
if(error){
fputs(error,stderr);
exit(1);
}
(*func)();//找到了此函数如输入的是 save 则在此调用save函数
continue;
}
dlclose(dp);
exit(0);
}
int showGUI(){
FILE *login;
char c;
login = fopen("login.txt","r");
if(!login){
printf("file err:login/n");
return;
}
while(1){
c = fgetc(login);
if(c == EOF){
break;
}
printf("%c",c);
}
fclose(login);
return 0;
}
编译过程:
1.编译得到my.so文件
gcc save.c deposit.c -fPIC -shared -o my.so
2.编译生成执行文件 dy
gcc dy.c -L. -lmy -o dy
详细说明:
-fPIC:表示编译为位置独立的代码,不用此选项的话编译后的代码是位置相关的,所以动态载入时是通过代码拷贝的方式来满足不同进程的需要,而不能达到真正的代码段共享的目的
-L. :表示要连接的库在当前的目录中
-lmy:编译器查找动态链接库时有隐含的命名规则,即在给出的名字前面加上lib,后面加上.so来确定库的名字
相关文章推荐
- linux下动态链接问题(so文件的编写与调用)
- windows下的动态链接问题(.dll文件的编写与调用)
- Linux动态链接(3)so文件映射地址
- Java框架JNA调用C方法(windows链接库dll文件、linux链接库so文件)
- Linux下的动态链接库.so文件的使用
- 转载:JNI 编写动态链接库 HelloNative 详细过程(也即用Java和C在Linux下动态生成并使用so文件过程)
- Linux 动态链接找不到.so文件解决方法
- linux 程序动态调用.so文件中的函数
- Linux下使用QT编写和调用动态链接库(.so文件)
- Linux下编译出来的动态链接库缺少so后缀的问题解决
- 关于制定so动态链接库文件需要注意的问题
- linux 动态调用 .so 库文件中的函数
- linux下将.a文件链接到.so文件的问题
- Linux下编写Lua扩展so文件和调用方法实例
- linux下编写动态链接库文件
- linux下将.a文件链接到.so文件的问题
- 加载动态链接库文件.so 失败出错—— No such file or directory (没有文件或目录)问题处理
- 如何让自己的动态链接库文件能够在linux下被调用
- linux替换运行程序或so动态库文件导致的问题
- Linux动态链接之五:运行时显式加载共享文件.so