简体   繁体   中英

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.

  • libevent.a
  • libevent.dll
  • libevent.dll.a
  • libevent_core.a
  • libevent_core.dll
  • libevent_core.dll.a
  • libevent_extra.a
  • libevent_extra.dll
  • libevent_extra.dll.a
  • libhiredis.dll
  • libhiredis.dll.a

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

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.

我曾经在环境变量中设置了64位版本的MinGW,但是忘记了当我改成32位MinGW路径时,编译成功,可以使用动态库

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM