Gurushant
Gurushant

Reputation: 942

Qt app crashing on linking with DLL made using Qt

Before asking the problem, I would like to tell that i have thoroughly searched for the answer here. None of them seems to address my issue. Problem: I have created a simple C++ DLL file using Qt after following steps given on the Qt wiki the problem is though the DLL is created and I have linked it to my Qt widget app, the app crashes on creating the object of the file. Here I am posting the source code and the qdebug output

libraray Qt pro

QT       += core gui widgets

TARGET = myLib
TEMPLATE = lib

DEFINES += MYLIB_LIBRARY

DEFINES += QT_DEPRECATED_WARNINGS

SOURCES += \
        mylib.cpp

HEADERS += \
        mylib.h \
        mylib_global.h 

unix {
    target.path = /usr/lib
    INSTALLS += target
}

mylib.h

#ifndef MYLIB_H
#define MYLIB_H

#include "mylib_global.h"

class MYLIBSHARED_EXPORT MyLib
{

public:
   MyLib();

   int add(int x, int y);
   int sub(int x, int y);

};

#endif // MYLIB_H

mylib_global.h

#ifndef MYLIB_GLOBAL_H
#define MYLIB_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(MYLIB_LIBRARY)
#  define MYLIBSHARED_EXPORT Q_DECL_EXPORT
#else
#  define MYLIBSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // MYLIB_GLOBAL_H

mylib.cpp

#include "mylib.h"
#include<QDebug>

MyLib::MyLib()
{
}

int MyLib::add(int x, int y)
{
    qDebug()<<"Adding";
    return x+y;
}

int MyLib::sub(int x, int y)
{
   return x-y;
}

Now the app

myWidgetApp.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = myWidgetApp
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
    main.cpp \
    mainwindow.cpp
HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui
INCLUDEPATH += "D:\Gurushant\My Other Side Projects\Making dll Files\myLib"

LIBS += "D:\Gurushant\My Other Side Projects\Making dll Files\build-myLib-Desktop_Qt_5_9_1_MinGW_32bit-Release\release\myLib.dll"

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"mylib.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    MyLib lib;
    lib.add(1,2); 
}

MainWindow::~MainWindow()
{
    delete ui;
}

Now the application output D:\Gurushant\My Other Side Projects\Making dll Files\build-myWidgetApp-Desktop_Qt_5_9_1_MinGW_32bit-Release\release\myWidgetApp.exe crashed.

I am using Windows 7 Professional with Qt 5.9.1

Upvotes: 1

Views: 995

Answers (1)

Gurushant
Gurushant

Reputation: 942

So far after doing a lot of trial and error, the best way to include the library is to

INCLUDEPATH += (Your_Path)
DEPENDPATH += (Your Path)

(if your target path is win32)
win32:CONFIG(release, debug|release): LIBS += -L(path) -l(lib_file_without_dot_dll)
  • don't use space after -L

Upvotes: 1

Related Questions