多个c/c++动态库函数同名冲突解决方法

最近某个程序需要调用多个动态库,但是不同的动态库中使用了功能不同的同名函数,导致调用出现偏差,为此做了些面向搜索引擎的编程工作(俗名百度google一下)。。。

样例

假设有动态库libFuncA.so和libFuncB.so,他们的内部实现分别为:

/*func_A.c*/
#include<stdio.h>
//内部函数
int sayHi()
{
    printf("Hi,this is AAAAA\n");    
    return 0;
}
//外部调用函数
int sayOut()
{
    sayHi();
    printf("Use this to introduce AAAAA\n");
    return 0;    
}
/*func_B.c*/
#include<stdio.h>
//内部函数
int sayHi()
{
    printf("Hi,this is BBBBB\n");    
    return 0;
}
//外部调用函数
int sayOut()
{
    sayHi();
    printf("Use this to introduce BBBBB\n");
    return 0;    
}

分别编译成动态库:
gcc -fPIC -shared -o libFuncA.so func_A.c
gcc -fPIC -shared -o libFuncB.so func_B.c

调用函数test.c:

#include<stdio.h>
extern int sayOut();
int main()
{
    sayOut();
    return 0;
}

则调用的时候,使用函数sayOut,实现的功能与编译时链接库顺序有关。
运行结果

阶段一

可使用dlopen函数族,显式指定要调用的动态库。
详细用法如:dlopen
该函数族需设定打开模式,返回一个动态库的句柄,调用句柄和函数进行操作,完成后需要关闭。
使用时,需引入头文件 dlfcn.h,定义函数指针, 编译时增加 -rdynamic 参数和链接 -ldl

更改调用函数test.c

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

extern int sayOut();

typedef int (*func_pt)();
int main()
{
    void *handle = NULL;
    func_pt func = NULL;
    
    if((handle = dlopen("./libFuncA.so", RTLD_LAZY)) == NULL)
    {
        printf("dlopen %s\n", dlerror());    
        return -1;
    }
    //定义函数指针,在动态库中查找符号
    func = dlsym(handle, "sayOut");
    func();
    dlclose(handle);
    
    printf("+++++++++++++++++++++++++++++++++++++\n");

    if((handle = dlopen("./libFuncB.so", RTLD_LAZY)) == NULL)
    {
        printf("dlopen %s\n", dlerror());    
        return -1;
    }
    func = dlsym(handle, "sayOut");
    func();
    dlclose(handle);    
}

编译执行:
运行结果
可以发现,我们能显式执行指定动态库的外部函数sayOut了,但是内部函数sayHi的调用还是和我们编译时链接的动态库顺序有关。

进阶二

为何内部函数sayHi都调用了链接顺序第一个的实现?
原因在于动态库中的内部函数没有设置限制,使得sayHi函数也暴露给外部,调用时自然选择第一个函数实现。
用nm指令可以看出,两个函数都暴露出来了
运行结果
再次搜索,可以用gcc编译器的特性来设置动态库函数的导出控制。
可在函数前增加__attribute__ 前缀来控制
更改动态库函数如下:

/* func_A.c*/
#include<stdio.h>

#define DLL_PUBLIC __attribute__((visibility("default")))
#define DLL_LOCAL  __attribute__((visibility("hidden")))

DLL_LOCAL int sayHi()
{
    printf("Hi,this is AAAAA\n");    
    return 0;
}

DLL_PUBLIC int sayOut()
{
    sayHi();
    printf("Use this to introduce AAAAA\n");
    return 0;    
    
}
/* func_B.c*/
#include<stdio.h>

#define DLL_PUBLIC __attribute__((visibility("default")))
#define DLL_LOCAL  __attribute__((visibility("hidden")))

DLL_LOCAL int sayHi()
{
    printf("Hi,this is BBBBB\n");    
    return 0;
}

DLL_PUBLIC int sayOut()
{
    sayHi();
    printf("Use this to introduce BBBBB\n");
    return 0;    
    
}

重新编译动态库,用nm指令可以查看sayHi不再导出了
在这里插入图片描述
重新编译测试程序,则运行正常啦
在这里插入图片描述

进阶三

如果两个动态库中相似函数很多,一个个加 __attribute__前缀也是很大工作量。此时可以编译时设置默认函数不导出,只在需要导出的函数前面加前缀。以libFuncA.c为例:

/*funcA.c*/
#include<stdio.h>

#define DLL_PUBLIC __attribute__((visibility("default")))

int sayHi()
{
    printf("Hi,this is BBBBB\n");    
    return 0;
}

DLL_PUBLIC int sayOut()
{
    sayHi();
    printf("Use this to introduce BBBBB\n");
    return 0;    
    
}

编译时,增加-fvisibility=hidden 参数,则未增加前缀的函数都不会导出
在这里插入图片描述

后记

当然,因为动态设置函数调用实在太复杂,最后我让封装库的童鞋将外部函数改了名字,内部函数用导出前缀来区分,┓( ´∀` )┏

  • 18
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值