Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Problem Linking a Qt Gui with a c++ project



  • Hello everyone !
    My teacher gave me a c++ code (zcl: Zigbee Cluster Library), an asked me to make a user interface for this code.
    -1- The code given can only be compiled with the "mvs nativex64 command prompt", and it's not a structured project, only a .cpp file using a header and a library + a .dll
    -2- After compiling, there is a .exe file
    And here is my problem:
    How to link my Qt user interface with the code given ?
    Should i copy/paste the c++ code into the Qt project ?! And if so, how to compile it to make it run ?! (because i have tried compiling it with the same "command line", but without success.. (maybe i have to set the "environment variable" for Qt) ?!
    Or should i use the ".exe" from my Qt Gui ?! And if so, how to do it since i want to link "features" from the .exe file to the user interface ?!
    I'm open to any other suggestions, assuming you have understood my messy explanation.



  • @Mohamed-Fouzi said in Problem Linking a Qt Gui with a c++ project:

    only a .cpp file using a header

    What do those files contain?

    a library + a .dll

    a dll is a library (dll = dinamically linked library) so what do you mean?


  • Moderators

    @Mohamed-Fouzi

    What I think you're saying is, you have a precompiled c/c++ libary and a header file to access the functions in your libary?

    And now you look for a way to access those functions from QT?

    if that is the case, I can give you a short example from a project of mine, where I did exactly that with libusb

    In you Project-File,

    you'll have to add the lib-header file, include the path to the folder of your libary and add it to your LIBS:

    SOURCES += main.cpp\
            mainwindow.cpp
    
    HEADERS  += mainwindow.h \
        libusb.h
    
    FORMS    += mainwindow.ui
    
    INCLUDEPATH += C:/Software/libUSB/include
    
    LIBS += -LC:/Software/libUSB/MinGW32/dll\
            libusb-1.0
    

    in your UI-Class mainwindow in this example you include the *h-file:

    #include <QMainWindow>
    #include <QIODevice>
    ....
    #include "libusb.h"
    

    Now you can simply access the functions of your libary by calling them . e.g.:

    r = libusb_init(&ctx); //initialize a library session
    ...
    libusb_exit(ctx); //close the session
    


  • @J-Hilk
    Thank you for your answer,. I think that you get exactly what i want to do. I will try that immediatly, even if i have some questions to ask.
    For example :
    -1- The "LIBS += -LC:/Software/libUSB/MinGW32/dll
    libusb-1.0"
    Correct me if i'm wrong. You added the library path, (C:/Software/libUSB/MinGW32/dll) and the name of the library (libusb-1.0), is that right ?!
    -2- The "-L" before the path of the library isn't a mistake, just to be sure ?!

    And last but not least, when i tried to compile the c/c++ code, i was able to do it only with the vs2015 x64 compiler, when i tried with "gcc", it didn't worked.. So if i will try to compile the Qt project with those files, would there be a problem ?!
    Thank you.

    @VRonin : The .cpp files contains the logic of the program and the .header contains the interface.
    For the (dll = dynamic library), i know that. But, i think i didn't explain myself well.
    My teacher gave me a code already precompiled, but also gave me these three folders in case i wanted to modify the code and compile it again.

    folder1 : Windows-gcc_64bits, contains:

    TestZCL.exe
    zcl.a
    zcl.def
    zcl.dll

    folder2 : Windows-msvc_64bits, contains:

    licence.lic
    TestZCL.exe
    zcl.dll
    zcl.lib

    folder3 : Windows-msvc2013_64bits, contains:

    TestZCL.exe
    zcl.dll
    zcl.lib

    I hope you understand me better now.





  • @VRonin, @J-Hilk : i would like to thank both of you, it was helpful. With your help, i managed to get the code working in Qt.

    However, i have one last question if you permit me.

    When i was frustrated (:p), i tried to get the code working in Visual Studio, i struggled a lot.

    In the beginning stages: The c/c++ code (given by the teacher) worked only with the prompt command, but, hopefully, putting the right .dll in the right folders (and changing the plateform configuration from Win32 to x64) helped me to run the code from VS directly. Unfortunately, the problem that i got start from here:

    -1- When i execute the c/c++ code "alone", it works like a charm. (adding the necessary libraries)
    -2- When i execute a Qt code form Visual Studio (using the add-on), it works also perfectly.

    But, when i "mix" the codes into one and try to execute, i get a lot of "Error LNK2001_ Unresolved External" and "Error LNK2019_Unresolved External", both refering to "Qt functions".

    I hope i was clear enough. Thank you again.



  • @Mohamed-Fouzi said in Problem Linking a Qt Gui with a c++ project:

    But, when i "mix" the codes into one and try to execute, i get a lot of "Error LNK2001_ Unresolved External" and "Error LNK2019_Unresolved External", both refering to "Qt functions".

    That's because the VSaddin hides the linking to Qt libraries and the calls to uic/moc/rcc if you start with a non-Qt-addin project you have to do all those things manually



  • @VRonin said in Problem Linking a Qt Gui with a c++ project:

    @Mohamed-Fouzi said in Problem Linking a Qt Gui with a c++ project:

    But, when i "mix" the codes into one and try to execute, i get a lot of "Error LNK2001_ Unresolved External" and "Error LNK2019_Unresolved External", both refering to "Qt functions".

    That's because the VSaddin hides the linking to Qt libraries and the calls to uic/moc/rcc if you start with a non-Qt-addin project you have to do all those things manually

    The fact is, i'm getting those errors even if i've started a Qt-addin project, in which i've added the c/c++ code.

    It seems like i have a new problem now, too.
    You see, in this project, i use a Ubee device (Usb Zigbee Controller). And, when i execute the code without it plugged in, the code works (Ubee not found is displayed).
    But when i plugged in the Ubee, and try to run the code, it is found, but it crashes immediatly.. Could it be related to the .dll files ?!


  • Lifetime Qt Champion

    @Mohamed-Fouzi If it crashes you should debug to see where it is crashing.


  • Moderators

    Hi, let me answer a few questions you had:

    @Mohamed-Fouzi said in Problem Linking a Qt Gui with a c++ project:

    -1- The "LIBS += -LC:/Software/libUSB/MinGW32/dll
    libusb-1.0"
    Correct me if i'm wrong. You added the library path, (C:/Software/libUSB/MinGW32/dll) and the name of the library (libusb-1.0), is that right ?!

    in a nutshell, yes. I like to write the actuall libary in a seperated line.Makes it easier, in case I have more to add later on, that have the same basic path.

    -2- The "-L" before the path of the library isn't a mistake, just to be sure ?!

    No it's not, its a flag for qmake to mark the following as a libary path.

    And last but not least, when i tried to compile the c/c++ code, i was able to do it only with the vs2015 x64 compiler, when i tried with "gcc", it didn't worked.. So if i will try to compile the Qt project with those files, would there be a problem ?!
    Thank you.

    That really depends on what was used to create the libary. If its a pure c-libary you usually don't have to worry at all.
    But if it is a c++ one you'll have to use the compiler for your project, that was also used for the libary. I believe that is way your teacher? gave you the source code of those. To not be limited to MSVC. Just recompile the lib using Qt and mingw and your good to go.



  • What do you mean by recompile the .lib ?! Note that for gcc. i have a "zcl.a" library (a static library).

    I just compiled the same code with MinGW, and i get errors of type : "undefined reference" to all the functions of the library. Is there a solution ?!


  • Lifetime Qt Champion

    @Mohamed-Fouzi I guess this library is built using another compiler (not MinGW), right? This cannot work: you cannot mix binaries built with different compilers. If you want to use MinGW then you need to compile this library with MinGW as well - this is what @J-Hilk means.



  • Ok, i would like to thank both of you.
    Guess i'll need to see how to compile a library with MinGW, then.

    i'll keep you informed if there is any improvement.

    EDIT: i finally managed to get the whole thing working. So, basically, since compiling from the command line, gave me no errors at all (even if the ZigBee device was pluged on), i asked myself if there would be some change to do in the "building and compiling" parts of Visual Studio.
    So all i did, was to change the "plateform to x64" and the configuration to "release" version.
    It seems the last change, "the release" was the most important, because when i did it, it worked perfectly.

    So, i jumped to Qt and did the same (even i'm i'm sure i did it before, but didn't worked). So, with the kit using MSVC2015, and choose a release version, and tada, it worked also in Qt, there were no crashes at all.


Log in to reply