加载.so库C ++ [英] loading .so library C++
本文介绍了加载.so库C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图加载库library.so,当我尝试如果存在返回true,但当我使用dlopen返回库不存在。
I'm trying load library library.so, when i try if exist return true, but when I use dlopen return library doesn't exist.
std::ifstream ifile("library.so"); if (ifile) {
cout << "Exist!" << std::endl; }
cout << "C++ dlopen demo\n\n";
// open the library cout << "Opening hello.so...\n"; void* handle = dlopen("library.so", RTLD_LAZY);
if (!handle) {
cerr << "Cannot open library: " << dlerror() << '\n';
return 1; }
推荐答案
dlopen
在它可以搜索的路径上非常有限(保持简短:默认路径加上 LD_LIBRARY_PATH
变量 - 参见完整的文档以获取完整列表)。您的 ifstream
查看当前目录(无论是什么),这很可能不包括在 dlopen $ c $
dlopen
is quite restricted in the paths it can search (to keep it short: the default paths plus LD_LIBRARY_PATH
variable -- see the full documentation for a complete list). Your ifstream
looks in the current directory (whatever it is), which is quite probably not included by default in the paths that dlopen
considers.
解决方案包括:
- 相应地设置LD_LIBRARY_PATH通常是首选方法)。
- 使用绝对路径而不是相对路径。
- 将您的库放入其中一个默认路径
/ lib
或/ usr / lib
)。
- Setting LD_LIBRARY_PATH accordingly (which is usually the preferred method).
- Using an absolute path instead of a relative one.
- Putting your library in one of the default paths (eg.
/lib
or/usr/lib
).
这篇关于加载.so库C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文