vs2015開發so動態庫linux


#include <stdio.h>
#include <dlfcn.h>

typedef int(*fn_max)(int a, int b);

int main()
{
    printf("entery in main\n");

    void* hdl = dlopen("liblinuxdlltest.so.1.0", RTLD_NOW);//RTLD_LAZY
    if (!hdl)
    {
        printf("load dll fail\n");
        return;
    }

    fn_max max = (fn_max)dlsym(hdl, "max");
    char* perr = dlerror();
    if (perr)
    {
        printf("load symbol failed: %s\n", perr);
        return;
    }

    int result = max(200, 123);

    printf("the max is %d\n", result);

    dlclose(hdl);


    printf("exit main\n");

    return 0;
}

 

dllmain.h

#pragma once



int max(int a, int b);

dllmain.c

#include <stdio.h>

//int main()
//{
//    printf("hello from ConsoleApplication7!\n");
//
//    printf("this is my so dll test project");
//    return 0;
//}

/************************************************************************/
/* 構造函數                                                             */
/************************************************************************/
void __attribute__((constructor)) x_init(void)
{
    printf("----so lib is loaded\n");
}

/************************************************************************/
/* 析構函數                                                             */
/************************************************************************/
void __attribute__((destructor))  x_fini(void)
{
    printf("----so lib is unload\n");
}

int max(int a, int b)
{
    printf("enter in max\n  .. \n");
    return a > b ? a : b;
}

 

工程源碼下載: http://download.csdn.net/download/jiftlixu/10236153

 

導出符號控制:

https://www.cnblogs.com/lidabo/p/5703890.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM