5

Possible Duplicate:
Can I mix static and shared-object libraries when linking?

I want to compile my app, linking statically only boost_system library. Other(glibc and etc) should be linked dynamically. How can i do it?

My command to compile dynamically:

g++  -o newserver  server.cpp ... -lboost_system -std=c++0x

Command to compile statically:

g++ -static  -o newserver  server.cpp ... -pthread -lboost_system -std=c++0x

But this command link statically all! And app weights 2mb more!

Can you advice me what command to compile statically only boost lib?

Thanks!

Community
  • 1
  • 1
Breakdown
  • 1,035
  • 4
  • 16
  • 26

2 Answers2

8

Replace -lboost_system with -Wl,-Bstatic -lboost_system -Wl,-Bdynamic. The -Wl option sends the thing after it to the linker, in the order it appears on the command-line.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Don't `-Bstatic` and `-Bdynamic` do the same thing? – James Kanze Jan 30 '13 at 13:45
  • Not sure about -Bstatic - I think that isn't recognised by the compiler, so passed to linker, which would work. But officially, you use -Wl for "linker commands". – Mats Petersson Jan 30 '13 at 13:47
  • @MatsPetersson thanks for your answer, but i have an error `/usr/bin/ld: invalid BFD target 'static'`. How to fix that? – Breakdown Jan 30 '13 at 13:49
  • @MatsPetersson I thought the difference was that `-Wl,...` passed the command to the linker directly, so you had to know the commands of whatever linker your system used, but `-Bstatic` passed whatever command was appropriate for this to the linker actually being used. (Under Solaris, for example, it is usual to use the system linker, rather than the GNU linker.) – James Kanze Jan 30 '13 at 15:57
  • @JamesKanze I know it's quite a while since you commented here, but `-Bstatic` and `-Bdynamic` do not do the same thing. '`-Bstatic` tells ld to *not* link with shared libraries (on systems that support this, i.e. systems that support shared libraries). Whereas `-Bdynamic` instructs ld to link with shared libraries (which is the default on system that support them) – Andrew Falanga Mar 06 '15 at 20:03
2

There are two solutions. You can specify -Bstatic and -Bdynamic in the command line, each affects all of the libraries which follow it. Or you can arrange it that the static versions of the libraries which you want to be linked statically are present in a directory which is searched before the directory which contains the dynamic version. This allows you to make some sort of global decision: you create the directory once, and all users you do a -L for it before the -L general will use the static versions.

In practice, I can't think of a case where you'ld want to link the Boost libraries other than statically, so the simplest solution might just be to remove the .so files. The only time g++ will make a decision (and take into account the -Bstatic and -Bdynamic) is if it finds both in the same directory. It searches the directories in the given order, and when it finds a directory which has either the static or the dynamic version of the library, it stops. And if only one version is present, it uses that one, regardless.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • "In practice, I can't think of a case where you'ld want to link the Boost libraries other than statically" what if you have several applications all running on the same target device and binary size is a constraint? You'll want to use shared libraries to reduce the overall size of your application. – ClydeTheGhost Dec 28 '17 at 19:55