我正在嘗試轉移到 Qt 以重寫具有大量靜態庫的 win32 專案,因此作為初步測驗專案,我嘗試按照https://www.toptal.com/qt/vital-中的說明創建一個子目錄專案指南-qmake。我在這個網站上看到了很多其他的例子和類似的問題,但是它們都不是一個簡單的例子,在我構建它時實際上可以作業,所以我提出這個最小的例子,希望有人能告訴我它是什么是我失蹤了。
這是檔案結構;
project/
project.pro
app/
app.pro
main.ccp
library/
library.pro
mainwindow.h
library.pri
應用程式和庫專案都是簡單的 Qt Widgets 應用程式,使用 New Subproject 向導創建。app專案有main.cpp;
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
庫專案只有定義 MainWindow 類的頭檔案 mainwindow.h;
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) {}
~MainWindow() {}
};
#endif // MAINWINDOW_H
這里是專業人士。檔案;
專案.pro;
TEMPLATE = subdirs
TARGET = project
SUBDIRS = app library
app.depends = library
應用程式.pro;
TEMPLATE = app
TARGET = app
QT = core gui
greaterThan(QT_MAJOR_VERSION, 4): QT = widgets
CONFIG = c 11
SOURCES = \
main.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS = target
include(../library/library.pri)
圖書館.pro;
TEMPLATE = lib
TARGET = library
QT = core gui
greaterThan(QT_MAJOR_VERSION, 4): QT = widgets
CONFIG = c 11
HEADERS = \
mainwindow.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS = target
DISTFILES = \
library.pri
Lastly, the library.pri file;
LIBTARGET = library
BASEDIR = $${PWD}
INCLUDEPATH *= $${BASEDIR}/include
LIBS = -L$${DESTDIR} -llibrary
The point of this is to check that I can write a project in Qt where a function in one project can reference a class in another. When compiled as a single project, with main.cpp and mainwindow.h in the same project, you get the usual application window on screen with no errors so I know both main.cpp and the MainWindow class are good; compiled as-is, with the two sub-projects, I get "'mainwindow.h' file not found" on line 1, main.cpp, and "unknown type name 'MainWindow'" on line 6, main.cpp, so the library project is obviously not getting referenced properly by the app project. I'm new to Qt and have absolutely no experience with qmake, any help would be appreciated, and as I know I'm not the only person struggling with this if anyone can help get this working I'll put it up on gtihub.
uj5u.com熱心網友回復:
要使用庫,您需要設定兩件事: appendINCLUDEPATH和LIBS,您可以在 pri 檔案中執行,然后 include in app,如果錯誤顯示“檔案 *.h 未找到”,則表示INCLUDEPATH不完整。
以下是您的操作方法:
專案結構
project/
├── app
│ ├── app.pro
│ └── main.cpp
├── library
│ ├── library.pri
│ ├── library.pro
│ ├── mainwindow.cpp
│ ├── mainwindow.h
│ └── mainwindow.ui
└── project.pro
專案.pro
TEMPLATE = subdirs
SUBDIRS = \
app \
library
app.depends = library
圖書館.pri
INCLUDEPATH = $$PWD
win32 {
CONFIG(debug, debug|release) {
LIBS = -L$$PWD/debug
} else {
LIBS = -L$$PWD/release
}
} else {
LIBS = -L$$PWD
}
LIBS = -llibrary
圖書館.pro
QT = gui widgets
TEMPLATE = lib
CONFIG = staticlib
SOURCES = \
mainwindow.cpp
HEADERS = \
mainwindow.h
FORMS = \
mainwindow.ui
應用程式.pro
QT = gui widgets
SOURCES = main.cpp
include($$PWD/../library/library.pri)
QtCreator uses shadow build by default (it can be disabled on projects page), so if you build from QtCreator IDE not from shell (command line), you are likely using shadow build. In this case all build artifacts including static libraries are build outide of source directory in a separate directory named build-${project-name}-${device-type}-${build-config} (build-project-Desktop-Debug in our case), so -L$$PWD wont work, there's OUT_PWD for that, unfortunately for some reason it points not to pri directory but to pro directory (build-project-Desktop-Debug/app in out case) and things get a little hairier. Here's library.pri that works for both shadow and normal build:
INCLUDEPATH = $$PWD
win32 {
CONFIG(debug, debug|release) {
LIBS = -L$$PWD/debug -L$$OUT_PWD/../library/debug
} else {
LIBS = -L$$PWD/release -L$$OUT_PWD/../library/release
}
} else {
LIBS = -L$$PWD -L$$OUT_PWD/../library
}
LIBS = -llibrary
Full source: https://github.com/mugiseyebrows/app-and-library.git
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/445343.html
上一篇:基于型別的建構式模板特化
下一篇:三元運算子決定呼叫哪個成員函式
