Linux 下 GCC 编译共享库控制导出函数的方法

通过一些实际项目的开发,发现这样一个现象,在 Windows 下可以通过指定 __declspec(dllexport) 定义来控制 DLL(动态链接库)中哪些函数可以导出,暴露给其他程序链接使用,哪些函数是 DLL 内部自己使用;而在 Linux 下不存在 dllexport 这样的指示字,默认情况下 GCC 编译 SO(共享库)时把代码中的所有函数都导出了,那么如何实现 Windows 下的那种效果,由我们自己来控制共享库导出函数呢?

其实在 Linux 下也有类似的控制机制。在 GCC 帮助文档中,对 -fvisibility=default|internal|hidden|protected 参数及其取值有这样一段描述:
a superior solution made possible by this option to marking things hidden when the default is public is to make the default hidden and mark things public. This is the norm with DLL's on Windows and with -fvisibility=hidden and "__attribute__((visibility("default")))" instead of "__declspec(dllexport)" you get almost identical semantics with identical syntax. This is a great boon to those working with cross-platform projects.
需要了解的是,在 Linux下,源文件中的所有函数的 visibility 属性都被默认设置为 public,在编译命令中加入 -fvisibility=hidden 参数后,会将所有默认的 public 属性变为hidden。此时,如果对函数设置 __attribute__((visibility("default"))) 参数,则该函数按 public 属性处理,-fvisibility=hidden 参数不会对该函数起作用。所以,针对 GCC 设置了 -fvisibility=hidden 参数之后,只有设置了 __attribute__((visibility("default"))) 属性的函数才是对外可见的,如此则效果等同于 Visual Studio 下的 __declspec(dllexport) 定义。比如:

extern int foo(int a, int b, int c) __attribute__((visibility("default")));

这样声明即可。

原文地址:https://www.cnblogs.com/cyberniuniu/p/5021975.html