4

I want to compile my code including the OpenSSL library. For my purpose it is necessary to link the library statically.

If I was dynamically linking the script for compilation would look like g++ test.cpp -lcrypto -o test.

I tried to use the -static option for the compilation, but if i do so i get the following error:

/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_globallookup':
(.text+0x15): Nicht definierter Verweis auf `dlopen'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_globallookup':
(.text+0x2b): Nicht definierter Verweis auf `dlsym'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_globallookup':
(.text+0x35): Nicht definierter Verweis auf `dlclose'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_bind_func':
(.text+0x381): Nicht definierter Verweis auf `dlsym'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_bind_func':
(.text+0x460): Nicht definierter Verweis auf `dlerror'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_bind_var':
(.text+0x4e1): Nicht definierter Verweis auf `dlsym'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_bind_var':
(.text+0x5c0): Nicht definierter Verweis auf `dlerror'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_load':
(.text+0x630): Nicht definierter Verweis auf `dlopen'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_load':
(.text+0x6a0): Nicht definierter Verweis auf `dlclose'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_load':
(.text+0x6df): Nicht definierter Verweis auf `dlerror'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_pathbyaddr':
(.text+0x788): Nicht definierter Verweis auf `dladdr'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_pathbyaddr':
(.text+0x7d9): Nicht definierter Verweis auf `dlerror'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_unload':
(.text+0x834): Nicht definierter Verweis auf `dlclose'
collect2: error: ld returned 1 exit status

How can i solve this problem?

jww
  • 97,681
  • 90
  • 411
  • 885
Maximilian
  • 1,325
  • 2
  • 14
  • 35
  • @Sacx Actually these answers are pretty unsatisfying. If i follow the first answer, i do not get a statically linked binary (has the same size as the dynamically linked one). If i try to use the -static option instead of -static-libc, and I link -ldl additional to -lcrypto, i get the following warning: /usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): "In Funktion `dlfcn_globallookup': (.text+0x15): Warnung: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking" – Maximilian May 04 '15 at 10:55
  • Why is it necessary to link the -ldl, which seems to be the dynamic linker library – Maximilian May 04 '15 at 11:02
  • I did the equivalent (static compile tcpdump) about 8 months ago. It was a day-long slog of banging through different Google hits until I found the one that worked, and sorry no, I don't recall how I did it. All that said, I'm voting to move this to Stack Overflow, as this is a Programming question and not a security Question. – gowenfawr May 04 '15 at 12:12
  • the link of the first comment is the equivalent for Stack Overflow, @gowenfawr could you please put your solution there? – Maximilian May 04 '15 at 12:15
  • Again, I don't recall how I did it... My advice to you is to bang your head against the wall repeatedly and try different things until something works. Heck, that's how compiling packages worked when I was a kid (back before `configure`). – gowenfawr May 04 '15 at 12:16
  • Possible duplicate of [Cannot find libcrypto library error](http://stackoverflow.com/questions/10368671/cannot-find-libcrypto-library-error). `gcc` vs `g++` does not really matter in this case. – jww May 04 '15 at 17:03

2 Answers2

2
g++ test.cpp -lcrypto -o test

Try:

g++ test.cpp -o test -lcrypto -ldl

Order and placement of the library matter.


For my purpose it is necessary to link the library statically.

My bad. I missed this earlier. For this, its easiest to perform:

g++ test.cpp /usr/lib/x86_64-linux-gnu/libcrypto.a /usr/lib/x86_64-linux-gnu/libssl.a -o test -ldl

Some folks want to use -Bstatic, L and -l. But that's not portable (BSDs, OS X and iOS don't honor them).

An archive is just a collection of object files (*.o). So you can use the archive anywhere a object file is needed.

You can locate the libraries with:

$ lsb_release -r
Release:    14.04
$ find /usr/lib -iname libcrypto.a
/usr/lib/x86_64-linux-gnu/libcrypto.a

Also, test is a real program. Be sure to run it with ./test. I usually name it test.exe to avoid collisions.


what is the difference if i place the lib in front of the output file or after it?

The way to envision this is to convert this (with some hand waiving):

g++ test.cpp /usr/lib/x86_64-linux-gnu/libcrypto.a /usr/lib/x86_64-linux-gnu/libssl.a -o test -ldl

Into:

g++ test.o libcrypto.o libssl.o -o test -ldl

The location of the output file (-o test) is not relevant. I use it to separate object (on the left) and libraries (on the right).

jww
  • 97,681
  • 90
  • 411
  • 885
0

I'm not sure if I understand exactly what you're trying to do, but have you tried replacing -lcrypto -ldl with /path/to/libcrypto.a /path/to/libdl.a?

Juan Serrats
  • 1,358
  • 5
  • 24
  • 30
adam
  • 384
  • 2
  • 9