2

I am having a piece of code (test.cpp) which depends on OpenSSL. I would like to create a static library with my piece of code, but it should really include already all dependencies. So that in the end I really only need to link against libtest.a file (also on other distros).

I have tried this

g++-7 -c -std=c++17 -static -L/usr/local/lib -lssl -lcrypto test.cpp -o test.o
ar crf libtest.a test.o
g++-5 main.cpp -std=c++11 libtest.a

but it still gives undefined references to OpenSSL stuff.

Don't judge me, my knowledge about compiling is equal 0, usually I let Qt handle this.

I would appreciate it if somebody could sketch how to accomplish it.

  • 1
    [Similar SO thread](https://stackoverflow.com/questions/33851045/gcc-a-static-library-with-undefined-symbols) – Mahesh Mar 04 '18 at 02:44
  • @Mahesh Thank you for the link! In the accepted answer it is written to use the `-static` flag for fully-static binaries, but I already use it. So it somehow has no effect. Guess I am doing something wrong :( . –  Mar 04 '18 at 02:58
  • @M.M Once more I have very little knowledge about compiling/linking. So I want to avoid the library flags at link step (hope this is line 3) I want to be them already inside libtest.a. So i can ship libtest.a (after line 2) to clients and they can use it without taking care about OpenSSL and other stuff. So I put the lib flags at line 1 (compile step) in hope they get statically included but seems like thats not working. How i can then include them inside libtest.a ? –  Mar 04 '18 at 03:41
  • @M.M Great thank you! Thats exactly what i need, found an usefull SO thread on how to do it too. Thank you once more ! –  Mar 04 '18 at 05:09

1 Answers1

2

A .a file is a collection of .o files and no more. There is no possibility of linking.

Perhaps you could make a .so file (shared library) which does allow things to be statically linked to it.

Note: If you statically link OpenSSL and the person using your library also uses OpenSSL then it means their binary will be bloated by having two copies of it (linkers are not smart enough to optimize this), so it may be a good idea to also publish a version of your library that doesn't statically link OpenSSL.

M.M
  • 138,810
  • 21
  • 208
  • 365