How to use static binding with OpenSSL in C / C ++

I have written simple applications with Openssl in C and C ++. I compile them as follows:

gcc openssltest.c -o openssltest -lcrypto g++ openssltest.cpp -o openssltest -lcrypto 

And all this is normal, but only when you have Openssl installed.

I would like to compile it so that I can run them on an OS where Openssl installend (Linux-like OS) is not installed. I tried this:

 gcc -c openssltest.c -lcrypto -static gcc openssltest.o -o openssltest -lcrypto -static 

and same for C ++:

 g++ -c openssltest.cpp -lcrypto -static g++ openssltest.o -o openssltest -lcrypto -static 

But they have these errors:

 /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup': (.text+0x19): undefined reference to `dlopen' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup': (.text+0x2c): undefined reference to `dlsym' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup': (.text+0x37): undefined reference to `dlclose' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_bind_func': (.text+0x354): undefined reference to `dlsym' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_bind_func': (.text+0x3fb): undefined reference to `dlerror' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_bind_var': (.text+0x474): undefined reference to `dlsym' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_bind_var': (.text+0x52e): undefined reference to `dlerror' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_load': (.text+0x5a2): undefined reference to `dlopen' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_load': (.text+0x60b): undefined reference to `dlclose' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_load': (.text+0x638): undefined reference to `dlerror' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_pathbyaddr': (.text+0x6cd): undefined reference to `dladdr' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_pathbyaddr': (.text+0x731): undefined reference to `dlerror' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_unload': (.text+0x78a): undefined reference to `dlclose' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_free': (.text+0x4d): undefined reference to `inflateEnd' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_free': (.text+0x6b): undefined reference to `deflateEnd' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_ctrl': (.text+0x284): undefined reference to `deflate' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_ctrl': (.text+0x342): undefined reference to `zError' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `zlib_stateful_expand_block': (.text+0x411): undefined reference to `inflate' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `zlib_stateful_compress_block': (.text+0x4ca): undefined reference to `deflate' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `zlib_stateful_finish': (.text+0x51f): undefined reference to `inflateEnd' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `zlib_stateful_finish': (.text+0x528): undefined reference to `deflateEnd' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `zlib_stateful_init': (.text+0x5d7): undefined reference to `inflateInit_' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `zlib_stateful_init': (.text+0x659): undefined reference to `deflateInit_' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_read': (.text+0x893): undefined reference to `inflate' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_read': (.text+0x90d): undefined reference to `zError' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_read': (.text+0x97c): undefined reference to `inflateInit_' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_write': (.text+0xa6f): undefined reference to `deflate' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_write': (.text+0xaec): undefined reference to `zError' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_write': (.text+0xb7e): undefined reference to `deflateInit_' collect2: error: ld returned 1 exit status 

How can i do this? I remember that I did it a while ago, but now, erm, I forgot how to do it. Im on Ubuntu 13.04 x64 - could this be the problem?

+12
c ++ c static linker static-linking
source share
4 answers

I had a similar problem when I was trying to statically compile a simple DES program using openssl lib. I used -lcrypto -lz -ldl -static-libgcc and this worked for me. No warnings or errors.

+4
source share

You need to know that libraries must be available as statically linked * .a files. If this is not the case, then compilation will either fail or end with a dynamically linked executable.

If this is too much PITA (all library dependencies should be statically compiled, as well as their fingerprints, etc.) use something like buildroot

+1
source share

Bugfix: try also setting the link in libdl.

If this does not work, IMHO your libcrypto.a is poorly compiled.

0
source share

I had the same problem as you. Here is the team that solved this for me:

 gcc yourfile.c -o yourfile -static -lcrypto -lz -ldl 

It generates this warning:

 /usr/lib/gcc/i686-linux-gnu/4.7/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup': (.text+0x1b): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking 

but the executable is still working.

-3
source share

All Articles