21

I am implementing a OpenSSL code and have already included required header files but still I am getting errors like *

undefined reference to SSL_library_init

I guess it's a linking error rather than a compilation error.

I am implementing it in Linux box using slickeditor.

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
samprat
  • 2,150
  • 8
  • 39
  • 73
  • And how are you invoking the linker? Are you instructing it to link against the OpenSSL library, such as with `-lcrypto` for gcc? – rlibby Apr 08 '11 at 10:21
  • nope. the machine I got already has openssl. when I do which openssl it shows be path like /usr/bin/openssl. So the next step I did was to include header files in my existing code and then in slickeditor property i i tried to include -llibeay32 and -lssleay32. But no joy – samprat Apr 08 '11 at 10:25
  • The OpenSSL library is called libcrypto. Link to it with with -lcrypto. I don't know anything about SlickEdit. Is it invoking the compiler and linker for you, is that done in a makefile, or do you do it on a command line? Somehow you need to tell the linker to link to libcrypto. For invoking gcc on the command line, this means adding the option `-lcrypto`. – rlibby Apr 08 '11 at 10:30
  • New udpate. I did try to include -lssl in slickeditor and still no joy – samprat Apr 08 '11 at 10:31
  • The slickeditor provides complier G++ and linker as well. I did try to add -lcrypto in it. Also I have noticed there is -lcrpt and not crypto there. Still no joy – samprat Apr 08 '11 at 10:34
  • 6
    Apologies, `SSL_library_init` is in libssl, so the link option would be `-lssl`. `ldd $(which openssl)` will show you how your openssl is linked and where those libraries are. If it still doesn't work, perhaps that directory is not on the path for the linker. You can add that path with `-Lpath`, such as `-L/lib/` – rlibby Apr 08 '11 at 10:42
  • I have copied both libraires from /usr/lib into directory of project lib and issued command -lssl and -lcrypto .. But still no joy – samprat Apr 08 '11 at 10:56
  • on typing command ld $(which openssl) I got following o/p – samprat Apr 08 '11 at 10:57
  • ' 'linux-gate.so.1 => (0x00110000) libssl.so.7 => /lib/libssl.so.7 (0x00411000) libgssapi_krb5.so.2 => /usr/lib/libgssapi_krb5.so.2 (0x00919000) libkrb5.so.3 => /usr/lib/libkrb5.so.3 (0x00877000) libcom_err.so.2 => /lib/libcom_err.so.2 (0x007ec000) – samprat Apr 08 '11 at 10:58
  • libk5crypto.so.3 => /usr/lib/libk5crypto.so.3 (0x004d2000) libresolv.so.2 => /lib/libresolv.so.2 (0x007d5000) libcrypto.so.7 => /lib/libcrypto.so.7 (0x03d94000) libdl.so.2 => /lib/libdl.so.2 (0x0038d000) – samprat Apr 08 '11 at 10:58
  • libz.so.1 => /lib/libz.so.1 (0x003cd000) libc.so.6 => /lib/libc.so.6 (0x001f7000) libkrb5support.so.0 => /usr/lib/libkrb5support.so.0 (0x004f9000) libkeyutils.so.1 => /lib/libkeyutils.so.1 (0x00872000) – samprat Apr 08 '11 at 10:59
  • So libssl is in /lib/. Try the linker options `-L/lib/ -lssl` (in that order). – rlibby Apr 08 '11 at 11:03
  • I cant able to issue via command prompt . But I will try to figure out how to use linker in slickeditor as I am also too new in slick editor – samprat Apr 08 '11 at 11:04
  • 1
    You should get a minimal test case working on the command line. `#include "whatever" \n int main(void) { SSL_library_init(blah, blah, blah); return 0; }` and then `g++ my_minimal_test_case.c++ -lssl`. If this works then you don't understand your editor/IDE. If it doesn't then you have some configuration issue. – rlibby Apr 08 '11 at 11:08
  • Yeah thats works . SO I guess there is prob with my editor. I will investigate in that. AND THANKS A LOT for help – samprat Apr 08 '11 at 11:51
  • Thats done. I have copied crypto.a and ssl.a into lib folder of project and added -lcrypto -lssl in make file and its done. – samprat Apr 08 '11 at 14:00

5 Answers5

31

Link against libssl and libcrypto. Your LDFLAGS and LDLIBS would be as follows. Order matters for LDLIBS:

LDFLAGS = -L/usr/local/ssl/lib
LDLIBS = -lssl -lcrypto

Don't worry about adding the "lib" in front of library name, or the "so" or "a" suffix. The linker will do it for you.

If you are building from the command line, then you would use the following. Again, order matters.

gcc foo.c -o foo.exe -L/usr/local/ssl/lib -lssl -lcrypto

If you are using the system's OpenSSL, then you can omit -L/usr/local/ssl/lib.

jww
  • 97,681
  • 90
  • 411
  • 885
  • 1
    According to comments on the question, `SSL_library_init` is in libssl, so shouldn't the correct order be `LDLIBS = -lcrypto -lssl` instead? – Craig Scott Mar 31 '16 at 06:48
  • 3
    @CraigScott - Sorry about the late reply. Order matters because its a single pass linker. When LD encounters unsatisfied link symbols, it notes them and looks for them in libraries that follow. If `libcrypto` was first, then `libssl` would have unsatisfied symbols like `BN_new` because `libcrypto` needs to follow `libssl` to satisfy the missing symbols. The other option is a two-pass linker. Sometimes you will also see something like `-la -lb -la` (`liba` followed by `libb` followed by `liba`). Its usually due to a circular reference lurking behind the scenes. – jww Jun 01 '16 at 12:55
  • 1
    "Order matters" was the key for me. Thanks. – Ray Jun 11 '19 at 11:35
12

For me this meant to install

 apt install libssl1.0-dev
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
7

These methods are deprecated in OpenSSL 1.1. You don't need to use it more. You can just remove it. More info in OpenSSL manual.

unDEFER
  • 129
  • 1
  • 4
0

ldd libssl.so -> libcrypto.so.1.1 => not found

sudo ln -s /usr/local/lib64/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1

libcrypto.so.1.1 => /lib64/libcrypto.so.1.1 (0x00007f17d46c7000)

Michael Popovich
  • 301
  • 1
  • 10
0

Simply add -lssl -lcrypto to your Makefile and it should work.

Example of Makefile:

foo: foo.o
g++ -std=c++17 -o foo foo.cpp -lcrypto -lssl