openssl is a common used third-party library. I imported the library in my Mingw project by adding the following lines in the .pro file:
INCLUDEPATH += c:\Qt\Qt5.12.1\Tools\mingw730_64\opt\include
LIBS += -L c:\Qt\Qt5.12.1\Tools\mingw730_64\opt\lib -lcrypto
Note that you can use / or \ in the path, which does not matter. This also applies to MSVC. But when I switch to the MSVC build kit, something bad happens in the building:
:-1: error: LNK1146: no argument specified with option ‘/LIBPATH:’
This is caused by the space between -L and the lib path. Get rid of the space, run qmake, then compile again, I get another error:
:-1: error: LNK1104: cannot open file ‘crypto.lib’
It seems g++ in Mingw will expand the string immediately after the -l from crypto to libcrypto.a, then find the lib. But MSVC expands crypto to crypto.lib and there is no such file in c:\Qt\Qt5.12.1\Tools\mingw730_64\opt\lib. You can download and install Win32 OpenSSL, which will have a libcrypto.lib in its installation directory. And you should change the .pro file to:
INCLUDEPATH += c:\Qt\Qt5.12.1\Tools\mingw730_64\opt\include
LIBS += -Lc:\Qt\Qt5.12.1\Tools\mingw730_64\opt\lib -llibcrypto
libcrypto would be expanded to libcrypto.lib. MSVC does not add the prefix lib and the suffix .lib msvc compiler appends is a fixed one so if you change the LIBS to:
LIBS += -Lc:\Qt\Qt5.12.1\Tools\mingw730_64\opt\lib -llibcrypto.a
libcrypto.a will be expanded to libcrypto.a.lib which does not exist in the mingw version of openssl, either.
If you do not want to install Win32 OpenSSL, how do you link against the Mingw32 OpenSSL lib that is accompanied by the qt installation? I notice this file: c:\Qt\Qt5.12.1\Tools\mingw730_64\opt\lib\libcrypto.dll.a, which can be linked to our program. Since the option -l introduces a fixed .lib suffix, we cannot use it to link against this file. Instead, we can use this one:
INCLUDEPATH += c:\Qt\Qt5.12.1\Tools\mingw730_64\opt\include
LIBS += c:\Qt\Qt5.12.1\Tools\mingw730_64\opt\lib\libcrypto.dll.a
No -L and -l. Simply put the full path and file name of the lib in the LIBS.
Now you successfully build your program that uses OpenSSL. But before running the program, you should tell Windows where to load the openssl dll, otherwise your program won’t start. The name of the dll we links against the mingw openssl is libeay32.dll, which resides in c:\Qt\Qt5.12.1\Tools\mingw730_64\opt\bin. You should add this path to the environment variable PATH.