Reputation: 347
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 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
testlib.pro:
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.a
to 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
Upvotes: 1
Views: 168
Reputation: 347
So, what I've changed following @R Sahu hints:
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.
Upvotes: 1
Reputation: 206557
You have to manipulate the compiler options such that:
When building the library, you have
#define EXPORT __declspec(dllexport)
When using the library, you have
#define EXPORT __declspec(dllimport)
One way to do this is to use:
#if defined(BUILD_DLL)
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __declspec(dllimport)
#endif
And then use -DBUILD_DLL
when building the DLL and leave it undefined when using the DLL.
Upvotes: 2