请教:dlopen总是打不开动态库文件??

silently 2005-05-31 03:32:34
//----------------------------------------------------------------
//main.c
#include<stdio.h>
#include<stdlib.h>
#include<dlfcn.h>
#include"types.h"
struct resource rcs;

int main()
{
int i;
void *handle;
void (*myfcn)(void *);
const char* errmsg;
char* dir = "./policy.so";

handle=dlopen(dir, RTLD_NOW);
if(handle == NULL) {
fprintf(stderr, "Failed to load '%s'\n", dir);
exit(EXIT_FAILURE);
}
dlerror();
myfcn = dlsym(handle, "init_policy");

if((errmsg = dlerror()) != NULL) {
fprintf(stderr, "Didn't find policy_entry");
exit(EXIT_FAILURE);
}

rcs.aaa = malloc(6);
rcs.bbb = malloc(6);

for(i = 0; i < 5; i++) {
rcs.aaa[i] = '0'+i;
rcs.bbb[i] = rcs.aaa[i];
}

rcs.aaa[i] = '\0';
rcs.bbb[i] = '\0';

myfcn(NULL);

dlclose(handle);
return 1;
}
//-------------------------------------------------------------------------------
//mklib.sh
#! /bin/bash
gcc -fPIC -g -c policy.c -o libpolicy.o
gcc -g -shared -Wl,-soname,libpolicy.so -o libpolicy.so.1.0.0 libpolicy.o -lc
ln -s libpolicy.so.1.0.0 libpolicy.so.l
ln -s libpolicy.so.1.0.0 libpolicy.so
gcc -g -Wall main.c -o main -ldl

//--------------------------------------------------------------------------------
//policy.c
#include"policy.h"
char ccc[10] = "iloveyou\n";
void init_policy()
{
int i = 1;
printf("%s\n", rcs.aaa);
printf("%s\n", rcs.bbb);
release_policy();
for(i = 0; i < 5; i++) {
ccc[i] = 'a';
}
release_policy();
while(1);
}

void release_policy()
{
printf(ccc);
}
//-----------------------------------------------------------------------
我怎么也打不开那个.so文件,不知道为什么……求救~~
...全文
1186 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
silently 2005-06-04
  • 打赏
  • 举报
回复
yanghuajia说的是对的,非常感谢,加上那个编译选项后编译通过,正常运行,感谢其他兄弟们的关注,统统给分:)
yanghuajia 2005-06-01
  • 打赏
  • 举报
回复
想起来了,像这样的sharedlib 需要主程序定义的函数和变量(rcs)的情况需要加参数 -rdybmic进行链接
//mklib.sh
#! /bin/bash
gcc -fPIC -g -c policy.c -o libpolicy.o
gcc -g -shared -Wl,-soname,libpolicy.so -o libpolicy.so.1.0.0 libpolicy.o -lc
ln -s libpolicy.so.1.0.0 libpolicy.so.l
ln -s libpolicy.so.1.0.0 libpolicy.so
-----------------------
gcc -g -Wall main.c -o main -ldl -rdynamic
-----------------~~~~
yanghuajia 2005-06-01
  • 打赏
  • 举报
回复
./main
Failed to load './libpolicy.so'
fail ./libpolicy.so: undefined symbol: rcs
yanghuajia 2005-06-01
  • 打赏
  • 举报
回复
policy.h 怎么没加进来
boxban 2005-06-01
  • 打赏
  • 举报
回复
From Man page of dlopen :
...
dlopen loads a dynamic library from the file named by the
null terminated string filename and returns an opaque
"handle" for the dynamic library. If filename is not an
absolute path (i.e., it does not begin with a "/"), then
the file is searched for in the following locations:

A colon-separated list of directories in the user's
LD_LIBRARY_PATH environment variable.

The list of libraries cached in /etc/ld.so.cache.

/usr/lib, followed by /lib.

------------------------------------------
你把路径改为绝对路径试试看。
另外,出错时最好用perror()打印出系统错误消息,这有助于你发现错误原因。
gdhyj 2005-05-31
  • 打赏
  • 举报
回复
不要./行不行?另外另一个参数改成RTLD_LAZY行不行,虽然我都不懂这个参数做什么用

我是当前目录有个libpolicy.so的时候,dlopen("libpolicy.so",RTLD_LAZY);
silently 2005-05-31
  • 打赏
  • 举报
回复
运行以后就是:failed to load libpolicy.so,这个错误是在主程序中当handle为NULL时出现的提示,也就是没有打开那个文件
silently 2005-05-31
  • 打赏
  • 举报
回复
如果我在编译时链接那个库就可以正常运行
gcc -o main main.c -ldl -L. -lpolicy
但是用这种动态加载的方式应该是不用显式连接我需要的库德阿,不然不是没有意义了阿?
spiderww 2005-05-31
  • 打赏
  • 举报
回复
是编译错还是运行错啊?
silently 2005-05-31
  • 打赏
  • 举报
回复
哎,是我打错了,原文件里是./libpolicy.so,还是不行:(
Dizovin 2005-05-31
  • 打赏
  • 举报
回复
char* dir = "./libpolicy.so";

23,116

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧