Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you statically link Boost libraries only?

I've got a project which links against various common libraries, as well as Boost. Testing this on computers other than my own has proven to be difficult since various flavors of Linux come with different versions of Boost. I would prefer to avoid having to download and compile the same version of Boost on every machine.

Is there a way to link my program statically only with the Boost libraries and have everything else linked normally? I've tried linking everything statically (-static), but that causes other problems (namely lGL is not found). Is there an other potential way I could package only the necessary dynamic libraries with my program (say in the same folder as the executable) and distribute it that way?

Link error when trying to link everything statically:

g++ -static -o"acmserver"  ./src/acmserver.o ./src/airplane.o ./src/bullet.o ./src/control.o ./src/detail.o ./src/game.o ./src/gamelog.o ./src/gamelogitem.o ./src/guns.o ./src/map.o ./src/missile.o ./src/missilepod.o ./src/object.o ./src/server.o   -lboost_system -lboost_filesystem -lboost_thread -lboost_serialization -lboost_date_time -lpthread -lGLU -lGL
/usr/bin/ld: cannot find -lGL
collect2: ld returned 1 exit status
make: *** [acmserver] Error 1

EDIT (SOLUTION):

count0 mentioned exactly what I was looking for. In Eclipse I removed all the Boost libraries (e.g., boost_system) form Project -> Properties -> C/C++ Build -> Settings -> GCC C++ Linker -> Libraries -> Libraries (-l). Then I added the Boost .a files (e.g., /usr/lib/libboost_system.a) under Project -> Properties -> C/C++ Build -> Settings -> GCC C++ Linker -> Miscellaneous -> Other Objects. I also removed the "-static" from the linker flags. This produced an executable with all of the boost libraries linked statically instead of dynamically.

like image 717
JuiceboxHero Avatar asked May 04 '11 21:05

JuiceboxHero


People also ask

Can you statically link a shared library?

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.

What creates static libraries?

Static libraries are created using some type of archiving software, such as ar. ar takes one or more object files (that end in .o), zips them up, and generates an archive file (ends in . a) — This is our “static library”. Before using ar, we must first have some object files to give to it.


1 Answers

Use the boost archive files (.a files) instead of the shared lib files (.so aka. linking with -l). You are linking those boost libraries dynamically right now. Writing it out might help to ensure what is being linked statically and what dynamically.

This will look something like:

g++ -o"acmserver"  ./src/acmserver.o ... ./src/server.o \
  /usr/local/lib/libboost_system.a /usr/local/lib/boost_filesystem \
  ... -lGL ...

Depending on the gcc version or platform type you might also have to add the -static` flag.

like image 153
count0 Avatar answered Sep 28 '22 03:09

count0