VanChun
VanChun

Reputation: 43

How to link a dynamic library in QtCreator use qmake and MinGW32?

I am trying to use hiredis and libevent in my project, I downloaded their source code and compiled them with CmakeGUI and MinGW32. Then i got the header files and those library files.

Then I wrote some test programs to use these libraries, but could not link successfully The error looks like this:

error: undefined reference to `redisConnect'

here is my pro file and code.

#my pro file
#hiredis
INCLUDEPATH += $$PWD/include/hiredis
LIBS += -L$$PWD/lib/ -llibhiredis.dll

#event2
INCLUDEPATH += $$PWD/include/libevent
LIBS += -L$$PWD/lib/ -llibevent.dll
LIBS += -L$$PWD/lib/ -llibevent_core.dll
LIBS += -L$$PWD/lib/ -llibevent_extra.dll
#include "hiredis.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow) {
    ui->setupUi(this);
    redisContext *asd = redisConnect("127.0.0.1", 6379);
}

This is the compile output

g++ -Wl,-s -Wl,-subsystem,windows -mthreads -o release\Test.exe release/main.o
release/mainwindow.o release/moc_mainwindow.o 
-LC:\Users\VC\Desktop\Test\lib C:\Users\VC\Desktop\Test\lib\libhiredis.dll.a 
C:\Users\VC\Desktop\Test\lib\libevent.dll.a 
C:\Users\VC\Desktop\Test\lib\libevent_core.dll.a 
C:\Users\VC\Desktop\Test\lib\libevent_extra.dll.a 
D:\Work\env\cpp\Qt\5.12.9\mingw73_32\lib\libQt5Widgets.a 
D:\Work\env\cpp\Qt\5.12.9\mingw73_32\lib\libQt5Gui.a 
D:\Work\env\cpp\Qt\5.12.9\mingw73_32\lib\libQt5Core.a 
-lmingw32 D:\Work\env\cpp\Qt\5.12.9\mingw73_32\lib\libqtmain.a
-LC:\openssl\lib -LC:\Utils\my_sql\mysql-5.6.11-win32\lib 
-LC:\Utils\postgresql\pgsql\lib -lshell32 

release/mainwindow.o:mainwindow.cpp:(.text+0x2c9): undefined reference to `redisConnect'

I tried two different libraries(libevent and hiredis) and got similar results, what am I doing wrong?

thank you

Upvotes: 2

Views: 2167

Answers (2)

VanChun
VanChun

Reputation: 43

I once set a 64-bit version of MinGW in environment variables, but I forgot When I changed it to the 32-bit MinGW path, the compilation was successful and the dynamic library can be used

Upvotes: 0

Tom Rivoire
Tom Rivoire

Reputation: 651

Try to remove the .dll, just give the name of the lib, I mean include the lib like this :

INCLUDEPATH += $$PWD/include/hiredis
INCLUDEPATH += -L$$PWD/lib
LIBS += -L$$PWD/lib/ -llibhiredis  

Then clean the project and run qmake.

Upvotes: 2

Related Questions