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.
165 Answers
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
.
For me this meant to install
apt install libssl1.0-dev
2These 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.
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
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)
ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJobG5xY2eFdXvUp5uenpmjsqV50Z6dnqqVo7CmedOoZKyrnGK5qq7RmqmyZZmjtrV5wKebZqujoXqtu8CdZJ6qoqS%2Fbr%2FTq6Cnn6M%3D