Linux下gcc编译控制动态库导出函数小结

根据说明文档“How To Write Shared Libraries"介绍,

有四种方法:

1. 在方法声明定义时,加修饰:__attribute__((visibility("hidden")))

就是说将不公开的函数都加上这个属性,没加的就是可见的


2. gcc 在链接时设置 -fvisibility=hidden,则不加 visibility声明的都默认为hidden; gcc默认设置 -fvisibility=default,即全部可见;

在gcc中加了这个设置之后表示所有的函数都是对外不可见了,然后在代码里面对于想公开的函数加上 __attribute__((visibility("default")))


3. 使用export map,gcc -Wl,--version-script=export.map, 在export.map中指定

{

global:export_func;

local:*;

};

则除了export_func外,全部为内部可见;


4. 使用libtool的export-symbols选项,没试过,略;

 

以上方法,对于gcc的visibility功能需要 gcc 4以上版本,

常见的宏,用于等同于 windows下的 __declspec(dllexport)(摘自:http://gcc.gnu.org/wiki/Visibility):

#if defined _WIN32 || defined __CYGWIN__

  #ifdef BUILDING_DLL

    #ifdef __GNUC__

      #define DLL_PUBLIC __attribute__((dllexport))

    #else

      #define DLL_PUBLIC __declspec(dllexport) // Note: actually gcc seems to also supports this syntax.

    #endif

  #else

    #ifdef __GNUC__

      #define DLL_PUBLIC __attribute__((dllimport))

    #else

      #define DLL_PUBLIC __declspec(dllimport) // Note: actually gcc seems to also supports this syntax.

  #endif

  #define DLL_LOCAL

#else

  #if __GNUC__ >= 4

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

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

  #else

    #define DLL_PUBLIC

    #define DLL_LOCAL

  #endif

#endif



实践中发现几个有意思的东西[实验环境为ubuntu 9.10, gcc 4.4.1]:

1. gcc -fvisibility=hidden 只在链接时传入的.c文件起作用,对.o文件不其作用;

比如test.c test1.c, 使用以下命令:

gcc -shared -fvisibility=hidden -o test.so test.c test1.c

和命令

gcc -c test.c test1.c

gcc -shared -fvisibility=hidden -o test.so test.o test1.o

生成的test.so中的对应可见性是不一样的, 使用“readelf -s test.so”查看发现:

第一个达到预期目的,即将两个.c文件中的functions设为HIDDEN,

而第2个则不行,-fvisibility=hidden不起作用;

再用gcc -shared -fvisibility=hidden -o test.so test.o test1.c

生成的so,则可发现test1.c中的函数为HIDDEN,但test.o中的函数仍为DEFAULT;



2. 实验通过export map的方法,通过readelf总是发现函数仍然可见,后来搜索发现:

需同时使用ld --retain-symbols-file --version-script 两个选项方能达到 使得elf文件中的.dynsym和.symtab

中的符号表都得到控制;其中--retain-symbols-file控制静态符号表,--version-script则对应dynamic符号表;
另外可以参考 http://blog.csdn.net/shuiyingzi5/article/details/8108190
 
 
转自 http://blog.csdn.net/zdragon2002/article/details/6061962
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值