__attribute__ ((default)) 和 __attribute__ ((hidden))

制作一个共享库

1 /* a.h */
2 int func();
1 /* a.c */
2 #include <stdio.h>
3 #include "a.h"
4 
5 int func()
6 {
7     printf("### func ###\n");
8     return 0;
9 }
gcc -shared -o liba.so a.c -fPIC


main.c

1 #include "a.h"
2 
3 int main()
4 {
5     func();
6     return 0
7 }
gcc -o main main.c -la -L. -Wl,-rpath=.

 

如果在 func 前面加上

__attribute__((visibility("hidden")))

在编译 main 时,报错:

/tmp/ccbxiXwp.o: In function `main':
main.c:(.text+0x7): undefined reference to `func'
collect2: error: ld returned 1 exit status


如果在编译动态库时加上 -fvisibility=hidden,表示动态库的符号都是 hidden的

在函数前加上 __attribute__((visibility("default"))) 可以使函数对外可见

 

posted @ 2016-04-16 16:17  Kjing  阅读(918)  评论(0编辑  收藏  举报