19

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?

14
  • @billz: gcc -static -o openssltest openssltest.c -ldl -lz -lcrypto gives same errors I posted Commented Aug 12, 2013 at 11:26
  • 2
    -ldl -lz should be after -lcrypto Commented Aug 12, 2013 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 Commented Aug 12, 2013 at 11:30
  • 3
    can you try -static-libgcc instead of -static ? Commented Aug 12, 2013 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 Commented Aug 12, 2013 at 11:51

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

it' works, Thanks! can you just update your answer, to mention that the order is relevant.
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".
It doesn't seem to work for me. I am using g++ if that makes a difference. Does the C++ compiler do things differently?
This answer is probably wrong. file <output> on the output binary of the compilation process clearly says the binary is "Dynamically" linked on my machine.
Why is this upvoted? it's wrong as far as I can see... this does not result in a statically linked openssl.
3

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

Comments

0

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

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

Comments

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.

5 Comments

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.
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.
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.
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).
Did not see "13.04 Ubuntu".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.