经常遇到如此情况,动态库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 -