您当前的位置: 首页 >  linux

LINUX加载静态库so,取得函数地址并调用

柳鲲鹏 发布时间:2019-10-29 19:56:00 ,浏览量:0

  • 源码
#include 
#include 
#include 
#include 
#include 

#define SO_FILE   "test.so"
#define FUNC_NAME "call"

typedef int (*FUN_CALL)(int, int);

int main(const int argc, const char** argv)
{
    void     *pLib  = NULL;
    FUN_CALL  pCall = NULL;
    
    pLib = dlopen(SO_FILE, RTLD_NOW | RTLD_GLOBAL);
    if ( NULL == pLib )
    {
        printf("%s\n", dlerror());
        return -1;
    }
    
    pCall = (FUN_CALL)dlsym(pLib , FUNC_NAME);
    if ( NULL == pCall )
    {
        //ERROR
        return -1;
    }

    pCall(0, 1);
    
    //养成良好习惯
    dlclose(pLib);
}
  • 编译
gcc test.c -o test -ldl

关注
打赏
1688896170
查看更多评论

柳鲲鹏

暂无认证

  • 0浏览

    0关注

    4641博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0740s