Static link of shared library function in gcc
Asked Answered
P

6

166

How can I link a shared library function statically in gcc?

Phosphene answered 7/4, 2009 at 12:34 Comment(1)
What do you mean by statically linked? Do you want your executable to be distribuited without requiring the .so?Notional
E
130

Refer to:

http://www.linuxquestions.org/questions/linux-newbie-8/forcing-static-linking-of-shared-libraries-696714/

You need the static version of the library to link it.

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 a shared library (or dynamically link a static one).

The flag -static will force the linker to use static libraries (.a) instead of shared (.so) ones. But static libraries aren't always installed by default, so you may have to install the static library yourself.

Another possible approach is to use statifier or Ermine. Both tools take as input a dynamically linked executable and as output create a self-contained executable with all shared libraries embedded.

Emigrate answered 7/4, 2009 at 12:52 Comment(2)
What information does the static library have, so that it can be statically linked, that the dynamic library doesn't have?Wend
Bundling all together makes it like 20 times bigger.Monolingual
L
83

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).

Leggett answered 2/6, 2009 at 11:10 Comment(6)
Isn't there a way to tell gcc directly what to link statically, and not to bypass him and talk with the linker?Jurat
@ElazarLeibovich you can't get a combination of static and dynamic that way.Tooling
@EugeneBujak: The caveat does not apply on my system. Example: gcc -o main main.cc -Wl,-rpath=. -Wl,-Bdynamic -lB -Wl,-Bstatic -lA -Wl,-Bdynamic -L. libB uses libA, it linked and ldd does not show a reference to libA. The executable works fine. Tested with g++ 4.7.3.Remiss
An indirect (nested), static, dependency of a direct, dynamic, dependency does not itself become dynamically linked.Eloign
Consider the following: binA depends on libB.so which depends on libC.a As others have already stated, .so's are themselves executables, so when a shared object is linked, any static library dependents are processed by the linker much the same as if an executable was being linked: the only symbols pulled in from the .a static lib are those referenced (and unresolved) by the .so. This means that if binA references a symbol in libC.a, not referenced anywhere in libB.so, then even if binA links to libB.so, that symbol will be undefined (unless -Wl,--whole-archive is used when linking libB.so).Eloign
Can you clarify what -Wl and -B mean on their own?Snakebite
E
27

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.

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:

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.)

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.

Encyst answered 11/12, 2017 at 18:51 Comment(0)
O
21

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
Odontograph answered 6/9, 2011 at 15:8 Comment(0)
A
11

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

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

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).

  • 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

Best regards

Austenite answered 12/3, 2015 at 8:33 Comment(2)
While not exactly providing the answer to the question - its a notable solution to the problem.Ganja
The link seems to be dead now.Erdda
K
0

In gcc, this isn't supported. In fact, this isn't supported in any existing compiler/linker i'm aware of.

Ketchup answered 7/4, 2009 at 12:49 Comment(2)
Could you explain how static linking is not supported by any existing compiler?Lamarckian
@noloader, static linking of dynamic library?Ketchup

© 2022 - 2024 — McMap. All rights reserved.