C语言 静态链接 glibc,但使用 GCC 动态链接其他一些库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13187499/
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
Link glibc statically but some other library dynamically with GCC
提问by nkdm
I need to statically link glibc to my project, because the target platform supports only a very old one ( but it works with statically linked glibc from my toolchain, I have checked it)
我需要将 glibc 静态链接到我的项目,因为目标平台只支持一个非常旧的平台(但它可以与我的工具链中的静态链接 glibc 一起使用,我已经检查过了)
Unfortunately, this application has to make use of pthread library, but statically linked libpthread takes too much space.
不幸的是,这个应用程序必须使用 pthread 库,但是静态链接的 libpthread 占用太多空间。
I would like to statically link glibc, and dynamically pthread.
我想静态链接 glibc 和动态 pthread。
After running this command
运行此命令后
powerpc-unknown-linux-gnu-gcc object_files -lrt -lpthread -Wl,-Bstatic -lc
I get:
我得到:
/powerpc-unknown-linux-gnu/bin/ld: cannot find -lgcc_s
采纳答案by Anycorn
There is a -static-libgccif that may help
有一个-static-libgcc如果可能会有所帮助
回答by R.. GitHub STOP HELPING ICE
You should be using -static, not -Wl,-static. The latter bypasses gcc's knowledge, and therefore gcc is still trying to link the shared libgcc_s.sorather than the static libgcc_eh.a.
您应该使用-static,而不是-Wl,-static。后者绕过了 gcc 的知识,因此 gcc 仍在尝试链接 sharedlibgcc_s.so而不是 static libgcc_eh.a。
If your aim is to link libc statically but libpthread dynamically, this is simply not going to work. You cannot mix and match different versions of libpthread; it's part of glibc, just a separate file, and the internals need to match. Even with the same version, I think linking libc statically and libpthread dynamically will be very broken.
如果您的目标是静态链接 libc 而动态链接 libpthread,这根本行不通。你不能混合和匹配不同版本的 libpthread;它是 glibc 的一部分,只是一个单独的文件,内部需要匹配。即使使用相同的版本,我认为静态链接 libc 和动态链接 libpthread 也会很糟糕。
If glibc is too big for your needs, you could try an alternate libc like uClibcor musl.

