Static link of shared library function in gcc
如何在gcc中静态链接共享库函数?
Refer to:
http://www.linuxquestions.org/questions/linux-newbie-8/forcing-static-linking-of-shared-libraries-696714/
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.
Another possible approach is use statifier or Ermine. Both tools take as input dynamically linked executable and as output create self-contained executable with all shared libraries embedded.
If you want to link, say, libapplejuice statically, but not, say, liborangejuice , you can link like this:
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.
You'll have to link liborangejuice
statically alongside with libapplejuice
to get libapplejuice
static.
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).
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:
This generates main.o by just compiling:
gcc -c main.c
This links that object file with the corresponding static library and creates the executable (named "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
链接地址: http://www.djcxy.com/p/85764.html
上一篇: 直接告诉gcc静态链接一个库
下一篇: gcc中共享库函数的静态链接