用dlopen和dlsym得到的函数指针,在dlclose后还能继续使用么?

qingcairousi 2009-04-22 05:42:24
请高手具体谈谈共享对象的加载和卸载机制。
...全文
1136 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
aeou 2009-04-22
  • 打赏
  • 举报
回复
不能dlclose的,除非你是确定不再使用了的才需要dlclose,否则不要执行这个操作

共享对象通过dlopen动态加载动态库的
加载完成后,通过dlsym定位到你需要执行的函数指针
然后可以在程序中使用
当不需要使用的时候,再执行dlclose卸载掉动态链接库

自己去一个个函数man一下就明白了,顺手贴给你

  dlopen -- open a dynamically linked library

Syntax
======

cc . . . -lc

#include <dlfcn.h>

void *dlopen(const char *pathname, int mode);


  dlsym -- get the address of a symbol in a dynamically linked library

Syntax
======

cc . . . -lc

#include <dlfcn.h>

void *dlsym(void *handle, const char *name);


Example:

     void *handle;
int i, *iptr;
int (*fptr)(int);
/* open the needed object */
handle = dlopen("/usr/mydir/libx.so", RTLD_LAZY);

/* find address of function and data objects */
fptr = (int (*)(int))dlsym(handle, "some_function");

iptr = (int *)dlsym(handle, "int_object");

/* invoke function, passing value of integer as a parameter */

i = (*fptr)(*iptr);


  dlclose -- close a dynamically linked library

Syntax
======

cc . . . -lc

#include <dlfcn.h>

int dlclose(void *handle);
Description
===========

dlclose(S) disassociates a shared object (a dynamically linked library in
our case) previously opened by dlopen(S) from the current process. Once an
object has been closed using dlclose( ), its symbols are no longer
available to dlsym(S). All objects loaded automatically as a result of
invoking dlopen( ) on the referenced object are also closed. handle is the
invoking dlopen( ) on the referenced object are also closed. handle is the
value returned by a previous invocation of dlopen( ).

Return values
=============

If the referenced object was successfully closed, dlclose( ) returns 0. If
the object could not be closed, or if handle does not refer to an open
object, dlclose( ) returns a non-zero value. More detailed diagnostic
information is available through dlerror(S).

Notes
=====

A successful invocation of dlclose( ) does not guarantee that the objects
associated with handle have actually been removed from the address space of
the process. Objects loaded by one invocation of dlopen( ) may also be
loaded by another invocation of dlopen( ). The same object may also be
opened multiple times. An object is not removed from the address space
until all references to that object through an explicit dlopen( )
invocation have been closed and all other objects implicitly referencing
that object have also been closed.

associated with handle have actually been removed from the address space of
the process. Objects loaded by one invocation of dlopen( ) may also be
loaded by another invocation of dlopen( ). The same object may also be
opened multiple times. An object is not removed from the address space
until all references to that object through an explicit dlopen( )
invocation have been closed and all other objects implicitly referencing
that object have also been closed.

Once an object has been closed by dlclose( ), referencing symbols contained
in that object can cause undefined behavior.

See also
========

dlerror(S), dlopen(S), dlsym(S)

Standards conformance
=====================

dlclose(S) is not part of any currently supported standard; it is an
extension of AT&T System V provided by The Santa Cruz Operation, Inc.
morris88 2009-04-22
  • 打赏
  • 举报
回复
一般情况下,貌似 dlcose后,很少有人再使用其前面解析出来的函数吧...
文中是linux下 C++动态库 实现接口提供类导出的一个例子 注意其中使用函数返回基类指针的用法,因为Linux的动态链接库不能像MFC中那样直接导出类 一、介绍 如何使用dlopen API动态地加载C++函数和类,是Unix C++程序员经常碰到的问题。 事实上,情况偶尔有些复杂,需要一些解释。这正是写这篇mini HOWTO的缘由。 理解这篇文档的前提是对C/C++语言中dlopen API有基本的了解。 这篇HOWTO的维护链接是: http://www.isotton.com/howtos/C++-dlopen-mini-HOWTO/ 二、问题所在 有时你想在运行时加载一个库(并使用其中的函数),这在你为你的程序写一些插件或模块架构的时候经常发生。 在C语言中,加载一个库轻而易举(调用dlopendlsymdlclose就够了),但对C++来说,情况稍微复杂。 动态加载一个C++库的困难一部分是因为C++的name mangling (译者注:也有人把它翻译为“名字毁坏”,我觉得还是不翻译好), 另一部分是因为dlopen API是用C语言实现的,因而没有提供一个合适的方式来装载类。 在解释如何装载C++库之前,最好再详细了解一下name mangling。 我推荐您了解一下它,即使您对它不感兴趣。因为这有助于您理解问题是如何产生的,如何才能解决它们。 1. Name Mangling 在每个C++程序(或库、目标文件)中, 所有非静态(non-static)函数在二进制文件中都是以“符号(symbol)”形式出现的。 这些符号都是唯一的字符串,从而把各个函数在程序、库、目标文件中区分开来。 在C中,符号名正是函数名:strcpy函数的符号名就是“strcpy”,等等。 这可能是因为两个非静态函数的名字一定各不相同的缘故。 而C++允许重载(不同的函数有相同的名字但不同的参数), 并且有很多C所没有的特性──比如类、成员函数、异常说明──几乎不可能直接用函数名作符号名。 为了解决这个问题,C++采用了所谓的name mangling。它把函数名和一些信息(如参数数量和大小)杂糅在一起, 改造成奇形怪状,只有编译器才懂的符号名。 例如,被mangle后的foo可能看起来像foo@4%6^,或者,符号名里头甚至不包括“foo”。 其中一个问题是,C++标准(目前是[ISO14882])并没有定义名字必须如何被mangle, 所以每个编译器都按自己的方式来进行name mangling。 有些编译器甚至在不同版本间更换mangling算法(尤其是g++ 2.x和3.x)。 即使您搞清楚了您的编译器到底怎么进行mangling的,从而可以用dlsym调用函数了, 但可能仅仅限于您手头的这个编译器而已,而无法在下一版编译器下工作。 三、类 使用dlopen API的另一个问题是,它只支持加载函数。 但在C++中,您可能要用到库中的一个类,而这需要创建该类的一个实例,这不容易做到。 四、解决方案 1. extern "C" C++有个特定的关键字用来声明采用C binding的函数: extern "C" 。 用 extern "C"声明的函数将使用函数名作符号名,就像C函数一样。 因此,只有非成员函数才能被声明为extern "C",并且不能被重载。 尽管限制多多,extern "C"函数还是非常有用,因为它们可以象C函数一样被dlopen动态加载。 冠以extern "C"限定符后,并不意味着函数中无法使用C++代码了, 相反,它仍然是一个完全的C++函数,可以使用任何C++特性和各种类型的参数。

23,125

社区成员

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

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