QT应用程序与使用QT制造的DLL链接时崩溃

Qt app crashing on linking with DLL made using Qt

本文关键字:QT DLL 链接 崩溃 制造 应用程序      更新时间:2023-10-16

在询问问题之前,我想告诉我我已经在这里彻底搜索了答案。他们似乎都没有解决我的问题。问题:我已经在QT Wiki上给出的以下步骤创建了一个简单的C DLL文件。问题是创建了DLL,并且我将其链接到我的QT Widget应用程序,该应用程序在创建文件的对象时崩溃。在这里,我发布了源代码和QDebug输出

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;
}

现在应用

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:GurushantMy Other Side ProjectsMaking dll FilesmyLib"
LIBS += "D:GurushantMy Other Side ProjectsMaking dll Filesbuild-myLib-Desktop_Qt_5_9_1_MinGW_32bit-ReleasereleasemyLib.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;
}

现在的应用程序输出 D:GurushantMy Other Side ProjectsMaking dll Filesbuild-myWidgetApp-Desktop_Qt_5_9_1_MinGW_32bit-ReleasereleasemyWidgetApp.exe崩溃了。

我正在使用QT 5.9.1

的Windows 7 Professional

在进行大量反复试验之后,包括库的最佳方法是

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)
  • 在-l
  • 之后不要使用空间