C/C++中控制动态库的符号可见性

一、需求

写了一个动态库供客户使用,此库使用了一些第三方静态库,无奈客户也使用了这些第三方库,从而产生了符号冲突。
所以需要隐藏此库中第三方库的导出符号。

二、可选解决方案

最终选择方案三,即利用gnu的visibility 属性。
(1)修改CMakeList.txt,添加如下属性,从而使所有符号默认不导出。

set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_C_VISIBILITY_PRESET hidden)

(2)设置visibility属性,导出so的关键符号

void __attribute__ ((visibility ("default"))) fun();

三、参考

1.https://stackoverflow.com/questions/17080869/what-is-the-cmake-equivalent-to-gcc-fvisibility-hidden-when-controlling-the-e
2.https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes
3.https://www.ibm.com/developerworks/cn/aix/library/au-aix-symbol-visibility/
4.https://holtstrom.com/michael/blog/post/437/Shared-Library-Symbol-Conflicts.html

你可能感兴趣的:(编程语言)