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

qingcairousi 2009-04-22 05:42:24
请高手具体谈谈共享对象的加载和卸载机制。
...全文
1370 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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后,很少有人再使用其前面解析出来的函数吧...

23,217

社区成员

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

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