Issue linking .lib or .dll for usage in .cpp
-
Hi all,
So as I have been experiencing this error for a little under a week, I have searched this forum for the answer, and have came across many possible solutions but none that have worked for me yet. I am currently trying to connect a .lib or .dll file to my project. My understanding is that I only need one of them, so whichever allows me to use the functions provided would be okay at this point.
Some specs:
-
I am programming the application in C++
-
My Qt version is Qt 6.2.2
-
I am on a Windows PC
-
I am using Qt Creator
Currently, I am experiencing the error
No rule to make target C:/Users/Sorsini/Documents/Qt ProtoTypes/Test/Test../../../../../../DLL/libFwlib32d.a needed by debug/Test.exe. Stop.
My current *.pro file is the following:
QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++11 # 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 += \ main.cpp \ mainwindow.cpp HEADERS += \ Fwlib32.h \ mainwindow.h FORMS += \ mainwindow.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/../../../../../../DLL/ -lFwlib32 else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../../../DLL/ -lFwlib32d INCLUDEPATH += $$PWD/../../../../../../DLL DEPENDPATH += $$PWD/../../../../../../DLL win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../../../../../DLL/libFwlib32.a else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../../../../../DLL/libFwlib32d.a else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../../../../../DLL/Fwlib32.lib else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../../../../../DLL/Fwlib32d.lib
On my C drive, I have a folder named DLL (I know this only contains .h and .lib files, I have been testing a plethora of things to make this work), which has the following files in it
-
Fwlib32.h
-
Fwlib32.lib
My original belief was that I would specify these with the INCLUDEPATH/LIBS variable as
INCLUDEPATH += C:/DLL win32:LIBS += C:/DLL/Fwlib32.lib
Although when I took out the Qt generated 'Add to library' code, which starts at the CONFIG variable in the above .pro file, I received the following error:
undefined reference to `__imp_....`
Where the '...' implies the function name I was testing on.
I hope someone can spot what I am doing incorrectly, as I am new to Qt but have done many packaging with .dll's in Maven for Java, and I recall experiencing learning curve issues when beginning that, but I just cannot seem to figure out what I'm doing wrong here.
Thank you in advance, and if you need anymore information please let me know!
-
-
Hi, I just tested for you :-) ir's pretty easy to find fwlib32 from FOCAS on google
Using a Qt version 6 then it's much more difficult to build for 32-bit dlls.
So instead I recommend you downgrade to Qt 5, I just tried Qt 5.15.2 and MinGW 8.1 32-bit version.
First, make an empty vanilla widgets app.
Add this line to your .pro file:LIBS += C:/DLL/fwlib32.lib
Edit your mainwindow.cpp file to look like this:
#include "mainwindow.h" #include "ui_mainwindow.h" #include "C:/DLL/fwlib32.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); cnc_mdg_moniclear(1); } MainWindow::~MainWindow() { delete ui; }
compile and build it in Release mode.
Then to test, make sure to copy the fwlib32.dll to the same directory as the .exe file.
Good luck! -
How do you export the functions? Are they plain c? If so did you add extern "C" around your include statement when you use the header.
-
The functions are in the C header file supplied by the company, and I assume that was just added to the .cpp file as
#include "Fwlib32.h"
So following your comment, would the correct form be:
extern "C" { #include "Fwlib32.h" }
Edit: I just added the above include statement to my mainwindow.cpp, and still received the undefined reference error for the function...Here's my mainwindow.cpp code, it's just barebones. I wanted to connect to the library without any other possible factors before integrating it into a more complex application.
#include "mainwindow.h" #include "ui_mainwindow.h" extern "C" { #include "Fwlib32.h" } MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); cnc_mdg_moniclear(1); // <---- This is the line that is causing the error } MainWindow::~MainWindow() { delete ui; }
Thanks @Christian-Ehrlicher for helping out (:
Edit #2: Not sure if I mentioned this before, or if it matters, but it is a external header file, along with the external .dll and .lib files.
-
When you really include Fwlib32.h with
extern "C"
everywhere then you should not get warnings about missing__imp_FOO
functions when I'm correct.
Make sure to add the guard everywhere. Also please post the linker error with the full name of the function./edit: You're using MinGW? Right? Then try to link directly against the dll instead the lib - MinGW can't do anything with the static import lib for MSVC but has the ability to directly link against C dlls since some years iirc.
-
The full error(s) here:
debug/mainwindow.o: in function `MainWindow::MainWindow(QWidget*)':
C:\Users\Sorsini\Documents\Qt Prototypes\Test\build-Test-Desktop_Qt_6_2_3_MinGW_64_bit-Debug/../Test/mainwindow.cpp:13: undefined reference to `__imp_cnc_mdg_moniclear'
collect2.exe: error: ld returned 1 exit status
[Makefile.Debug:73: debug/Test.exe] Error 1
I went ahead and put the
extern "C" {#include "Fwlib32.h"}
around where I am including the .h file, and this is the only place I include it, which is in mainwindow.cpp.Could this be an issue with how I am initializing the LIBS and INCLUDEPATH variables within the *.pro file?
-
@Christian-Ehrlicher Yes I am using MinGW. So instead of in the .pro file having
win32:LIBS += C:/DLL/Fwlib32.lib // Change it to the following win32:LIBS += C:/DLL/Fwlib32.dll
Also, how would I verify that I am using MinGW? I am almost sure that I am, as when I go to the bottom where the errors are I right click and press 'Show Output', which takes me to the compile output, and I see this for my Makefile, making me believe I am using MinGW.
C:/Qt/Tools/mingw900_64/bin/mingw32-make -f Makefile.Debug
Edit: So I changed around my .pro file to mimic what you had said about the .dll instead of the .lib. When I did that, I receievd a new error which was
collect2.exe: error: ld returned 5 exit status
I saw on stackoverflow that adding this to the .pro file helped someone mitigate the issue
mingw { contains(QT_ARCH, x86_64): { LIBS+=-Wl,--no-gc-sections } }
Which now I received a
Failed to start program. Path or permissions wrong?
I know that the path is there, and that the files I am looking for are as well there...
-
@orsini29 said in Issue linking .lib or .dll for usage in .cpp:
x86_64
So you're using a 64Bit Qt? The library seems to be a 32bit one (when I interpret Fwlib32 correct) - you can't mix them.
-
I can not believe that slipped my mind... So if I download the 32 bit version, and then configure the .pro file the same as above, minus
mingw { contains(QT_ARCH, x86_64): { LIBS+=-Wl,--no-gc-sections } }
You think that would be the solution?
-
Hi, I just tested for you :-) ir's pretty easy to find fwlib32 from FOCAS on google
Using a Qt version 6 then it's much more difficult to build for 32-bit dlls.
So instead I recommend you downgrade to Qt 5, I just tried Qt 5.15.2 and MinGW 8.1 32-bit version.
First, make an empty vanilla widgets app.
Add this line to your .pro file:LIBS += C:/DLL/fwlib32.lib
Edit your mainwindow.cpp file to look like this:
#include "mainwindow.h" #include "ui_mainwindow.h" #include "C:/DLL/fwlib32.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); cnc_mdg_moniclear(1); } MainWindow::~MainWindow() { delete ui; }
compile and build it in Release mode.
Then to test, make sure to copy the fwlib32.dll to the same directory as the .exe file.
Good luck! -
@hskoglund Awesome! I am going to try that now. Thanks so much, seriously. I have been banging my head against the wall on this over a week now. I couldn't find version 5.15.2, but I did find 5.0.3. Is just the MinGW the 32 bit version? I can't seem to find a Qt Creator in 32 bit version.... Thanks again :D
Edit: Nevermind...I found the 5.15.2 version and the MinGW 8.1 32 bit..I will be testing it out shortly! Thanks again @hskoglund
-
Hi,
There's no need for Qt Creator's architecture to match the architecture of the Qt version you use for your project.