在linux/android开发时,我们经常会遇到第三方的so库,如何加载这些so库呢,下面以helloworld的简单例程进行详细讲解,为了方便移植和管理,所有涉及加载实现相关的代码都放在so.cpp和so.h模块,实现代码如下:
so.cpp
//so.cpp
//
// Created by taxiang&xuezi on 2019-02-27.
//
#define SO_GLOBALS
#include "so.h"
typedef int (* helloworld_func)(char input[], int inlen, char output[], int outlen);
helloworld_func helloworld_c= NULL;
/*******************************************************************************
* 函数名称:int load_helloworld_so(const char *so_path)
* 函数功能:加载so库
* 输入参数:
* 输出参数:
* 返回值 :
*******************************************************************************/
int load_helloworld_so(const char *so_path)
{
g_so_en = 0;
g_handle = dlopen(so_path, RTLD_LAZY);
if (g_handle == NULL) {
printf("dlopen %s failed\n",so_path);
return -1;
}
helloworld_c = (helloworld_func)dlsym(g_handle, "h