Including MQTT wrapper for mosquitto library on windows
-
Hi, I am making a widget application in qt creator 9.0.1
I have been stuck on this for a while now, I have previously had this library (https://github.com/KostiantynBushko/QMosqMqttClient) working on a linux computer so just tried to follow the same as I did before in the .pro file and what I put in my widget.cpp file.
I have included the .h and .cpp file in the project as it says to do in the github instructions and included the compiler flag in my .pro file.
And the mosquitto library is working when used by itself outside of qt
The issue is that when I try to compile I get this error
C:\Program Files\mosquitto\devel\mosquittopp.h:31: error: mosquitto.h: No such file or directory In file included from ..\Microscope_GUI\QMMqttClient.h:6, from ..\Microscope_GUI\widget.cpp:12: C:/Program Files/mosquitto/devel/mosquittopp.h:31:10: fatal error: mosquitto.h: No such file or directory 31 | #include <mosquitto.h> | ^~~~~~~~~~~~~
And the same error 2 more times for where it cant find the library.
The mosquitto.h and mosquittopp.h are both part of the mosquitto mqtt library and are in the same directory, locate on my computer at C:\Program Files\mosquitto\devel
The include error is saying that the mosquittopp.h file doesnt recognise mosquitto.h as a file even though they are in the same directory.
I have tried included full path names in my widget.cpp, .pro file, the mosquittopp.h file and the mosquitto.h file but nothing seems to work.
This is my .pro file
QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++17 QMAKE_CXXFLAGS += -lmosquitto INCLUDEPATH += C:/Program Files/mosquitto/devel LIBS += C:/Program Files/mosquitto/devel -lmosquittopp # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ QMMqttClient.cpp \ main.cpp \ widget.cpp \ HEADERS += \ QMMqttClient.h \ widget.h FORMS += \ widget.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
This is my widget.cpp file
#include "widget.h" #include "ui_widget.h" #include <QPalette> #include <QDebug> #include <QTimer> #include <QString> //-----for qt qrapper for mosquitto----- #include <QCoreApplication> #include <QTime> #include "QMMqttClient.h" //-------------------------------------- Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); } Widget::~Widget() { delete ui; }
All other files are unaltered from the library or are automatically generated when you make a widget application.
Any help on what I could be doing wrong would be great as I am quite stuck on this.
-
@Dean21 said in Including MQTT wrapper for mosquitto library on windows:
INCLUDEPATH += C:/Program Files/mosquitto/devel
Are the header files directly inside this folder or in a subfolder?
-
@jsulm The path contains spaces and is not properly quoted.
-
-
@Dean21 i am thinking this is because you forgot to add DEPENDPATH
source: https://doc.qt.io/qt-6/qmake-variable-reference.html#dependpath
Specifies a list of directories for qmake to scan, to resolve dependencies. This variable is used when qmake crawls through the header files that you #include in your source code.
when using 3rd party libraries, this method is much more easy and less error prone. https://doc.qt.io/qtcreator/creator-project-qmake-libraries.html
Qt modifies your qmake file for you.
-
@Dean21 I think on Windows you need to link differently (see https://doc.qt.io/qt-6/qmake-variable-reference.html#libs):
unix:LIBS += -L/usr/local/lib -lmath win32:LIBS += c:/mylibs/math.lib
So, you need to use a path to the *.lib file of the library.
"the kit used when I made this project says it is Desktop Qt 6.4.1 MinGW 64-bit for the compiler" - the question is - what compiler was used to build that library? If MSVC was used it is not going to work. You need a MinGW build of the lib.
-
@starkm42 Thanks for the reply, that seemed to work to remove a lot of the linking errors but not I get this one error I havent seen before
:-1: error: No rule to make target 'C:/Users/deanm/Desktop/Uni_Year_3/3rd_year_project/Microscope_GUI_V2/Microscope_GUI/../../../../../../../Program Files/mosquitto/devel/libmosquittoppd.a', needed by 'debug/Microscope_GUI.exe'. Stop.
-
@jsulm This is now my .pro file.
QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++17 #QMAKE_CXXFLAGS += -lmosquitto #INCLUDEPATH += "C:/Program Files/mosquitto/devel" #LIBS += -L"C:/Program Files/mosquitto/devel" -lmosquittopp # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ QMMqttClient.cpp \ main.cpp \ widget.cpp \ HEADERS += \ QMMqttClient.h \ widget.h FORMS += \ widget.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target win32:CONFIG(release, debug|release): LIBS += -L$$PWD/'../../../../../../../Program Files/mosquitto/devel/' -lmosquittopp else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/'../../../../../../../Program Files/mosquitto/devel/' -lmosquittoppd else:unix: LIBS += -L$$PWD/'../../../../../../../Program Files/mosquitto/devel/' -lmosquittopp INCLUDEPATH += $$PWD/'../../../../../../../Program Files/mosquitto/devel' DEPENDPATH += $$PWD/'../../../../../../../Program Files/mosquitto/devel' win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/'../../../../../../../Program Files/mosquitto/devel/libmosquittopp.a' else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/'../../../../../../../Program Files/mosquitto/devel/libmosquittoppd.a' else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/'../../../../../../../Program Files/mosquitto/devel/mosquittopp.lib' else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/'../../../../../../../Program Files/mosquitto/devel/mosquittoppd.lib' else:unix: PRE_TARGETDEPS += $$PWD/'../../../../../../../Program Files/mosquitto/devel/libmosquittopp.a'
-
@Dean21 said in Including MQTT wrapper for mosquitto library on windows:
else:unix: PRE_TARGETDEPS += $$PWD/'../../../../../../../Program Files/mosquitto/devel/libmosquittopp.a'
Why do you have this?
Your LIBS for Windows are still wrong. -
@jsulm Sorry forgot to untick linux and mac boxes, here is the .pro file, it still has the same error though.
QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++17 QMAKE_CXXFLAGS += -lmosquitto INCLUDEPATH += "C:/Program Files/mosquitto/devel" LIBS += -L"C:/Program Files/mosquitto/devel" -lmosquittopp # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ QMMqttClient.cpp \ main.cpp \ widget.cpp \ HEADERS += \ QMMqttClient.h \ widget.h FORMS += \ widget.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target win32:CONFIG(release, debug|release): LIBS += -L$$PWD/'../../../../../../../Program Files/mosquitto/devel/' -lmosquittopp else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/'../../../../../../../Program Files/mosquitto/devel/' -lmosquittoppd INCLUDEPATH += $$PWD/'../../../../../../../Program Files/mosquitto/devel' DEPENDPATH += $$PWD/'../../../../../../../Program Files/mosquitto/devel' win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/'../../../../../../../Program Files/mosquitto/devel/libmosquittopp.a' else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/'../../../../../../../Program Files/mosquitto/devel/libmosquittoppd.a' else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/'../../../../../../../Program Files/mosquitto/devel/mosquittopp.lib' else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/'../../../../../../../Program Files/mosquitto/devel/mosquittoppd.lib'
Error: :-1: error: No rule to make target 'C:/Users/deanm/Desktop/Uni_Year_3/3rd_year_project/Microscope_GUI_V2/Microscope_GUI/../../../../../../../Program Files/mosquitto/devel/libmosquittoppd.a', needed by 'debug/Microscope_GUI.exe'. Stop.
-
@Dean21 said in Including MQTT wrapper for mosquitto library on windows:
QMAKE_CXXFLAGS += -lmosquitto
Why?
And again: your LIBS for Windows are wrong!
I also don't know why you have static versions if you want to link dynamically:
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/'../../../../../../../Program Files/mosquitto/devel/libmosquittoppd.a'
Remove alls these *.a stuff.
-
@jsulm My bad, this was done with dynamic and this is what was generated in the .pro file now.
QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++17 QMAKE_CXXFLAGS += -lmosquitto INCLUDEPATH += "C:/Program Files/mosquitto/devel" LIBS += -L"C:/Program Files/mosquitto/devel" -lmosquittopp # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ QMMqttClient.cpp \ main.cpp \ widget.cpp \ HEADERS += \ QMMqttClient.h \ widget.h FORMS += \ widget.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target win32:CONFIG(release, debug|release): LIBS += -L$$PWD/'../../../../../../../Program Files/mosquitto/devel/' -lmosquittopp else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/'../../../../../../../Program Files/mosquitto/devel/' -lmosquittoppd INCLUDEPATH += $$PWD/'../../../../../../../Program Files/mosquitto/devel' DEPENDPATH += $$PWD/'../../../../../../../Program Files/mosquitto/devel'
This now gives these errors:
:-1: error: cannot find -lmosquittoppd
:-1: error: cannot find -lmosquittod
:-1: error: collect2.exe: error: ld returned 1 exit status -
@Dean21 wait are you using the same compiled library that you used for linux on windows without recompiling or getting new binaries compiled for windows ?
@Dean21 said in Including MQTT wrapper for mosquitto library on windows:
@jsulm I am not sure what compiler was used to compile the library, but as I have had this working in Qt creator before on linux with the same library I assumed it would work fine on windows