【问题标题】:Qt Quick 2 : Creating and adding a shared libraryQt Quick 2:创建和添加共享库
【发布时间】:2019-12-24 11:05:39
【问题描述】:

情况

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

1 步:创建共享库

我去了create new project -> library -> c++ library -> added some test method that returns a const int -> built Realse and Debug versions

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

我所做的是:right click on my 'real' project -> clicked on add Library -> clicked on 'external library' option -> in 'library path' section browsed to my .so file -> Qt added auto-generated code to my .pro file

问题

我无法访问我的库中的标题。

这是我的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();
}

我的stickynotescore.h

#ifndef STICKYNOTESCORE_H
#define STICKYNOTESCORE_H

#include "stickynotescore_global.h"
class STICKYNOTESCORESHARED_EXPORT StickyNotesCore
{

public:
    StickyNotesCore();
    int Test();
};

#endif // STICKYNOTESCORE_H

我的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

编辑

文件夹树

【问题讨论】:

  • 您需要为包含路径指定正确的位置。
  • @rafaelgonzalez 我该怎么做?
  • 转到 Windows 资源管理器找到“stickynotescore.h”所在的位置,并在“StickyNotes.pro”中将 INCLUDEPATH 和 DEPENDEPATH 替换为正确的文件夹。
  • @rafaelgonzalez 如果我必须手动指定所有文件的位置,那么 .dll/.so 的意义何在
  • 你在导入dll时必须这样做,但认为你在导入dll时犯了错误,你最初没有指定正确的路径。

标签: c++ qt dll qmake


【解决方案1】:

对于您的 .pro 文件,将 INCLUDEPATH 和 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

【讨论】:

  • 对不起,愚蠢的问题。但是,如果我必须将它绑定到我的文件夹,那么构建 lib 的意义何在。我的意思是原始文件夹不包含 .dll/.so 文件。这是如何工作的?
  • 创建库的重点是代码的可重用性,您编写代码并可以将其用于其他项目。 QT 不知道在哪里可以找到您的库,因此您必须指定它的位置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多