linux动态库注册函数,linux下加载动态库函数
比较懒,而且时间也不早了,就贴上英文,以后有机会在翻译吧。
DLOPEN(3) Linux Programmer's Manual DLOPEN(3)
NAME
dladdr, dlclose, dlerror, dlopen, dlsym, dlvsym - program-
ming interface to dynamic linking loader
SYNOPSIS
#include
void *dlopen(const char *filename, int flag);
char *dlerror(void);
void *dlsym(void *handle, const char *symbol);
int dlclose(void *handle);
DESCRIPTION
The four functions dlopen(), dlsym(), dlclose(), dlerror()
implement the interface to the dynamic linking loader.
dlerror
The function dlerror() returns a human readable string
describing the most recent error that occurred from any of
the dl routines (dlopen, dlsym or dlclose) since the last
call to dlerror(). It returns NULL if no errors have
occurred since initialization or since it was last called.
dlopen
The function dlopen() loads the dynamic library file named
by the null-terminated string filename and returns an
opaque "handle" for the dynamic library. If filename is
NULL, then the returned handle is for the main program.
If filename contains a slash ("/"), then it is interpreted
as a (relative or absolute) pathname. Otherwise, the
dynamic linker searches for the library as follows (see
ld.so(8) for further details):
o (ELF only) If the executable file for the calling
program contains a DT_RPATH tag, and does not con-
tain a DT_RUNPATH tag, then the directories listed
in the DT_RPATH tag are searched.
o If the environment variable LD_LIBRARY_PATH is
defined to contain a colon-separated list of direc-
tories, then these are searched. (As a security
measure this variable is ignored for set-UID and
set-GID programs.)
o (ELF only) If the executable file for the calling
program contains a DT_RUNPATH tag, then the direc-
tories listed in that tag are searched.
o The cache file /etc/ld.so.cache (maintained by
ldconfig(8)) is checked to see whether it contains
an entry for filename.
o The directories /lib and /usr/lib are searched (in
that order).
If the library has dependencies on other shared libraries,
then these are also automatically loaded by the dynamic
linker using the same rules. (This process may occur
recursively, if those libraries in turn have dependencies,
and so on.)
The value of flag can be either RTLD_LAZY or RTLD_NOW.
When RTLD_NOW is specified, or the environment variable
LD_BIND_NOW is set to a non-empty string, all undefined
symbols in the library are resolved before dlopen()
returns. If this cannot be done, an error is returned.
Otherwise binding is lazy: symbol values are first
resolved when needed.
Optionally, RTLD_GLOBAL may be or'ed into flag, in which
case the external symbols defined in the library will be
made available for symbol resolution of subsequently
loaded libraries. (The converse of RTLD_GLOBAL is
RTLD_LOCAL. This is the default.)
If filename is a NULL pointer, then the returned handle is
for the main program. When given to dlsym(), this handle
causes a search for a symbol in the main program, followed
by all shared libraries loaded at program startup, and
then all shared libraries loaded by dlopen() with the flag
RTLD_GLOBAL.
External references in the library are resolved using the
libraries in that library's dependency list and any other
libraries previously opened with the RTLD_GLOBAL flag. If
the executable was linked with the flag "-rdynamic" (or,
synonymously, "--export-dynamic"), then the global symbols
in the executable will also be used to resolve references
in a dynamically loaded library.
If the same library is loaded again with dlopen(), the
same file handle is returned. The dl library maintains
reference counts for library handles, so a dynamic library
is not deallocated until dlclose() has been called on it
as many times as dlopen() has succeeded on it. The _init
routine, if present, is only called once. But a subsequent
call with RTLD_NOW may force symbol resolution for a
library earlier loaded with RTLD_LAZY.
If dlopen() fails for any reason, it returns NULL.
dlsym
The function dlsym() takes a "handle" of a dynamic library
returned by dlopen and the NUL-terminated symbol name,
returning the address where that symbol is loaded into
memory. If the symbol is not found, in the specified
library or any of the libraries that were automatically
loaded by dlopen() when that library was loaded, dlsym()
returns NULL. (The search performed by dlsym() is breadth
first through the dependency tree of these libraries.)
Since the value of the symbol could actually be NULL (so
that a NULL return from dlsym() need not indicate an
error), the correct way to test for an error is to call
dlerror() to clear any old error conditions, then call
dlsym(), and then call dlerror() again, saving its return
value into a variable, and check whether this saved value
is not NULL.
There are two special pseudo-handles, RTLD_DEFAULT and
RTLD_NEXT. The former will find the first occurrence of
the desired symbol using the default library search order.
The latter will find the next occurrence of a function in
the search order after the current library. This allows
one to provide a wrapper around a function in another
shared library.
dlclose
The function dlclose() decrements the reference count on
the dynamic library handle handle. If the reference count
drops to zero and no other loaded libraries use symbols in
it, then the dynamic library is unloaded.
The function dlclose() returns 0 on success, and non-zero
on error.
The obsolete symbols _init and _fini
The linker recognizes special symbols _init and _fini. If
a dynamic library exports a routine named _init, then that
code is executed after the loading, before dlopen()
returns. If the dynamic library exports a routine named
_fini, then that routine is called just before the library
is unloaded. In case you need to avoid linking against
the system startup files, this can be done by giving gcc
the "-nostartfiles" parameter on the command line.
Using these routines, or the gcc -nostartupfiles or -nost-
dlib options, is not recommended. Their use may result in
undesired behavior, since the constructor/destructor rou-
tines will not be executed (unless special measures are
taken).
Instead, libraries should export routines using the
__attribute__((constructor)) and __attribute__((destruc-
tor)) function attributes. See the gcc info pages for
information on these. Constructor routines are executed
before dlopen returns, and destructor routines are exe-
cuted before dlclose returns.
GNU EXTENSIONS
Glibc adds two functions not described by POSIX, with pro-
totypes
#define GNU_SOURCE
#include
int dladdr(void *addr, Dl_info *info);
void *dlvsym(void *handle, char *symbol, char *version);
The function dladdr() takes a function pointer and tries
to resolve name and file where it is located. Information
is stored in the Dl_info structure:
typedef struct {
const char *dli_fname;/* File name of defining object */
void *dli_fbase; /* Load address of that object */
const char *dli_sname;/* Name of nearest lower symbol */
void *dli_saddr; /* Exact value of nearest symbol */
} Dl_info;
dladdr() returns 0 on error, and non-zero on success.
The function dlvsym() does the same as dlsym() but takes a
version string as additional argument.
EXAMPLE
Load the math library, and print the cosine of 2.0:
#include
#include
int main(int argc, char **argv) {
void *handle;
double (*cosine)(double);
char *error;
handle = dlopen ("libm.so", RTLD_LAZY);
if (!handle) {
fprintf (stderr, "%s\n", dlerror());
exit(1);
}
dlerror(); /* Clear any existing error */
*(void **) (&cosine) = dlsym(handle, "cos");
if ((error = dlerror()) != NULL) {
fprintf (stderr, "%s\n", error);
exit(1);
}
printf ("%f\n", (*cosine)(2.0));
dlclose(handle);
return 0;
}
If this program were in a file named "foo.c", you would
build the program with the following command:
gcc -rdynamic -o foo foo.c -ldl
Libraries exporting _init() and _fini() will want to be
compiled as follows, using bar.c as the example name:
gcc -shared -nostartfiles -o bar bar.c
NOTES
The symbols RTLD_DEFAULT and RTLD_NEXT are defined by
only when _GNU_SOURCE was defined before includ-
ing it.
HISTORY
The dlopen interface standard comes from SunOS. That sys-
tem also has dladdr, but not dlvsym.
CONFORMING TO
POSIX 1003.1-2003 describes dlclose, dlerror, dlopen,
dlsym.
SEE ALSO
ld(1), ldd(1), ld.so(8), ldconfig(8), ld.so info pages,
gcc info pages, ld info pages
Linux 2003-11-17 DLOPEN(3)
linux动态库注册函数,linux下加载动态库函数相关推荐
- linux动态库注册函数,Linux动态库函数的详解
linux动态库函数的详解 加载动态库 void *dlopen(const char *filename, int flag); flag的可能值: rtld_lazy rtld_now rtld_ ...
- linux直接运行程序加载动态库失败,扣丁学堂Linux培训详解程序运行时加载动态库失败解决方法...
今天扣丁学堂Linux培训老师给大家介绍一下关于Linux程序运行时加载动态库失败的解决方法,希望对同学们学习有所帮助,下面我们一起来看一下吧. Linux下不能加载动态库问题 当出现下边异常情况 . ...
- dlopen linux 实例_Linux下c函数dlopen实现加载动态库so文件代码举例
dlopen()是一个强大的库函数.该函数将打开一个新库,并把它装入内存.该函数主要用来加载库中的符号,这些符号在编译的时候是不知道的.这种机制使得在系统中添加或者删除一个模块时,都不需要重新编译了. ...
- Linux添加相对库路径,Linux C编程(8) 使用相对路径加载动态库-rpath和$ORIGIN
商业程序如何加载自己的so 使用LD_LIBRARY_PATH的缺点是要实现设置LD_LIBRARY_PATH.不够自动化.那么大型的商业程序是如何加载自己的so呢. 这里以QtCreator为例. ...
- linux驱动的入口函数module_init的加载和释放
就像你写C程序需要包含C库的头文件那样,Linux内核编程也需要包含Kernel头文件,大多的Linux驱动程序需要包含下面三个头文件: #include <linux/init.h> # ...
- linux驱动的入口函数module_init的加载和释放(转)
像你写C程序需要包含C库的头文件那样,Linux内核编程也需要包含Kernel头文件,大多的Linux驱动程序需要包含下面三个头文件: #include <linux/init.h> #i ...
- macOS下加载动态库dylib报code signature invalid错误的解决办法
一.现象描述 在macOS上搞开发也有一段时间了,也积攒了一定的经验.然而,今天在替换工程中的一个动态库时还是碰到了一个问题.原来工程中用的是一个静态库,调试时发现有问题就把它替换成了动态库.这本来没 ...
- linux下项目开发加载动态库:ldconfig与 /etc/ld.so.conf
场景:自己开发一个项目,程序里包含一些自定义动态库.运行,需要加载这些动态库. 假如这些库在/pro/output/lib/下面,可执行程序在/pro/output/bin/下面. 那么,我们需要: ...
- Linux系统程序运行时加载动态库路径顺序
程序运行时加载动态库路径顺序(Linux) 在linux系统中,如果程序需要加载动态库,它会按照一定的顺序(优先级)去查找: 链接时路径(Link-time path)和运行时路径(Run-time ...
最新文章
- jstl c:choose、c:when和c:otherwise标签
- Xcode11 上传苹果卡在Authenticating with the iTunes store 或者transpoter上传卡在正在验证 APP - 正在通过App Store进行认证
- #.NET分别以GET和POST方式抓取远程页面
- 通过IEnumerable和IDisposable实现可暂停和取消的任务队列
- 广告深度学习计算:异构硬件加速实践
- Asp.NET中如何一次性下载多个文件
- python安装mysqlclient_Python-安装mysqlclient(MySQLdb)
- vim永久取消空格颜色
- ajax请求参数为中文乱码的情况
- ssm校园帮代服务系统的设计与实现答辩PPT模板
- 现代软件工程团队项目贝塔阶段_大规模测试结果_2018.02.08
- ORB-SLAM3: An Accurate Open-Source Library for Visual, Visual-Inertial and Multi-Map SLAM
- JAVAME 还有钱途么?
- ajhua门禁_大华门禁主机密码 ajhua门禁
- linux iq测试题,ayawawa测试题
- php 正则 英文开头,php 正则表达式 匹配以“XXX”开头不能以“YYY”结尾
- java等待所有子线程执行完毕再执行
- 微信公众号的用户运营?
- linux pam 版本号,Linux中pam板块详解
- AD生成Gerber及CAM350、DFM检查