• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
wjshan0808

Learn from yesterday, Live for today, For a better tomorrow.
 ————wjshan0808

博客园    首页    新随笔    联系   管理    订阅  订阅

Linux下C++动态加载so 调用方法

Windows 下的 C++动态加载DLL调用方法

 

文献参考 http://man7.org/linux/man-pages/man0/dlfcn.h.0p.html

    http://man7.org/linux/man-pages/man3/dlopen.3.html

    http://tldp.org/HOWTO/Program-Library-HOWTO/dl-libraries.html

    http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html

 

加入头文件

#include <dlfcn.h>

定义函数接口 如:

typedef long (*PFN_TEST)(const char* szName, int nAge);
PFN_TEST g_Test = NULL;

然后是调用步骤:

    void* handle = dlopen("/path/to/so", RTLD_LAZY);
    if(!handle)
    {        
            printf("ERROR, Message(%s).\n", dlerror());
            return -1;
    }

    g_Test = (PFN_TEST)dlsym(handle, "Test");
    char* szError = dlerror();
    if(szError != NULL)
    {
        printf("ERROR, Message(%s).\n", szError);
        dlclose(handle);
        return -1;
    }
    if(g_Test != NULL)
    {
        g_Test ("wjshan", 0808);
    }
    dlclose(handle);
    g_Test = NULL;
    return 0;

 


posted @ 2017-06-22 13:04  wjshan0808  阅读(13719)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3