不要在自己的静态库中公开已使用库中的符号

时间:2023-01-15 15:59:54

I am writing a reusable static library for the iPhone, following the directions provided here.

我正按照此处提供的说明为iPhone编写可重复使用的静态库。

I want to use minizip in my library internally, but don't want to expose it to the user.

我想在内部使用minizip,但不想将它暴露给用户。

It should be possible for the user to include minizip themselves, possibly a different version, and not cause *es with my "inner" minizip version.

用户应该可以自己包括minizip,可能是不同的版本,并且不会导致与我的“内部”迷你剪辑版本发生冲突。

Is this possible?

这可能吗?

Edit:

编辑:

I've tried adding -fvisibility=hidden to additional compiler flags for minizip files and changing functions to be __private_extern__ and __attribute__((visibility("hidden"))), but it still seems to produce defined external symbols:

我已经尝试将-fvisibility = hidden添加到minizip文件的其他编译器标志,并将函数更改为__private_extern__和__attribute __((visibility(“hidden”))),但它似乎仍然生成定义的外部符号:

00000918 T _unzOpen
0000058e T _unzOpen2
00001d06 T _unzOpenCurrentFile
00001d6b T _unzOpenCurrentFile2
...

Edit #2:

编辑#2:

Apparently the symbols marked with these annotations are only made private by the linker, which never happens when Xcode builds the sources, since it adds the -c parameter ("Compile or assemble the source files, but do not link.")

显然,标记有这些注释的符号仅由链接器设为私有,这在Xcode构建源时不会发生,因为它添加了-c参数(“编译或汇编源文件,但不链接。”)

3 个解决方案

#1


10  

You could rename all exported symbol from minizip with objcopy.

您可以使用objcopy重命名minizip中的所有导出符号。

something like

就像是

objcopy -redefine-sym=minizip.syms yourstaticlibray.a 

and minizip.syms

和minizip.syms

_unzOpen     _yourownprefix_unzOpen
_unzOpen2    _yourownprefix_unzOpen2
...          ...

No * if an executable is linked with an other minizip.a and yourstaticlibray.a, and because you renamed all the symbol in yourstaticlibray.a your call inside yourstaticlibray.a to minizip will use the prefixed symbol, and not the unzOpen one.

如果可执行文件与其他minizip.a和yourstaticlibray.a链接,并且因为您重命名了yourstaticlibray.a中的所有符号,则在yourstaticlibray.a中调用minizip将使用带前缀的符号,而不是unzOpen符号。

#2


4  

Since static library is nothing more than a set of .o files (which are not linked yet, as you have mentioned), the only way to completely hide presence of minizip from the outside world is to somehow compile minizip and your library together as a single compilation unit and make minizip functions/variables static.

由于静态库只不过是一组.o文件(尚未链接,正如你所提到的),完全隐藏外部世界minizip存在的唯一方法是以某种方式将minizip和你的库一起编译为单个编译单元并使minizip函数/变量静态。

You could have a look at how does SQLite do the "amalgamation" process which turns library source code into single .c file for further compilation: The SQLite Amalgamation.

您可以看看SQLite如何进行“合并”过程,将库源代码转换为单个.c文件以进行进一步编译:SQLite Amalgamation。

As a bonus you'll get better optimization (really recent GCC and Binutils are able to make link-time optimizations, but this functionality is not released yet).

作为奖励,您将获得更好的优化(实际上最近的GCC和Binutils能够进行链接时优化,但此功能尚未发布)。

#3


-1  

You'll want to take a look at Dynamic Library Programming Topics, specifically the Symbol Exporting Strategies section and the gcc -exported_symbols_listFILE option.

您需要查看动态库编程主题,特别是符号导出策略部分和gcc -exported_symbols_listFILE选项。

#1


10  

You could rename all exported symbol from minizip with objcopy.

您可以使用objcopy重命名minizip中的所有导出符号。

something like

就像是

objcopy -redefine-sym=minizip.syms yourstaticlibray.a 

and minizip.syms

和minizip.syms

_unzOpen     _yourownprefix_unzOpen
_unzOpen2    _yourownprefix_unzOpen2
...          ...

No * if an executable is linked with an other minizip.a and yourstaticlibray.a, and because you renamed all the symbol in yourstaticlibray.a your call inside yourstaticlibray.a to minizip will use the prefixed symbol, and not the unzOpen one.

如果可执行文件与其他minizip.a和yourstaticlibray.a链接,并且因为您重命名了yourstaticlibray.a中的所有符号,则在yourstaticlibray.a中调用minizip将使用带前缀的符号,而不是unzOpen符号。

#2


4  

Since static library is nothing more than a set of .o files (which are not linked yet, as you have mentioned), the only way to completely hide presence of minizip from the outside world is to somehow compile minizip and your library together as a single compilation unit and make minizip functions/variables static.

由于静态库只不过是一组.o文件(尚未链接,正如你所提到的),完全隐藏外部世界minizip存在的唯一方法是以某种方式将minizip和你的库一起编译为单个编译单元并使minizip函数/变量静态。

You could have a look at how does SQLite do the "amalgamation" process which turns library source code into single .c file for further compilation: The SQLite Amalgamation.

您可以看看SQLite如何进行“合并”过程,将库源代码转换为单个.c文件以进行进一步编译:SQLite Amalgamation。

As a bonus you'll get better optimization (really recent GCC and Binutils are able to make link-time optimizations, but this functionality is not released yet).

作为奖励,您将获得更好的优化(实际上最近的GCC和Binutils能够进行链接时优化,但此功能尚未发布)。

#3


-1  

You'll want to take a look at Dynamic Library Programming Topics, specifically the Symbol Exporting Strategies section and the gcc -exported_symbols_listFILE option.

您需要查看动态库编程主题,特别是符号导出策略部分和gcc -exported_symbols_listFILE选项。