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