I've created a static library in GCC, but I'd like to hide most of the symbols.
我在GCC中创建了一个静态库,但是我想隐藏大部分符号。
For example, test1.c:
例如,test1.c:
extern void test2(void);
void test1(void) {
printf("test1: ");
test2();
}
test2.c:
extern void test1(void);
void test2(void) {
printf("test2\n");
}
library_api.c:
extern void test1(void);
extern void test2(void);
void library_api(void) {
test1();
test2();
}
Now compile with:
现在编译:
gcc -c test1.c -o test1.o
gcc -c test2.c -o test2.o
gcc -c library_api.c -o library_api.o
ar rcs libapi.a test1.o test2.o library_api.o
How do I get only the "library_api()" function to show up for:
如何仅显示“library_api()”函数以显示:
nm libapi.a
instead of the functions "test1()", "test2()", and "library_api()"? In other words, how do I hide "test1()" and "test2()" from showing up and being callable to external users of libapi.a? I don't want external users to know anything about internal test functions.
而不是函数“test1()”,“test2()”和“library_api()”?换句话说,我如何隐藏“test1()”和“test2()”从显示和可调用到libapi.a的外部用户?我不希望外部用户知道任何有关内部测试功能的信息。
2 个解决方案
#1
9
The simplest solution is to #include test1.c and test2.c into library_api.c
, and only compile that file. Then you can make test1() and test2() static.
最简单的解决方案是#include test1.c和test2.c到library_api.c,并且只编译该文件。然后你可以使test1()和test2()静态。
Alternatively, you can combine the object files with ld -r
, and use objcopy --localize-symbols
to make the test functions static after linking. As this can get fairly tedious, I really recommend the first option, though.
或者,您可以将目标文件与ld -r组合使用,并使用objcopy --localize-symbols在链接后使测试函数保持静态。因为这可能相当繁琐,但我真的推荐第一个选项。
#2
2
ld has the option
ld有选择权
--retain-symbols-file FILE Keep only symbols listed in FILE
--retain-symbols-file FILE仅保留FILE中列出的符号
to allow you to explicitly name the symbols you want to keep.
允许您明确命名要保留的符号。
#1
9
The simplest solution is to #include test1.c and test2.c into library_api.c
, and only compile that file. Then you can make test1() and test2() static.
最简单的解决方案是#include test1.c和test2.c到library_api.c,并且只编译该文件。然后你可以使test1()和test2()静态。
Alternatively, you can combine the object files with ld -r
, and use objcopy --localize-symbols
to make the test functions static after linking. As this can get fairly tedious, I really recommend the first option, though.
或者,您可以将目标文件与ld -r组合使用,并使用objcopy --localize-symbols在链接后使测试函数保持静态。因为这可能相当繁琐,但我真的推荐第一个选项。
#2
2
ld has the option
ld有选择权
--retain-symbols-file FILE Keep only symbols listed in FILE
--retain-symbols-file FILE仅保留FILE中列出的符号
to allow you to explicitly name the symbols you want to keep.
允许您明确命名要保留的符号。