Search code examples
c++qtopensslpkg-configlibs

How I can include OpenSSL in my Qt program on Linux?


In my Qt program I have to convert a string to a hash. I want to use OpenSSL on Debian. Little brief of my code:

#include <openssl/sha.h>
...

unsigned char data[] = "data to hash";
unsigned char hash[SHA512_DIGEST_LENGTH];
SHA512(data, sizeof(data) - 1, hash);

I have installed openssl and libssl-dev. And I also have changed my .pro document, looks like that:

TEMPLATE = app
QT = core gui

HEADERS += window.h
PKGCONFIG += openssl //found in another solution, but it does not work
LIBS += -L/usr/share/doc libssl-dev
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

SOURCES +=  main.cpp window.cpp

In LIBS I have tried all possible locations that I get with sudo dpkg -L libssl-dev . Does anyone what I am missing? It doesn't compile and I sure is about that.

Thanks.


Here is the error when linking:

/usr/lib/x86_64-linux-gnu/qt4/bin/qmake -o Makefile App.pro
g++ -m64 -Wl,-O1 -o App main.o window.o moc_window.o    -L/usr/lib/x86_64-linux-gnu -L/usr/share/doc/libssl-dev -libssl-dev -lQtGui -lQtCore -lpthread 
/usr/bin/ld: cannot find -libssl-dev
collect2: error: ld returned 1 exit status
Makefile:105: recipe for target 'App' failed
make: *** [App] Error 1

Solution

  • In general if the library is libsomelib.so you must import it using -lsomelib, and in your case assuming that you have installed it using the system's tools it is not necessary to pass it a path, just add the following since the library is libssl.so:

    LIBS += -lssl -lcrypto
    

    or you can also use pkg-config:

    PKGCONFIG += libssl
    

    where /usr/lib/pkgconfig/libssl.pc is:

    prefix=/usr
    exec_prefix=${prefix}
    libdir=${exec_prefix}/lib
    includedir=${prefix}/include
    
    Name: OpenSSL-libssl
    Description: Secure Sockets Layer and cryptography libraries
    Version: 1.1.0g
    Requires.private: libcrypto
    Libs: -L${libdir} -lssl
    Libs.private: -ldl 
    Cflags: -I${includedir}