在提出问题之前,我想告诉我,我已经在这里彻底搜索了答案。他们似乎都没有解决我的问题。 问题: 在给出on the Qt wiki后面的步骤后,我使用Qt创建了一个简单的C ++ DLL文件,问题是虽然创建了DLL并且我已将其链接到我的Qt小部件应用程序,但应用程序在创建文件对象时崩溃。 这里我发布了源代码和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:\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;
}
现在应用程序输出
D:\Gurushant\My Other Side Projects\Making dll Files\build-myWidgetApp-Desktop_Qt_5_9_1_MinGW_32bit-Release\release\myWidgetApp.exe
崩溃了。
我使用的是Windows 7 Professional和Qt 5.9.1
答案 0 :(得分: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)