Issue linking .lib or .dll for usage in .cpp
-
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.