Linux gcc中共享库函数的静态链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/725472/
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
Static link of shared library function in gcc
提问by suresh
How can I link a shared library function statically in gcc?
如何在 gcc 中静态链接共享库函数?
采纳答案by Sam Liao
Refer to:
参考:
http://linux.derkeiler.com/Newsgroups/comp.os.linux.development.apps/2004-05/0436.html
http://linux.derkeiler.com/Newsgroups/comp.os.linux.development.apps/2004-05/0436.html
You need static version of the library to link.
您需要链接库的静态版本。
A shared library is actually an executable in a special format with entry points specified (and some sticky addressing issues included). It does not have all the information needed to link statically.
共享库实际上是具有指定入口点的特殊格式的可执行文件(包括一些粘性寻址问题)。它没有静态链接所需的所有信息。
You can't statically link shared library (or dynamically link static)
您不能静态链接共享库(或动态链接静态)
Flag -static will force linker to use static library (.a) instead of shared (.so) But. Static libraries not always installed by default. So if you need static link you have to install static libraries.
标志 -static 将强制链接器使用静态库 (.a) 而不是共享 (.so) 但是。默认情况下并不总是安装静态库。因此,如果您需要静态链接,则必须安装静态库。
Another possible approach is use statifieror Ermine. Both tools take as input dynamically linked executable and as output create self-contained executable with all shared libraries embedded.
另一种可能的方法是使用statifier或Ermine。这两种工具都将动态链接的可执行文件作为输入,并作为输出创建嵌入所有共享库的自包含可执行文件。
回答by nothrow
In gcc, this isn't supported. In fact, this isn't supported in any existing compiler/linker i'm aware of.
在 gcc 中,这不受支持。事实上,我所知道的任何现有编译器/链接器都不支持这一点。
回答by Eugene Bujak
If you want to link, say, libapplejuicestatically, but not, say, liborangejuice, you can link like this:
如果你想静态链接libapplejuice,而不是libapplejuice,你可以像这样链接:
gcc object1.o object2.o -Wl,-Bstatic -lapplejuice -Wl,-Bdynamic -lorangejuice -o binary
There's a caveat -- if liborangejuice
uses libapplejuice
, then libapplejuice
will be dynamically linked too.
有一个警告——如果liborangejuice
使用libapplejuice
,那么libapplejuice
也将被动态链接。
You'll have to link liborangejuice
statically alongside with libapplejuice
to get libapplejuice
static.
您必须liborangejuice
与 with 一起静态链接libapplejuice
才能获得libapplejuice
静态。
And don't forget to keep -Wl,-Bdynamic
else you'll end up linking everything static, including libc
(which isn't a good thing to do).
并且不要忘记保持-Wl,-Bdynamic
其他内容,否则您最终将链接所有静态内容,包括libc
(这不是一件好事)。
回答by NeoEGM
If you have the .a file of your shared library (.so) you can simply include it with its full path as if it was an object file, like this:
如果您有共享库 (.so) 的 .a 文件,您可以简单地将其包含在其完整路径中,就像它是一个目标文件一样,如下所示:
This generates main.o by just compiling:
只需编译即可生成 main.o:
gcc -c main.c
This links that object file with the corresponding static library and creates the executable (named "main"):
这将该对象文件与相应的静态库链接并创建可执行文件(名为“main”):
gcc main.o mylibrary.a -o main
Or in a single command:
或者在单个命令中:
gcc main.c mylibrary.a -o main
It could also be an absolute or relative path:
它也可以是绝对或相对路径:
gcc main.c /usr/local/mylibs/mylibrary.a -o main
回答by Francis
A bit late but ... I found a link that I saved a couple of years ago and I thought it might be useful for you guys:
有点晚了但是......我找到了一个我几年前保存的链接,我认为它可能对你们有用:
CDE: Automatically create portable Linux applications
CDE:自动创建可移植的 Linux 应用程序
http://www.pgbovine.net/cde.html
http://www.pgbovine.net/cde.html
- Just download the program
Execute the binary passing as a argument the name of the binary you want make portable, for example: nmap
./cde_2011-08-15_64bit nmap
- 只需下载程序
执行二进制文件,将你想要移植的二进制文件的名称作为参数传递,例如:nmap
./cde_2011-08-15_64bit nmap
The program will read all of libs linked to nmap and its dependencias and it will save all of them in a folder called cde-package/(in the same directory that you are).
该程序将读取链接到 nmap 及其依赖项的所有库,并将所有库保存在名为cde-package/的文件夹中(在您所在的同一目录中)。
- Finally, you can compress the folder and deploy the portable binary in whatever system.
- 最后,您可以压缩文件夹并在任何系统中部署可移植二进制文件。
Remember, to launch the portable program you have to exec the binary located in cde-package/nmap.cde
请记住,要启动可移植程序,您必须执行位于cde-package/nmap.cde 中的二进制文件
Best regards
此致
回答by Ian Moote
Yeah, I know this is an 8 year-old question, but I was told that it was possible to statically link against a shared-object library and this was literally the top hit when I searched for more information about it.
是的,我知道这是一个 8 年前的问题,但有人告诉我可以静态链接到共享对象库,当我搜索有关它的更多信息时,这确实是最受欢迎的。
To actually demonstrate that statically linking a shared-object library is not possible with ld
(gcc
's linker) -- as opposed to just a bunch of people insisting that it's not possible -- use the following gcc
command:
要实际证明使用ld
(gcc
的链接器)静态链接共享对象库是不可能的——而不是一群人坚持认为这是不可能的——使用以下gcc
命令:
gcc -o executablename objectname.o -Wl,-Bstatic -l:libnamespec.so
(Of course you'll have to compile objectname.o
from sourcename.c
, and you should probably make up your own shared-object library as well. If you do, use -Wl,--library-path,.
so that ld can find your library in the local directory.)
(当然,您必须objectname.o
从编译sourcename.c
,并且您可能还应该构建自己的共享对象库。如果这样做,请使用-Wl,--library-path,.
ld 以便 ld 可以在本地目录中找到您的库。)
The actual error you receive is:
您收到的实际错误是:
/usr/bin/ld: attempted static link of dynamic object `libnamespec.so'
collect2: error: ld returned 1 exit status
Hope that helps.
希望有帮助。