我想创建一个包含另一个动态库B的动态库A. 库A本身用于创建使用A的GUI的项目中。一旦我单击编译,Gui应用程序就会崩溃。我认为这是一个图书馆导入问题。
所以我设置了一个最小的例子,你可以在问题的最后找到。 在最小的例子中,我可以让主项目工作,但不是在实际项目中。我错过了类比。
正如我从最后的例子中想到的那样,似乎可以归结为在我的文件夹结构中放置B库。我的目录结构就像
LibA
include (include folder for the class "MainUsesLibA"
libaclass.h
libbclass.h
libb.dll (copied from output of LibB compilation)
LibA.pro
libaclass.cpp
main.cpp
LibB
include
libaclass.h
LibB.pro
libbclass.cpp
main.cpp
MainUsesLibA
libs
liba.dll
libaclass.h <-- you do only need libaclass.h, not libbclass.h
libb.dll <-- must be supported, else CRASH **before** compiling!
This was my initial error (*)
(*)所以,这是一个主要问题:动态库必须部署到最后,即使它们在另一个dll中使用。清楚这一点。但是使用我的主要AudioPlayer应用程序,我也尝试了这个并且首先失败了(现在我必须再检查一下事情对我来说更清楚了)。
问题是:我一般要将哪个目录复制'libb.dll'以便'liba.dll'找到它?我没有在pro文件中指定'libb.dll',但我需要在项目目录中。到底在哪里?我是否从LibA内部了解到这一点?或者它总是在LibA的同一目录中?
库“libB”中使用的库“libA”在主应用程序“MainUsesLibA”中使用。
LibB.pro
QT += core
QT -= gui
TARGET = libb
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = lib
DEFINES += TEST_BUILD_LIBB
INCLUDEPATH += \
$$PWD/include
SOURCES += main.cpp \
libbclass.cpp
HEADERS += \
$$PWD/include/libbclass.h
libbclass.h
#ifndef LIBBCLASS_H
#define LIBBCLASS_H
#include <QObject>
#if defined TEST_BUILD_LIBB
#define TEST_COMMON_DLLSPEC Q_DECL_EXPORT
#else
#define TEST_COMMON_DLLSPEC Q_DECL_IMPORT
#endif
class TEST_COMMON_DLLSPEC LibBClass : public QObject
{
public:
LibBClass(QObject* parent = 0);
void sayHello();
};
#endif // LIBBCLASS_H
libbclass.cpp
#include "libbclass.h"
#include <QDebug>
LibBClass::LibBClass(QObject* parent) : QObject(parent){
}
void LibBClass::sayHello(){
qDebug()<<"Lib B: Hello";
}
创建lib“libB”并将dll复制到LibA的include目录。
LibA.pro
QT += core
QT -= gui
TARGET = liba
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = lib
DEFINES += TEST_BUILD_LIBA
LIBS += -L$$PWD/include/ -lb
INCLUDEPATH += $$PWD/include
SOURCES += \
main.cpp \
libaclass.cpp
HEADERS += \
include/libaclass.h \
include/libbclass.h
libaclass.h
#ifndef LIBACLASS_H
#define LIBACLASS_H
#include <QObject>
#if defined TEST_BUILD_LIBA
#define TEST_COMMON_DLLSPEC Q_DECL_EXPORT
#else
#define TEST_COMMON_DLLSPEC Q_DECL_IMPORT
#endif
class LibBClass;
class TEST_COMMON_DLLSPEC LibAClass : public QObject
{
Q_OBJECT
public:
LibAClass(QObject *parent = 0);
void sayHello();
private:
LibBClass* libB;
};
#endif // LIBACLASS_H
libaclass.cpp
#include "libaclass.h"
#include "libbclass.h"
#include <QDebug>
LibAClass::LibAClass(QObject* parent) : QObject(parent){
libB = new LibBClass(this);
}
void LibAClass::sayHello(){
qDebug()<<"LibA: Hello";
libB->sayHello();
}
MainUsesLibA.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MainUsesLibA
TEMPLATE = app
LIBS += -L$$PWD/libs/ -la <-- only specify the library LibA!
You do not need to specify LibB! (At least for compiling)
INCLUDEPATH += $$PWD/libs
SOURCES += \
main.cpp\
mainwindow.cpp
HEADERS += \
mainwindow.h \
libs/libaclass.h
FORMS += mainwindow.ui