【问题标题】:Qt app crashing on linking with DLL made using QtQt 应用程序在与使用 Qt 制作的 DLL 链接时崩溃
【发布时间】:2017-09-26 05:02:28
【问题描述】:

在问这个问题之前,我想告诉你,我已经在这里彻底寻找了答案。他们似乎都没有解决我的问题。 问题: 在给定on the Qt wiki 的步骤之后,我使用Qt 创建了一个简单的C++ DLL 文件,问题是虽然创建了DLL,并且我已将其链接到我的Qt 小部件应用程序,但应用程序在创建文件对象时崩溃。 这里我发布源代码和 qdebug 输出

图书馆Qt专业版

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:\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"

主窗口.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:\Gurushant\My Other Side Projects\Making dll Files\build-myWidgetApp-Desktop_Qt_5_9_1_MinGW_32bit-Release\release\myWidgetApp.exe 崩溃了。

我正在使用带有 Qt 5.9.1 的 Windows 7 Professional

【问题讨论】:

  • 复制你的可执行文件旁边的myLib.dll,然后重试
  • 您的路径包含空格 最好使用添加库对话框来添加外部库,只需右键单击您的 .pro 文件并选择添加库
  • 您是否通过 Dependency Walker 运行它以查看是否缺少任何其他 DLL?
  • @saeed 我试过了,但效果不错
  • @saeed 我也尝试将库保持在不包含空间但它有效的路径中

标签: c++ qt dll


【解决方案1】:

到目前为止,经过大量试验和错误,包含库的最佳方法是

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 后不要使用空格

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    相关资源
    最近更新 更多