Undefined reference to SSL_library_init and SSL_load_error_strings
Solution 1
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
.
Solution 2
For me this meant to install
apt install libssl1.0-dev
Solution 3
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.
Related videos on Youtube
samprat
Updated on April 16, 2020Comments
-
samprat about 4 years
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.
-
rlibby about 13 yearsApologies,
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 about 13 yearsYou 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 theng++ 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.
-
-
Craig Scott about 8 yearsAccording to comments on the question,
SSL_library_init
is in libssl, so shouldn't the correct order beLDLIBS = -lcrypto -lssl
instead? -
jww almost 8 years@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, thenlibssl
would have unsatisfied symbols likeBN_new
becauselibcrypto
needs to followlibssl
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 bylibb
followed byliba
). Its usually due to a circular reference lurking behind the scenes. -
Ray almost 5 years"Order matters" was the key for me. Thanks.
-
Rick Sanchez over 4 yearsYou'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
-
Brent Writes Code about 4 yearsHigh five to both of you. I had the same issue and the combination of these two comments made me realize it.