C++ Qt Quick 2:创建和添加共享库

C++ Qt Quick 2:创建和添加共享库,c++,qt,dll,qmake,C++,Qt,Dll,Qmake,形势 我试图为我的项目创建一个dll,但idk我做错了什么 1步:创建共享库 我去了创建新项目-> 库-> c++库-> 添加了一些返回常量int的测试方法-> 内置实现和调试版本 2步:将库添加到我的项目中 我所做的是:右键单击我的“真实”项目-> 单击添加库-> 点击“外部库”选项-> 在“库路径”部分浏览到我的.so文件-> Qt将自动生成的代码添加到my.pro文件中 问题 我无法访问库中的标题 这是我的main.cpp #include <QGuiApplication>

形势

我试图为我的项目创建一个dll,但idk我做错了什么

1步:创建共享库

我去了
创建新项目
->
->
c++库
->
添加了一些返回常量int的测试方法
->
内置实现和调试版本

2步:将库添加到我的项目中

我所做的是:
右键单击我的“真实”项目
->
单击添加库
->
点击“外部库”选项
->
在“库路径”部分浏览到我的.so文件
->
Qt将自动生成的代码添加到my.pro文件中

问题

我无法访问库中的标题

这是我的
main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>

#include "StickyNotesCore.h" // <- says error: file not found

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;       

    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}
my
StickyNotes.pro

QT += quick
CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Refer to the documentation for the
# deprecated API to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp

RESOURCES += qml.qrc

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =

QT_QUICK_CONTROLS_STYLE=material ./app

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../build-StickyNotesCore-Desktop_Qt_5_13_0_GCC_64bit-Debug/release/ -lStickyNotesCore
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../build-StickyNotesCore-Desktop_Qt_5_13_0_GCC_64bit-Debug/debug/ -lStickyNotesCore
else:unix: LIBS += -L$$PWD/../build-StickyNotesCore-Desktop_Qt_5_13_0_GCC_64bit-Debug/ -lStickyNotesCore

INCLUDEPATH += $$PWD/../build-StickyNotesCore-Desktop_Qt_5_13_0_GCC_64bit-Debug
DEPENDPATH += $$PWD/../build-StickyNotesCore-Desktop_Qt_5_13_0_GCC_64bit-Debug
编辑

文件夹树


对于.pro文件,替换INCLUDEPATH ad DEPENDEPATH到库的.h文件所在的路径

确保在进行更改后运行
qmake

INCLUDEPATH += $$PWD/../build-StickyNotesCore-Desktop_Qt_5_13_0_GCC_64bit-Debug
DEPENDPATH += $$PWD/../build-StickyNotesCore-Desktop_Qt_5_13_0_GCC_64bit-Debug
下面是我在.pro文件中所做的一个示例

   win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../../mylibrary/lib/vc140/x64/ -lMYLIBRARY
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../../mylibrary/lib/vc140/x64/ -lMYLIBRARYd
else:unix: LIBS += -L$$PWD/../../../../../mylibrary/lib/vc140/Win32/ -lMYLIBRARY

INCLUDEPATH += $$PWD/../../../../../mylibrary/include
DEPENDPATH += $$PWD/../../../../../mylibrary/include

您需要为包含路径指定正确的位置。@rafaelgonzalez我该怎么做?转到windows资源管理器,在“stickynotescore.h”和“StickyNotes.pro”中找到“stickynotescore.h”所在的位置用正确的文件夹替换INCLUDEPATH和DEPENDEPATH。@rafaelgonzalez.dll/的意义是什么。因此,如果我必须手动指定我的所有文件的位置,那么在导入dll时您必须这样做,但您认为在导入dll时犯了错误,您最初没有指定正确的路径。对于愚蠢的问题,抱歉。但是,如果我必须将lib绑定到我的文件夹,那么构建lib有什么意义呢。我的意思是原始文件夹不包括.dll/.so文件。这是如何工作的?创建库的关键是代码的可重用性,您可以编写代码并将其用于其他项目。QT不知道在哪里可以找到您的库,这就是为什么您必须指定它的位置。
   win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../../mylibrary/lib/vc140/x64/ -lMYLIBRARY
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../../mylibrary/lib/vc140/x64/ -lMYLIBRARYd
else:unix: LIBS += -L$$PWD/../../../../../mylibrary/lib/vc140/Win32/ -lMYLIBRARY

INCLUDEPATH += $$PWD/../../../../../mylibrary/include
DEPENDPATH += $$PWD/../../../../../mylibrary/include