18

I wrote simple apps with Openssl in C and C++. I compile them like this:

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

And its all ok but only, when you have Openssl installed.

I would like to compile it that I can run them on OS where theres no Openssl installend (linux-like OS). 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 have those 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 make this? I remember that a while ago I did this but now, erm, forgot how to do it. Im on Ubuntu 13.04 x64 - can it be an issue?

jww
  • 97,681
  • 90
  • 411
  • 885
yak
  • 3,770
  • 19
  • 60
  • 111
  • @billz: `gcc -static -o openssltest openssltest.c -ldl -lz -lcrypto` gives same errors I posted – yak Aug 12 '13 at 11:26
  • 2
    -ldl -lz should be after -lcrypto – billz Aug 12 '13 at 11:27
  • 1
    @billz: `gcc -o openssltest openssltest.c -lcrypto -ldl -lz -static` gives: `/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function dlfcn_globallookup: (.text+0x19): warning: Using dlopen in statically linked applications requires at runtime the shared libraries from the glibc version used for linking` but I dont use any OS-secific libs in my code – yak Aug 12 '13 at 11:30
  • 3
    can you try -static-libgcc instead of -static ? – rakib_ Aug 12 '13 at 11:46
  • @rakib: sure I can ;) Did this: `gcc -o openssltest openssltest.c -lcrypto -static-libgcc` with no errors but Im not sure if it gave a real static app, because its size is to samll for a static application :( the size is the same like I link it without `-static` flag – yak Aug 12 '13 at 11:51
  • @yak not sure about the size, it supposed to be bigger. but from the warning you've last posted (in answer to billz) indicates that you need libgcc to be statically linked. – rakib_ Aug 12 '13 at 12:00
  • @rakib interesting ... How can I check if its really statisticly linked app? The size isnt bigger, checked it, its for sure. – yak Aug 12 '13 at 12:01
  • @yak - use file command, file programname , it'll show how it was linked. – rakib_ Aug 12 '13 at 12:03
  • @rakib: I guess I was right, unfortunatly, please take a look: `openssltest: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x2267689c737410d31bf179f500eb36d04cf475c4, not stripped` – yak Aug 12 '13 at 12:05
  • @yak - yes it was dynamically linked. Well, i'm not sure how you went on to tried building statically. If im not wrong all the necessary libraries also needs to be statically build and then you need to pass a linking reference to them while building your app. – rakib_ Aug 12 '13 at 12:08
  • @yak take a look at this tutorial http://belski.net/archives/13-Building-static-executables-on-Linux.html – rakib_ Aug 12 '13 at 12:10
  • @rakib: ok, will try it – yak Aug 13 '13 at 12:00
  • why has the author skipped `libssl`? Or does `libcrypto` takes care of that? – AjB Nov 04 '19 at 05:57
  • So as a security professional, I would say that linking to openssl statically is a bad idea, unless you are prepared to support your app with every openssl update. Openssl is a frequently patched item due to the fact it is central to computer security. May want to try to understand how to make your app expect openssl be installed in all target platforms and let the OS do what it is supposed to do and keep that library up to date with regular updates. – Steve Owens Nov 16 '20 at 17:20

4 Answers4

5

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

jww
  • 97,681
  • 90
  • 411
  • 885
user4035357
  • 59
  • 1
  • 2
  • 1
    it' works, Thanks! can you just update your answer, to mention that the order is relevant. – Zskdan Feb 05 '16 at 14:47
  • This way it compiles and runs with an increase of the executable by 2MB, but there's also this warning: **"Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking"**. – Hack06 Jun 11 '18 at 21:35
  • It doesn't seem to work for me. I am using g++ if that makes a difference. Does the C++ compiler do things differently? – AjB Nov 12 '19 at 07:43
  • This answer is probably wrong. `file ` on the output binary of the compilation process clearly says the binary is "Dynamically" linked on my machine. – C-- Dec 11 '21 at 18:42
2

You need to be aware that libraries need to availiable as statically linked *.a files. If they are not, then compilation will either fail or you end up with a dynamically linked executable.

If this gets too much PITA (all the libraries dependencies need to be statically compiled to, and their deps too and so on) use something like buildroot

drahnr
  • 6,782
  • 5
  • 48
  • 75
0

A hotfix: try to link in libdl statically, too.

If this not works, IMHO your libcrypto.a is bad compiled.

peterh
  • 11,875
  • 18
  • 85
  • 108
0

I had the same problem as you. Here's the command that solved it 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 still works.

schulwitz
  • 1,651
  • 1
  • 13
  • 19
  • As long as you have the libgcc/libc libs readily availiable on your target device, sure it will work - but if not you are doomed. So in fact it means the binary is not entirely static – also this not a solution. – drahnr Jan 21 '14 at 08:28
  • You say "sure it will work" and then end with "this is not a solution". In my book, if something works, it's a solution. – schulwitz Jan 21 '14 at 09:05
  • Because you preclude something that is not obvious. Not every system (especially embedded ones) do not necessarily include libgcc. Note that the "sure it will work" is part of a "if" statement. – drahnr Jan 21 '14 at 09:26
  • And that "if" statement holds true for the vast majority of linux systems, including by default, Ubuntu 13.04 (the OS specified by the person who asked the question). – schulwitz Jan 22 '14 at 18:13
  • Did not see "13.04 Ubuntu". – drahnr Jan 22 '14 at 18:14