我怎么只包括使用符号时用gcc静态链接? [英] How do I include only used symbols when statically linking with gcc?

查看:167
本文介绍了我怎么只包括使用符号时用gcc静态链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我部署与海湾合作委员会,4.3.2-1.1(Debian的)编译的小程序。这项计划将部署在虚拟机模板,从5的Debain到尖端的Fedora,Ubuntu的,Slackware的,凯旋门等。

I'm deploying a small program compiled with gcc, 4.3.2-1.1 (Debian). This program will be deployed on virtual machine templates ranging from Debain 5 to bleeding edge Fedora, Ubuntu, Slackware, Arch and others.

程序依赖于从Xen的库这是只在一个不稳定的树可用的一些符号。因此,在虚拟机模板安装通过各自的包管理器Xen的图书馆解决不了我的眼前问题。

The program depends on some symbols from Xen's libraries which are only available in an unstable tree. Hence, installing Xen's libraries via respective package managers on the virtual machine templates would not solve my immediate issue.

直到我打包我自己的这些库的版本,我需要静态链接可执行文件。

Until I package my own version of these libraries, I need to statically link the executable.

请问GCC 4.3-X,默认情况下只包括实际上的符号使用的时候静态链接,或者是有,我应该传递给另一个链接优化标志?我知道,静态链接是坏的,我做的只是作为一个临时的变通。

Does gcc 4.3-x, by default only include symbols that are actually used when statically linking, or is there another optimization flag that I should be passing to the linker? I know that statically linking is bad, I'm doing it only as a temporary work around.

推荐答案

此问题不仅涉及到GCC,而是给ld(1)了。

This issue is related not only to gcc, but to ld(1) too.

在默认情况下,海湾合作委员会并没有消除死code,可以通过编译/链接的可执行文件,然后运行该检查

By default, gcc doesn't eliminate dead code, you can check this by compiling/linking executable, and then running

objdump的-d的a.out

这表明你在你的可执行程序的所有功能。

which shows you all functions in your executable.

简单的谷歌搜索给这个<一个href=\"http://embeddedfreak.word$p$pss.com/2009/02/10/removing-unused-functionsdead-$c$cs-with-gccgnu-ld/\"相对=nofollow>链接。

Simple "googling" give this link.

因此​​,删除未使用的功能,你需要:

So, to remove unused functions, you need:


  • 用-fdata截面,以保持在单独的数据段和-ffunction截面保持在单独的段函数的数据,因此,它们(数据和函数)编译可在未使用丢弃。

  • 链接以--gc截面删除未使用的部分。

这篇关于我怎么只包括使用符号时用gcc静态链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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