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

 

转载于:https://www.cnblogs.com/jiftle/p/8401887.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值