CMake再探:引入SO文件并调用

上次已经通过CMake编译自己写的C/C++代码了,这次尝试导入第三方代码来进行调用。通过上次写的JniTest生成的so文件来测试,生成的so文件在项目的app/build/intermediates/cmake/debug/obj目录下。


1.导入so文件

将so文件拷贝到项目中,路径自己定吧,只要配置的时候不出错就行,我是这样的拷贝到jniLibs文件夹中的。jniLibs下的子文件夹表示的是cpu类型,你可以少建些,但不可以不建,so文件就放在这些文件夹下,每个cpu类型都放。

:导入的so文件需要在库名前添加“lib”,即lib库名.so,比如我要导入库名为test的so文件,那么你得将so文件名改为libtest.so再拷贝进项目)


2.配置

 (1)在CMakeLists.txt中添加add_library()以及set_target_properties()方法


:通常情况下so文件还会附带.h头文件(很明显,我没写头文件),这时候需要再加上include_directories()语句来定位头文件的位置,不是很懂的童鞋可以去这里看看

CMakeLists.txt:

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/native-lib.cpp )

add_library(test-lib SHARED IMPORTED)
set_target_properties(test-lib
  PROPERTIES IMPORTED_LOCATION
  ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libtest-lib.so)

3.调用

导入so文件相当于你“实现”了方法,还需要“声明”。比如我的so文件中实现了“Java_com_example_win7_jnitest_util_JniUtil_stringFromSelf”方法,

com_example_win7_jnitest_util”表示的是包名,

JniUtil”表示的是方法所在类,“stringFromSelf”表示的是方法名。

因此,我在“com.example.win7.jnitest.util”包下新建了JniUtil类来声名方法

:so文件实现了哪些方法可以在.h头文件中看到)

JniUtil.java:

public class JniUtil {
    static {
        System.loadLibrary("test-lib");
    }
    public static native String stringFromSelf(String str); //这里虽然是红字,但可以跑起来
}
在MainActivity中调用该方法:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(JniUtil.stringFromSelf("jack"));
    }

}

附Demo下载地址

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
假设有一个名为`myapp`的可执行文件,需要引入名为`libfoo.so`的动态库并使用其中的函数,可以按照以下步骤进行: 1. 创建`CMakeLists.txt`文件,并在其中添加以下内容: ``` cmake_minimum_required(VERSION 3.5) project(myapp) add_executable(myapp main.cpp) find_library(foo NAMES foo PATHS /usr/local/lib) target_link_libraries(myapp ${foo}) ``` 其中,`main.cpp`是`myapp`的源文件,`foo`是要引入的动态库的名称,`/usr/local/lib`是`libfoo.so`所在的路径。 2. 创建`main.cpp`文件,并在其中使用`libfoo.so`中的函数: ```c++ #include <iostream> #include <dlfcn.h> typedef int (*foo_func_t)(int, int); int main() { void *handle = dlopen("libfoo.so", RTLD_LAZY); if (!handle) { std::cerr << "Error: " << dlerror() << std::endl; return 1; } foo_func_t foo_func = (foo_func_t)dlsym(handle, "foo"); if (!foo_func) { std::cerr << "Error: " << dlerror() << std::endl; dlclose(handle); return 1; } int result = foo_func(3, 4); std::cout << "Result: " << result << std::endl; dlclose(handle); return 0; } ``` 在`main.cpp`中,首先使用`dlopen`函数打开`libfoo.so`动态库,然后使用`dlsym`函数获取其中的`foo`函数,并将其保存到函数指针变量`foo_func`中。最后,调用`foo_func`函数并输出结果。 注意,这里使用了`dlfcn.h`头文件中的函数,需要在编译时链接`dl`库,可以在`CMakeLists.txt`中添加以下命令: ``` target_link_libraries(myapp dl) ``` 3. 在命令行中执行以下命令进行编译: ``` mkdir build cd build cmake .. make ``` 编译完成后,可以运行`myapp`可执行文件,输出结果应为`Result: 7`。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值