简体   繁体   English

如何使用C ++ dlopen宏从C ++代码而不是从JAVA加载库

[英]How to use C++ dlopen macro to load a library from C++ code instead from JAVA

I'm trying to use some already written C++ code for server communication in my Android application (Basically creating a client), sadly, the logic of the code depends on this piece of code: 我试图在Android应用程序中使用一些已经编写的C ++代码进行服务器通信(基本创建一个客户端),可悲的是,代码的逻辑取决于这段代码:

#ifndef LoadLib
#define LoadLib(a) (hmod)dlopen(a, RTLD_NOW)
#endif

The code gets called from: 该代码从以下位置调用:

m_hmod = LoadLib(m_namespace::LIBRARY);

where LIBRARY = "nameofmylib.so" 其中LIBRARY = "nameofmylib.so"

Problem is that when I call LoadLib, my m_var remains NULL, which means it can't find the library ( That's my guess so far ) and so my logic goes into error handling rather than connecting to the socket. 问题是,当我调用LoadLib时,我的m_var保持为NULL,这意味着它找不到该库(到目前为止,这是我的猜测),因此我的逻辑进入了错误处理而不是连接到套接字。

So far, I've been surfing StackOverflow and trying solutions people suggest with changing the Gradle file and adjusting CMakeList.txt but none work. 到目前为止,我一直在浏览StackOverflow,并尝试人们建议的解决方案,包括更改Gradle文件和调整CMakeList.txt,但没有任何效果。 I've been debugging it for few hours as well, and I even tried looking for the .so location inside the Android Studio Device File Explorer, so that I could put in the absolute path. 我也已经调试了几个小时,甚至尝试在Android Studio设备文件资源管理器中查找.so位置,以便可以输入绝对路径。 But that also yielded no results. 但这也没有结果。

The complete flow of loading goes like this: 完整的加载流程如下:

#ifndef LoadLib
#define LoadLib(a) (hmod)dlopen(a, RTLD_NOW)
#endif


typedef void * hmod;
hmod m_hmod;

m_hmod = LoadLib(m_namespace::LIBRARY);

if (m_hmod) {
    ... Some code that needs to execute ...
} else {
    ... What actually happens ...
}

The code I am working with isn't something I wrote, but I have to stick to it, thus I need to use the logic that's inside it rather than rewriting it and making my own. 我正在使用的代码不是我编写的,但是我必须坚持下去,因此我需要使用其内部的逻辑,而不是重写并自行编写。

Most likely, m_namespace::LIBRARY is not being packed into the APK. 最有可能的是, m_namespace::LIBRARY没有打包到APK中。 To check this, you don't need to examine the device files. 为此,您无需检查设备文件。 Android Studio provides a menu command to analyze your APK. Android Studio提供了一个菜单命令来分析您的APK。

If the library is missing, you should ask yourself if the name follows Android rules: it must begin with lib and it must end with .so . 如果缺少该库,则应问自己名称是否遵循Android规则:它必须以lib开头,并且必须以.so结尾。 No number suffixes, no extra characters, and God forbid, no spaces. 没有数字后缀,没有多余的字符,上帝禁止,没有空格。

If the library name is OK, but it is used prebuilt by your CMake script, check that it is available for the ABI of your device. 如果库名称正确,但已由CMake脚本预先构建,则请检查该库是否可用于设备的ABI。 Also, make sure that its path is listed in jniLibs.src list in build.gradle . 同时,确保其路径在jniLibs.src列表列出的build.gradle。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM