设为首页 加入收藏

TOP

Linux动态链接库导出函数(一)
2014-11-24 07:43:27 来源: 作者: 【 】 浏览:4
Tags:Linux 动态 链接 导出 函数

近日在项目中遇到一个问题,需要在so库中控制导出函数,即像windows平台一样,只允许制定的函数导出,而其他函数需要屏蔽掉。


注意:参考文献中有一些函数是不能编译通过的。


编译环境:gcc 4.4.3


在windows中,我们可以指定__declspec(dllexport)定义来控制导出函数,在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参数不会对该函数起作用。所以,设置了-fvisibility=hidden参数之后,只有设置了__attribute__ ((visibility("default")))的函数才是对外可见的,如此则效果等同于Visual Studio下的__declspec(dllexport)定义。


下面看一个例子:


#include
#include
#include
extern int test (int i);
__attribute ((visibility("default"))) int test2 (int i)
{
test(i);
printf("this is test2/n");
}
int test3 (int i)
{
printf("this is test 3/n");
}


此时调用以下命令编译:


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


可以获得test.so文件,用以下命令查看so文件属性:


readelf -s test.so


得到以下结果:


Symbol table '.dynsym' contains 12 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 00000000 0 NOTYPE LOCAL DEFAULT UND
1: 00000000 0 NOTYPE WEAK DEFAULT UND __gmon_start__
2: 00000000 0 NOTYPE WEAK DEFAULT UND _Jv_RegisterClasses
3: 00000000 0 NOTYPE GLOBAL DEFAULT UND test
4: 00000000 0 FUNC GLOBAL DEFAULT UND puts@GLIBC_2.0 (2)
5: 00000000 0 FUNC WEAK DEFAULT UND __cxa_finalize@GLIBC_2.1.3 (3)
6: 00002014 0 NOTYPE GLOBAL DEFAULT ABS _end
7: 0000200c 0 NOTYPE GLOBAL DEFAULT ABS _edata
8: 0000049c 31 FUNC GLOBAL DEFAULT 12 test2
9: 0000200c 0 NOTYPE GLOBAL DEFAULT ABS __bss_start
10: 00000380 0 FUNC GLOBAL DEFAULT 10 _init
11: 00000508 0 FUNC GLOBAL DEFAULT 13 _fini
Symbol table '.symtab' contains 56 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 00000000 0 NOTYPE LOCAL DEFAULT UND
1: 000000f4 0 SECTION LOCAL DEFAULT 1
2: 00000118 0 SECTION LOCAL DEFAULT 2
3: 0000015c 0 SECTION LOCAL DEFAULT 3
4: 00000198 0 SECTION LOCAL DEFAULT 4
5: 00000258 0 SECTION LOCAL DEFAULT 5
6: 000002e0 0 SECTION LOCAL DEFAULT 6
7: 000002f8 0 SECTION LOCAL DEFAULT 7
8: 00000328 0 SECTION LOCAL DEFAULT 8
9: 00000370 0 SECTION LOCAL DEFAULT 9
10: 00000380 0 SECTION LOCAL DEFAULT 10
11: 000003b0 0 SECTION LOCAL DEFAULT 11
12: 000003e0 0 SECTION LOCAL DEFAULT 12
13: 00000508 0 SECTION LOCAL DEFAULT 13
14: 00000524 0 SECTION LOCAL DEFAULT 14
15: 00000544 0 SECTION LOCAL DEFAULT 15
16: 00001f04 0 SECTION LOCAL DEFAULT 16
17: 00001f0c 0 SECTION LOCAL DEFAULT 17
18: 00001f14 0 SECTION LOCAL DEFAULT 18
19: 00001f18 0 SECTION LOCAL DEFAULT 19
20: 00001fe8 0 SECTION LOCAL DEFAULT 20
21: 00001ff4 0 SECTION LOCAL DEFAULT 21
22: 00002008 0 SECTION LOCAL DEFAULT 22
23: 0000200c 0 SECTION LOCAL DEFAULT 23
24: 00000000 0 SECTION LOCAL DEFAULT 24
25: 00000000 0 FILE LOCAL DEFAULT ABS crtstuff.c
26: 00001f04 0 OBJEC

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言可变长参数函数与默认参数提.. 下一篇Android 2.3 StrictMode 使用

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·在 Redis 中如何查看 (2025-12-26 03:19:03)
·Redis在实际应用中, (2025-12-26 03:19:01)
·Redis配置中`require (2025-12-26 03:18:58)
·Asus Armoury Crate (2025-12-26 02:52:33)
·WindowsFX (LinuxFX) (2025-12-26 02:52:30)