动态库链接静态库防止符号污染

经常遇到如此情况,动态库A依赖静态库B,在制作动态库A时,链接静态库B,动态库A中会新增静态库B的符号,从而污染动态库A。

如何规避这种情况呢? 

答案时在link时添加选项

-Wl,--exclude-libs,ALL

是将所有的静态库的符号隐藏。

例子

1. 先制作一个静态库 libffunc.a

头文件 ffunc.h

//  ffunc.h
#pragma once

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

DLL_PUBLIC int ftest1();
DLL_LOCAL int ftest2();
 int ftest3();

cpp ffunc.cpp

// ffunc.cpp
#include "ffunc.h"


int ftest1() {
    return 11;
}
int ftest2() {
    return 12;
}
int ftest3() {
    return 13;
}
// 制作静态库
g++ -c -o ffunc.o -static  -fPIC -fvisibility=hidden ./ffunc.cpp

ar rcs libffunc.a ffunc.o

查看静态库libffunc.a的符号

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值