Problem with linking C library to Qt project
-
I know this is a 100times discussed topic, but after number of attempts, I just can't find the solution since don't understand the situation - no errors.
I am trying to connect dll "C" library to my project and receiving some crazy code in App output "...exited with code -1073741515" as well as empty console with Press <RETURN> to close this window...So, here are mine:
mylib.c:#include "mylib.h" int mysum(int a, int b){ return a + b; }
mylib.h:
#ifdef __cplusplus extern "C" { #endif #define EXPORT __declspec(dllexport) EXPORT int mysum(int, int); #ifdef __cplusplus } #endif
QT -= gui CONFIG += c++11 console CONFIG -= app_bundle SOURCES += \ main.cpp LIBS += -L$$PWD/../../../../TestDLL/ -lmylib INCLUDEPATH += $$PWD/../../../../TestDLL DEPENDPATH += $$PWD/../../../../TestDLL HEADERS += \ ../../../../TestDLL/mylib.h
main.cpp:
#include <QCoreApplication> #include "mylib.h" #include <QtDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); qDebug()<<mysum(1,2); return a.exec(); }
I think, I am missing something with linkage in pro file, but can't get what.
TestDLL is the folder with:
mylib.dll
mylib.h
mylib.c
libmylib.ato build the dll I've used:
gcc -c mylib.c gcc -shared -o mylib.dll -Wl,--out-implib,libmylib.a mylib.o
Will be appreciated for some hints... Thank you
-
what happens when you build the app with libmylib.a or mylib.o instead of the dll? Verify that the error is related to the dll linkage, and not the .o or .a file create when compiling the library.
What happens when you use the dll in a console app that is NOT a Qt app, but a simple gcc console app using std::cout << myint(a,b)
-
@Kent-Dorfman , thank you for the hint!
So, what I've changed:- in mylib.h :
#ifdef BUILDING_DLL #define EXPORT __declspec(dllexport) #else #define EXPORT __declspec(dllimport) #endif
and for compiling dll:
gcc -c -DBUILDING_DLL mylib.c gcc -shared -o mylib.dll mylib.o -Wl,--out-implib,libmylib.a
Plus in .pro file:
LIBS += "$$PWD/../../../../TestDLL/mylib.dll"
I believe, the line in .pro file is the most important. Now everything works fine.