避免在链接到静态库时链接到未使用的符号 [英] Avoid linking in unused symbols when linking against static libs

查看:1023
本文介绍了避免在链接到静态库时链接到未使用的符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 apple gcc 来编译一个我要重新分配的dylib。由于各种原因,我使用一些库,让我们说 libz 保持简单。



通常不在Mac系统上找到我希望使用静态链接通过将路径传递到 .a-file 以简化部署。



现在,链接器链接在所有符号从lib到生成的dylib虽然我只引用一个子集。在linux上,我从来没有遇到过这个问题,链接器很乐意丢弃所有未引用的符号,并创建一个非常纤薄的可执行文件,因此应该可能。我现在的dylib文件是比它应该是大10倍大。



我试着用-dead_code连接器标志,但没有效果。



有没有人知道这个解决方案?

解决方案

尝试 -Wl, - gc-sections



至于 -dead_strip -dead_code ):


在-dead_strip
选项你的项目将首先必须
移植使用死代码
剥离。这将包括将
从-gused(默认为-g)更改为
-gfull,并使用新编译器将
程序链接到的所有对象文件重新编译为
Mac OS X 2004年6月发行。另外如果
你建立一个可执行文件加载
插件,它使用符号从
可执行文件,你将必须确保
插件使用的符号不是
(使用
属性(使用)或-exported_symbols_list选项)。如果使用导出列表和构建
a共享库,或者使用ld(1)的
-bundle_loader标志的可执行文件
,则需要包含异常
导出列表中的框架信息
用于导出的C ++符号。这些
符号以.eh结尾,可以用nm(1)工具看到


a href =http://developer.apple.com/library/mac/#documentation/Performance/Conceptual/CodeFootprint/Articles/CompilerOptions.html =nofollow>和:


要从
命令行启用死代码剥离,请将-dead_strip
选项传递给ld。您还应该将
-gfull选项传递给GCC,以便为您的代码生成一组完整的调试符号
。链接器使用这个额外的
调试信息去除可执行文件


希望这有助于。 p>

此答案中的所有内容都位于apple ld static link unused symbols的前几个Google搜索结果中。 :)


I'm using the apple gcc to compile a dylib that I'm going to redistribute. For various reasons I'm using some libraries, let's say libz to keep it simple.

Since this library is not typically found on a Mac system I wish to static link in used symbols into the dylib by passing the path to the .a-file to simplify deployment.

Now, the linker links in all symbols from the lib into the resulting dylib although I only reference a subset. On linux I've never encountered this problem, the linker happily discards all unreferenced symbols and creates a very slim executable, so it should be possible. The dylib file I have now is ~10 times larger than it should.

I've tried fiddle around with the -dead_code linker flag, but to no avail. Perhaps I just don't understand it?

Does anyone know the solution to this?

解决方案

Try -Wl,--gc-sections.

As regards -dead_strip (what you probably meant by -dead_code):

Before turning on the -dead_strip option your project will first have to be "ported" to work with dead code stripping. This will include changing from -gused (the default for -g) to -gfull and re-compiling all of the objects files being linked into your program with the new compiler from the Mac OS X June 2004 release. Also if your building an executable that loads plugins, which uses symbols from the executable, you will have to make sure the symbols the plugins use are not stripped (by using attribute((used)) or the -exported_symbols_list option). If you are using an export list and building a shared library, or an executable that will be used with ld(1)'s -bundle_loader flag, you need to include the symbols for exception frame information in the export list for your exported C++ symbols. These symbols end with .eh and can be seen with the nm(1) tool.

and:

To enable dead-code stripping from the command line, pass the -dead_strip option to ld. You should also pass the -gfull option to GCC to generate a complete set of debugging symbols for your code. The linker uses this extra debugging information to dead strip the executable.

Hope this helps.

All content in this answer was located within the first few Google search results for "apple ld static link unused symbols". :)

这篇关于避免在链接到静态库时链接到未使用的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆