dlsym 找不到函数指针?

aby7984 2005-12-22 08:07:56
想导出api函数,编译成so,
dlopen 有返回值,但调用dlsym,返回是空。
请问 在编译so 时有什么要注意的吗???
谢谢!!
...全文
1009 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wallacemqx 2006-01-25
  • 打赏
  • 举报
回复
解决了
fierygnu 2006-01-25
  • 打赏
  • 举报
回复
有libhello.o或libhello.so库吗?
wallacemqx 2006-01-25
  • 打赏
  • 举报
回复
[meng-6@localhost dllopen]$ gcc -I./include -o main main.c -lhello -ldl
/usr/bin/ld: cannot find -lhello
collect2: ld returned 1 exit status

第三步报错,不知何故?
wyjam 2005-12-28
  • 打赏
  • 举报
回复
s 要另外包一层呀。
aby7984 2005-12-26
  • 打赏
  • 举报
回复
呵呵 真够详细的, 谢谢谢谢!!
fierygnu 2005-12-23
  • 打赏
  • 举报
回复
C++函数导不出。mangle后的函数名完全变了,而mangle又没有标准。
aby7984 2005-12-23
  • 打赏
  • 举报
回复
需要导出的是c++ 函数,
编译命令 用的是 g++ -pic -share 还是找不到。
我用 gcc 就可以。

顺便再问一下,如果我想导出类,是否还用同样的方法??
x86 2005-12-23
  • 打赏
  • 举报
回复
调用dlerror看看什么错误.
dlerror(); /* clear error code */
s = (actual_type) dlsym(handle, symbol_being_searched_for);
if ((err = dlerror()) != NULL)
{
/* handle error, the symbol wasn't found */
}
else
{
/* symbol found, its value is in s */
}

另外编译函数库时要加上-shared选项
这里有一个例子:
http://www.ddvip.net/OS/linux/index4/248.htm
SmileWolf 2005-12-23
  • 打赏
  • 举报
回复
如果用C++写,函数声明的前面加上extern "C" 修饰。否则C++编译器把函数名变得乱七八糟,dlsym不认识了^_^。
x86 2005-12-23
  • 打赏
  • 举报
回复
C++可以倒出,用extern "C"就是了. 给你一个完整的例子, 抄来的, 我试过可以.

==================================================

[root@localhost dlopen]# ls
chello.cpp hello.cpp include main.c
[root@localhost dlopen]# cat include/hello.h
#ifndef _HELLO_H_
#define _HELLO_H_

class CHello
{
public:
CHello();
~CHello();
int Print();
};

#endif
[root@localhost dlopen]# cat hello.cpp
#include "hello.h"
#include <iostream>

CHello::CHello()
{

}

CHello::~CHello()
{

}

int CHello::Print()
{
std::cerr << "hello world !" << std::endl;
}
[root@localhost dlopen]# cat chello.cpp
extern "C"
{
#include "hello.h"

int cprint()
{
CHello * phello;
phello = new CHello ;

(void *)phello-> Print();

return 0;
}
}
[root@localhost dlopen]# cat main.c
//#include "chello.h"
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
int cprint();

int
main ()
{
char *error;
void *handle = dlopen ("libhello.so", RTLD_NOW);
if (!handle)
{
fprintf (stderr, "%s\n", dlerror ());
exit (1);
}

int (*dlprint)() ;
dlprint = dlsym (handle, "cprint");
if ((error = dlerror ()) != NULL)
{
fprintf (stderr, "%s\n", error);
exit (1);
}

(*dlprint)();

dlclose (handle);


return 0;
}
[root@localhost dlopen]# g++ -I./include -c hello.cpp
[root@localhost dlopen]# g++ -I./include -c chello.cpp
[root@localhost dlopen]# g++ -fPIC -shared -o libhello.so hello.o chello.o
[root@localhost dlopen]# gcc -I./include -o main main.c -lhello -ldl
[root@localhost dlopen]# ls
chello.cpp chello.o hello.cpp hello.o include libhello.so main main.c
[root@localhost dlopen]# ./main
hello world !
[root@localhost dlopen]#
fierygnu 2005-12-22
  • 打赏
  • 举报
回复
1、编译时加-pic选项
2、如果是C++写的,dlsym只能找到static全局函数。

23,121

社区成员

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

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