Linux 如何创建在每个发行版上运行的静态二进制文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14842268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create static binary which runs on every distro?
提问by user1873947
Some linux apps like supertuxkart or regnum online have static binaries, which after downloading just work without needing to install any shared library. On every distro. How can I make such an app?
一些 linux 应用程序如 supertuxkart 或 regnum online 具有静态二进制文件,下载后无需安装任何共享库即可运行。在每个发行版上。我怎样才能制作这样的应用程序?
采纳答案by thiton
Ensure that all your resources are contained in the executable and link the executable statically:
确保所有资源都包含在可执行文件中并静态链接可执行文件:
gcc -o foo main.o -static -lbaz -lbar
However, this also has drawbacks. Look up dynamic linking.
然而,这也有缺点。查找动态链接。
回答by smoe
@user1873947, the tool "ldd" tells you for dynamiclibraries the dependencies of one library on others, but not for staticlibraries. For staticlibraries look at the tool "nm" to give you names of symbols ("function names") that could not be resolved. At some later point you may be interested in the tool "objdump". If you don't immediately know what library a missing function is from, then check if you find the function declared in /usr/include somewhere. You do that with the tool "grep" - but omit a leading "_" of symbol names. The file declaring it then typically stems from a -dev package that also ships the corresponding .a file.
@ user1873947,工具“ldd”告诉你动态库一个库对其他库的依赖关系,而不是静态库。对于静态库,请查看工具“nm”为您提供无法解析的符号名称(“函数名称”)。稍后您可能会对“objdump”工具感兴趣。如果您不能立即知道缺少的函数来自哪个库,请检查是否在 /usr/include 某处找到了声明的函数。您可以使用工具“grep”来做到这一点 - 但省略符号名称的前导“_”。声明它的文件通常源自一个 -dev 包,该包也提供相应的 .a 文件。