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).
", dlerror());
            return -1;
    }

    g_Test = (PFN_TEST)dlsym(handle, "Test");
    char* szError = dlerror();
    if(szError != NULL)
    {
        printf("ERROR, Message(%s).
", szError);
        dlclose(handle);
        return -1;
    }
    if(g_Test != NULL)
    {
        g_Test ("wjshan", 0808);
    }
    dlclose(handle);
    g_Test = NULL;
    return 0;
原文地址:https://www.cnblogs.com/wjshan0808/p/7064393.html