Linux中LoadLibrary()函数(zhuan)
Linux中LoadLibrary()函数(zhuan) 2009-09-08 14:01:28| 分类:默认分类 |字号 订阅在MS的WINDOWS系统中有一个LoadLibrary()函数可以在程序运行时加载DLL,在Linux中有类似的功能么?高手请教!-----------------------------------------------
·
在MS的WINDOWS系统中有一个LoadLibrary()函数可以在程序运行时加载DLL,在Linux中有类似的功能么?高手请教!
---------------------------------------------------------------
dlopen
参见:
http://expert.csdn.net/Expert/topic/1385/1385737.xml?temp=.8568994
sharelib.c
====================
#include <stdio.h>
void print()
{
extern int a;
fprintf(stdout,"Hello, World, I'm ari, %d\n", a);
}
main.c
===================
#include <dlfcn.h>
#include <stdio.h>
int a;
int main()
{
void *dp;
char *error;
void (*errmsg)(void);
a = 2;
dp = dlopen("./mylib.so", RTLD_LAZY );
if( dp ==NULL ){
printf("%s\n", dlerror());
exit(1);
}
errmsg = dlsym(dp, "print");
errmsg();
error = dlerror();
if( error )
printf("%s\n",error);
dlclose(dp);
return 0;
}
Makefile
====================
mylib.so:
gcc -shared -o mylib.so sharelib.c
main:
# make global for all
gcc -rdynamic -o main main.c -ldl
# make global notfor all
#gcc -o main main.c -ldl
---------------------------------------------------------------
dlopen
参见:
http://expert.csdn.net/Expert/topic/1385/1385737.xml?temp=.8568994
sharelib.c
====================
#include <stdio.h>
void print()
{
extern int a;
fprintf(stdout,"Hello, World, I'm ari, %d\n", a);
}
main.c
===================
#include <dlfcn.h>
#include <stdio.h>
int a;
int main()
{
void *dp;
char *error;
void (*errmsg)(void);
a = 2;
dp = dlopen("./mylib.so", RTLD_LAZY );
if( dp ==NULL ){
printf("%s\n", dlerror());
exit(1);
}
errmsg = dlsym(dp, "print");
errmsg();
error = dlerror();
if( error )
printf("%s\n",error);
dlclose(dp);
return 0;
}
Makefile
====================
mylib.so:
gcc -shared -o mylib.so sharelib.c
main:
# make global for all
gcc -rdynamic -o main main.c -ldl
# make global notfor all
#gcc -o main main.c -ldl
更多推荐
已为社区贡献5条内容
所有评论(0)