调用动态库,先加web
#include<dlfcn.h> //用于动态库管理的系统头文件 shell
(1)dlopen()
第一个参数:指定共享库的名称,将会在下面位置查找指定的共享库.
ide
第二个参数:指定如何打开共享库。
-RTLD_NOW:将共享库中的全部函数加载到内存
-RTLD_LAZY:会推后共享库中的函数的加载操做,直到调用dlsym()时方加载某函数函数
(2)dlsym()
调用dlsym时,利用dlopen()返回的共享库的phandle以及函数名称做为参数,返回要加载函数的入口地址。spa
(3)dlerror()
该函数用于检查调用共享库的相关函数出现的错误。 返回是string字符串code
(4)dlclose()
该函数用于关闭动态库。orm
#include <string.h> #include <jni.h> #include <dlfcn.h> jstring Java_org_crazyit_helloworld_MainActivity_stringFromJNI( JNIEnv* env, jobject thiz ) { return (*env)->NewStringUTF(env, "Hello world "); } //这样写,在动态加载时才能获取到函数地址 //第一次编译hello.so的 plus代码 /* jint plus(jint a,jint b) { return a+b; } jint Java_org_crazyit_helloworld_MainActivity_plus(JNIEnv* env, jobject obj, jint a, jint b) { return plus(a,b); } */ //第二次编译加载代码 jint Java_org_crazyit_helloworld_MainActivity_test(JNIEnv* env, jobject obj, jint a, jint b) { void* filehandle = dlopen("/data/data/org.crazyit.helloworld/lib/libhello.so", RTLD_LAZY ); jint sum = 123; // 打开原so文件 if(filehandle != NULL) { sum = 345; int(* oldmethod)(int,int); oldmethod= (int(*)(int,int))dlsym(filehandle, "plus"); //引入原so中的函数 if(oldmethod != NULL) { sum = 6*oldmethod(a,b); } } return sum; }
获取方法的另外一种写法对象
typedef void(* Tx_set_video_data_Func)(unsigned char *, int, int, int, int, int, int, int); Tx_set_video_data_Func oldmethod= (Tx_set_video_data_Func)dlsym(filehandle, "tx_set_video_data"); //引入原so中的函数 if(oldmethod != NULL) { }
2,编译代码 Android.mk内存
加上动态库的链接 LOCAL_LDLIBS := -L . -ldl 字符串
-l参数就是用来指定程序要连接的库 .表明当前目录下的库
-ldl选项,表示生成的对象模块须要使用共享库
LOCAL_PATH := $(call my-dir) LOCAL_LDLIBS := -L . -ldl include $(CLEAR_VARS) LOCAL_MODULE := hello LOCAL_SRC_FILES := hello-jni.c include $(BUILD_SHARED_LIBRARY)
第一个plus的hello.so与test.so放在lib目录下,运行MainActivity代码
static { System.loadLibrary("test"); } //进行加法操做 public native int test(int a,int b);
若是找不到要导入的库:shell里运行
export LD_LIBRARY_PATH=./lib:$LD_LIBRARY_PATH
NDK编译错误
toolchains incompatible target libtx.a(dev.o)
编译平台不一样致使的 如要把x86的 dev.o编译到 arm平台下,改变 Application.mk下的平台选择 x86
Application.mk 写不一样的平台就会调用不一样平台下的toolchains 交叉编译链
2,静态库 .a包含了全部.o文件 ar 进行 拆包 ar -x libdev.a 把全部的so解出来
静态库/动态库中函数查看
1,nm -g libdev.so 显示出 so所依赖的so库和其中的一些函数方法
2,readelf -a libdev.so 显示so的平台等之类的信息