Linux 强制 GCC 静态链接,例如 pthreads(而不是动态链接)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10390584/
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
Force GCC to static-link e.g. pthreads (and not dynamic link)
提问by imacake
My program is built as a loader and many modules which are shared libraries. Now one of those libraries uses pthreads and it seems its bound to the module dynamically (loaded on startup). Now it'd be simplier if i could force pthreads to be linked into the module file. GCC on linux, how do i do? I guess a libpthread.a is necessary....
我的程序被构建为一个加载器和许多共享库的模块。现在这些库之一使用 pthreads,它似乎动态绑定到模块(在启动时加载)。现在,如果我可以强制将 pthread 链接到模块文件中,那就更简单了。Linux 上的 GCC,我该怎么做?我想 libpthread.a 是必要的....
回答by Employed Russian
While linking libpthread.a
into a shared library is theoretically possible, it is a really bad idea. The reason is that libpthread
is part of glibc
, and allparts of glibc
mustmatch exactly, or you'll see strange and un-explainable crashes.
虽然链接libpthread.a
到共享库在理论上是可能的,但这是一个非常糟糕的主意。原因是它libpthread
是 的一部分glibc
,并且所有部分都glibc
必须完全匹配,否则您会看到奇怪且无法解释的崩溃。
So linking libpthread.a
into your shared library will:
因此,链接libpthread.a
到您的共享库将:
- Cause your program to crash when moved to a machine with a different version of
glibc
- Cause your existing program to crash when your current machine's
glibc
is upgraded, but your module is not re-linked against updatedlibpthread.a
.
- 导致您的程序在移动到具有不同版本的计算机时崩溃
glibc
- 当您当前的机器
glibc
升级时,导致您现有的程序崩溃,但您的模块不会重新链接到更新的libpthread.a
.
Spare yourself aggravation, and don't do that.
免得自己加重病情,不要那样做。