我在以下目录中有一个应用程序:
\Application
\GUI
MainWindow.cpp
MainWindow.h
main.cpp
gui.pro
gui.pro.user
\folder1
file1.cpp
file1.h
\folder2
file2.cpp
file2.h
我的应用程序可以在两种模式下运行:
- From the console
那个版本有效。我有一个负责处理依赖项的 makefile,因此我可以正常包含我的文件,因为它们位于同一目录中:
#include "file1.h"
代替:
#include "../folder1/file1.h"
- With my GUI
我的 GUI 在没有应用程序的情况下工作 - 我使用 QtCreator 编译它。
现在我想将我的 GUI 与应用程序结合起来。所以我扩展了 .pro 文件:
SOURCES += main.cpp\
Mainwindow.cpp \
..\folder1\file1.cpp \
..\folder2\file2.cpp \
HEADERS += Mainwindow.h \
MyTextEdit.h \
..\folder1\file1.h \
..\folder2\file2.h \
但是,当我运行 qmake 时,出现以下错误:
file1.h: no such file or directory.
我调查了一下,结果是编译器期望的
#include "../folder1/file1.h"
代替:
#include "file1.h"
好吧,我可以替换它,但我也希望能够运行控制台版本。
我该如何解决? .pro 文件不是 makefile,因此我不能像在控制台版本的应用程序的 makefile 中那样使用 INC 变量。
最佳答案
您需要指定 header 的完整路径,否则编译器不知道在哪里可以找到该文件。
为防止这种情况,在您的 GUI 的 pro 文件中添加以下行:
INCLUDEPATH += "../folder1/"
同时检查您的 header 和源路径。似乎缺少一些斜线。
编辑
我完整的工作 gui.pro 文件使用与上面给出的相同的文件夹结构。这样,一个简单的 #include "file1.h"
就可以在 gui 代码的每个文件中工作。
TEMPLATE=app
TARGET=gui
QT += widgets
INCLUDEPATH += ../folder1/
FORMS += \
mainwindow.ui
HEADERS += \
mainwindow.h \
../folder1/file1.h \
../folder2/file2.h
SOURCES += \
mainwindow.cpp \
main.cpp \
../folder1/file1.cpp \
../folder2/file2.cpp
关于c++ - 在这种情况下如何正确使用 qmake?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35621741/