tags:

views:

5337

answers:

4

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

+1  A: 

Take a look here, though I'm iffy given the paucity of information.

dirkgently
+1  A: 

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

Yossarian
+8  A: 

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.

arsane
+4  A: 

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

HMage