dlopen失败:无法打开共享对象文件:没有这样的文件或目录 [英] dlopen failed: cannot open shared object file: No such file or directory
问题描述
问题是我使用dlopen
加载了一个库(.so是我写的,它不是系统库),但是标题中显示了错误.
The problem is I use dlopen
to load a library (the .so is written by me, it's not a system library), but I got the error shown in the title.
- 我已包含
dlfcn.h
- 在编译器中,我使用了
-ldl
命令 - 我要加载的只是源代码文件夹,我尝试添加
-L.
,但是没有用.
- I have included
dlfcn.h
- in compiler, I used the
-ldl
command - What I want to load is just the source code folder, I tried to add
-L.
, but it did not work.
推荐答案
如果要dlopen的库不在标准搜索路径中,则有很多选择:
If the library you want to dlopen is not in the standard search path you have a number of options:
-
在dlopen中指定文件的完整路径
Specify the full path to the file in dlopen
dlopen("/full/path/to/libfile.so");
通过LD_LIBRARY_PATH将路径添加到库
Add the path to the library via LD_LIBRARY_PATH
LD_LIBRARY_PATH=/path/to/library/ ./executable
使用ld -rpath选项向应用程序添加库路径.
use the ld -rpath option to add a library path to the application.
g++ -link stuff- -Wl,-rpath=/path/to/library/
请注意,选项1& 3将库路径硬编码到您的应用程序中. -rpath确实具有指定相对路径的选项,即
Note that options 1 & 3 hardcode the library path into your application. -rpath does have an option to specify a relative path, i.e.
-Wl,-rpath=$ORIGIN/../lib/
将相对路径嵌入到应用程序中.
Will embed a relative path into the application.
这篇关于dlopen失败:无法打开共享对象文件:没有这样的文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!