当前位置: 技术问答>linux和unix
关于dlopen的问题
来源: 互联网 发布时间:2017-05-10
本文导语: 我弄了一个简单的测试程序,想试试linux下调用so文件,程序很简单: 在eclipse下新建2个工程,一个工程输出so包,编译的时候也加了-ldl,要输出的函数(就是一个print函数,打印一些信息)外面也包了extern "C". 编译出来...
我弄了一个简单的测试程序,想试试linux下调用so文件,程序很简单:
在eclipse下新建2个工程,一个工程输出so包,编译的时候也加了-ldl,要输出的函数(就是一个print函数,打印一些信息)外面也包了extern "C".
编译出来的包通过nm和ldd查看均完好。
另外一个工程调用该包里面的print函数,我使用了dlopen去打开那个so文件,发现返回的handle始终是0.不管采用绝对路径也好,还是项目属性里面指定library path,都不行。
我网上搜了一下,发现把so包放到/usr/lib下居然就可以了,不知道为什么。
请教整个事情的真相,谢谢!
在eclipse下新建2个工程,一个工程输出so包,编译的时候也加了-ldl,要输出的函数(就是一个print函数,打印一些信息)外面也包了extern "C".
编译出来的包通过nm和ldd查看均完好。
另外一个工程调用该包里面的print函数,我使用了dlopen去打开那个so文件,发现返回的handle始终是0.不管采用绝对路径也好,还是项目属性里面指定library path,都不行。
我网上搜了一下,发现把so包放到/usr/lib下居然就可以了,不知道为什么。
请教整个事情的真相,谢谢!
|
我做了个测试,绝对路径没问题,楼主要不把你的程序贴出来看看。
|
man dlopen
看看你的Linux系统上dlopen的具体实现。
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 con‐
tains 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 contain a DT_RUNPATH
tag, then the directories listed in the DT_RPATH tag are searched.
o If, at the time that the program was started, the environment variable LD_LIBRARY_PATH was defined to contain a colon-
separated list of directories, then these are searched. (As a security measure this variable is ignored for set-user-
ID and set-group-ID programs.)
o (ELF only) If the executable file for the calling program contains a DT_RUNPATH tag, then the directories 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 file‐
name.
o The directories /lib and /usr/lib are searched (in that order).
看看你的Linux系统上dlopen的具体实现。
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 con‐
tains 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 contain a DT_RUNPATH
tag, then the directories listed in the DT_RPATH tag are searched.
o If, at the time that the program was started, the environment variable LD_LIBRARY_PATH was defined to contain a colon-
separated list of directories, then these are searched. (As a security measure this variable is ignored for set-user-
ID and set-group-ID programs.)
o (ELF only) If the executable file for the calling program contains a DT_RUNPATH tag, then the directories 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 file‐
name.
o The directories /lib and /usr/lib are searched (in that order).
|
在终端下设置LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/dodomouse/workspace/MyLinuxTest/
再运行一下试试。
再运行一下试试。
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。