Search code examples
c++linkerstatic-linking

How do I statically link external libraries into my executable?


I don't want to have the end use have to have the libraries installed, so, having the libraries packaged in my exec would be preferred.

this is the relevant line in the make file:

hPif : src/main.o src/fann_utils.o src/hashes.o src/Config.o
    g++ -o  hPif src/main.o src/fann_utils.o src/hashes.o src/Config.o -static -lfann -lboost -L/usr/local/lib 

I'm trying to link fann and boost, and I read somewhere (http://www.adp-gmbh.ch/cpp/gcc/create_lib.html) that using the -static flag allows that.

What am I doing wrong?


Solution

  • The -static flag is correct, but you need to make sure your libraries are static libraries without dependencies. If they are built as shared (or have shared dependencies), gcc will not link them statically (and/or you will still have library dependencies).

    You may need to rebuild your Boost libraries to achieve this.