24

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.

16
  • And how are you invoking the linker? Are you instructing it to link against the OpenSSL library, such as with -lcrypto for gcc? Commented Apr 8, 2011 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 Commented Apr 8, 2011 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. Commented Apr 8, 2011 at 10:30
  • 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/ Commented Apr 8, 2011 at 10:42
  • 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. Commented Apr 8, 2011 at 11:08

5 Answers 5

35

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.

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

2 Comments

According to comments on the question, SSL_library_init is in libssl, so shouldn't the correct order be LDLIBS = -lcrypto -lssl instead?
@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.
12

For me this meant to install

 apt install libssl1.0-dev

2 Comments

You're a lifesaver. Was trying to compile AOSP on Ubuntu 18, and had to downgrade openssl 1.1 -> 1.0.1 , but had installed libssl-dev
High five to both of you. I had the same issue and the combination of these two comments made me realize it.
8

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.

Comments

1

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

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.