dlopen的问题

ychhu 2016-04-26 02:32:20
比如一个可执行文件E,两个库A、B,E可以直接装载A和B,但如果E装载A,然后在A里再装载B,dlopen就会返回空,dlerror给出的原因是invalid mode for dlopen(): Invalid argument,查看了一下,带的是绝对路径,请教高手,这是什么原因
...全文
391 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
LubinLew 2016-04-26
  • 打赏
  • 举报
回复
不知道你的代码是怎么写的 我测试发现是好用的

//lib1.h
#ifndef __LIB1_H__
#define __LIB1_H__

int lib1_hello(void);

#endif
//////////////////////////////////
//lib1.c
#include <stdio.h>

int lib1_hello(void)
{
	printf("hello this function is in lib1\n");
	return 0;
}
//////////////////////////////////
//lib2.h
#ifndef __LIB2_H__
#define __LIB2_H__

int call_lib1_func(void);

#endif
//////////////////////////////////
//lib2.c
#include "lib1.h"
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

int call_lib1_func(void)
{
	void* handle = NULL;
	int (*lib1func)(void);

	handle = dlopen("liblib1.so", RTLD_LAZY);
	 if (!handle) {
    	 printf("%s\n", dlerror());
         exit(EXIT_FAILURE);
     }
	
	lib1func = dlsym(handle, "lib1_hello");

	lib1func();

	return 0;

}
//////////////////////////////////
//main.c
#include "lib2.h"
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	void* handle = NULL;
	int (*lib2func)(void);

	handle = dlopen("liblib2.so", RTLD_LAZY);
	 if (!handle) {
    	 printf("%s\n", dlerror());
         exit(EXIT_FAILURE);
     }
	
	lib2func = dlsym(handle, "call_lib1_func");

	lib2func();

	return 0;

}
//////////////////////////////////
//Makefile
all:lib1 lib2
	gcc -g -Wall main.c -ldl -L. -llib2

lib1:
	gcc -g -Wall -shared -fPIC lib1.c -o liblib1.so

lib2:
	gcc -g -Wall -shared -fPIC lib2.c -o liblib2.so

//set env
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:2个so文件的绝对路径

//执行 a.out
hello this function is in lib1

ychhu 2016-04-26
  • 打赏
  • 举报
回复
哪个大牛帮看看啊

23,217

社区成员

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

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