分享屏蔽靜態庫接口的一種方法.
準備
hello.c
:
#include <stdio.h>
__attribute__ ((visibility ("default"))) void hello() {
printf("Hello World!\n");
}
hello.h
:
#ifndef __HELLO__H
#define __HELLO__H
#ifdef __cplusplus
extern "C" {
#endif
void hello();
#ifdef __cplusplus
}
#endif
#endif
bye.c
:
#include <stdio.h>
void bye() {
printf("Bye Bye!\n");
}
bye.h
:
#ifndef __BYE__H
#define __BYE__H
#ifdef __cplusplus
extern "C" {
#endif
void bye();
#ifdef __cplusplus
}
#endif
#endif
編譯
編譯時使用-fvisibility=hidden
,可以默認將符號隱藏;需要對外的符號使用__attribute__ ((visibility ("default")))
修飾即可:
$ gcc -fvisibility=hidden -I. -c hello.c -o hello.o
$ gcc -fvisibility=hidden -I. -c bye.c -o bye.o
其中hello()
未被隱藏,bye()
是被隱藏的.
鏈接
將生成的兩個.o
文件重定位到libt.o
中:
$ ld -r hello.o bye.o -o libt.o
去除無用的符號
$ strip --strip-unneeded libt.o
隱藏的符號本地化(我也不知道中文怎麼翻譯了)
$ objcopy --localize-hidden libt.o libt_hidden.o
打包成靜態庫
$ ar crv libt.a libt_hidden.o
驗證
調用未被隱藏的hello()
test1.c
:
#include "hello.h"
int main(void) {
hello();
return 0;
}
編譯並運行
$ gcc -I. test1.c -L. -lt -o test
$ ./test
Hello World!
調用隱藏的bye()
test2.c
#include "bye.h"
int main(void) {
bye();
return 0;
}
編譯並運行
$ gcc -I. test2.c -L. -lt -o test
$ ./test
/tmp/ccdaJT7s.o: In function `main':
test2.c:(.text+0xa): undefined reference to `bye'
collect2: error: ld returned 1 exit status
微信公衆號同步更新,微信搜索"AnSwEr不是答案"或者掃描二維碼,即可訂閱。
- GitHub:AnSwErYWJ
- Blog:http://www.answerywj.com
- Email:[email protected]
- Weibo:@AnSwEr不是答案