C++ Glibc 的静态链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14567689/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:27:10  来源:igfitidea点击:

Static linking of Glibc

c++g++glibcstatic-linking

提问by Breakdown

How can i compile my app linking statically glibc library, but only the code needed for my app? (Not all lib)

我如何编译我的应用程序静态链接 glibc 库,但只有我的应用程序所需的代码?(不是所有的lib)

Now my compile command:

现在我的编译命令:

g++  -o newserver  test.cpp ... -lboost_system -lboost_thread -std=c++0x

Thanks!

谢谢!

回答by Anton Kovalenko

That's what -staticdoes (as described in another answer): unneeded modules won't get linked into your program. But your expectations on the amount of stuff which is needed(in a sense that we can't convince linker to the contrary) may be too optimistic.

这就是-static(如另一个答案中所述):不需要的模块不会链接到您的程序中。但是,你就这东西量预期需要(在一定意义上,我们无法说服连接相反)可能过于乐观。

If you trying to do it for portability (running an executable on other machines with older glibc or something like that), there is one easy test question to see if you're going to get what you want:

如果您为了可移植性而尝试这样做(在具有较旧 glibc 或类似内容的其他机器上运行可执行文件),则有一个简单的测试问题可以查看您是否会得到您想要的:

Did you think of the problem with libnss, and are you sure it is not going to bite you?

你有没有想过这个问题libnss,你确定它不会咬你吗?

If your answer is yes, maybe it makes sense to go on. If the answer is no, or the question seems too obscure and there is no answer, just quit your expirements with statically linked glibc: it has more chance to hurt than help.

如果你的答案是肯定的,也许继续下去是有意义的。如果答案是否定的,或者问题看起来太模糊并且没有答案,只需使用静态链接的 glibc 退出您的到期:它有更多的机会伤害而不是帮助。

回答by Mats Petersson

Add -staticto the compile line. It will only add what your application needs [and of course, any functions the functions you application calls, and any functions those functions call, including a bunch of startup code and some other bits and pieces], so it will be around 800K (for a simple "hello world" program) on an x86 machine. Other architectures vary. Since boost probably also calls the standard library at least a little bit, it's likely that you will have more than 800K added to your appliciation. But it only applies functions used by any of the code in the final binary, not the entire library [about 2MB as a shared library].

添加-static到编译行。它只会添加您的应用程序需要的内容[当然,还有您应用程序调用的任何函数,以及这些函数调用的任何函数,包括一堆启动代码和其他一些零碎的部分],因此大约为 800K(对于一个简单的“hello world”程序)在 x86 机器上。其他架构各不相同。由于 boost 可能也至少会调用一点标准库,因此您的应用程序中可能会添加超过 800K。但它只适用于最终二进制文件中的任何代码使用的函数,而不是整个库[作为共享库大约 2MB]。

If you ONLY want link glibc, you will need to modify the linking line to your compile to: -Wl,-Bstatic -libc -Wl,-Bdynamic. This will prevent any other library from being linked statically [you sometimes need to have more than one of these statements, as sometimes something pulled in by another library requires "more" from glibc to be pulled in - don't worry, it won't bring in anything more than the linker thinks is necessary].

如果您只想要链接 glibc,则需要将链接行修改为编译为: -Wl,-Bstatic -libc -Wl,-Bdynamic。这将防止任何其他库被静态链接[您有时需要有多个这些语句,因为有时另一个库拉入的某些东西需要从 glibc 拉入“更多” - 别担心,它不会' t 引入任何超出链接器认为必要的东西]。