C++ 错误 QApplication:没有这样的文件或目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8995399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error QApplication: no such file or directory
提问by Ramy Al Zuhouri
I have installed C++SDK that have Qt but when I try compiling a code linking QApplication it gives me the error:
我已经安装了具有 Qt 的 C++SDK,但是当我尝试编译链接 QApplication 的代码时,它给了我错误:
Error QApplication: no such file or directory
How do I link these libraries? I searched into the directories and there is a file named QApplication.h; So I tried to link it with -I (linking the directory) but it was still giving me that error.
如何链接这些库?我搜索了目录,发现有一个名为 QApplication.h 的文件;所以我尝试将它与 -I(链接目录)链接,但它仍然给我那个错误。
回答by Timmmm
In Qt 5 you now have to add widgets
to the QT
qmake variable (in your MyProject.pro
file).
在 Qt 5 中,您现在必须添加widgets
到QT
qmake 变量(在您的MyProject.pro
文件中)。
QT += widgets
回答by karlphillip
To start things off, the error QApplication: no such file or directory
means your compiler was not able to find this header. It is not related to the linking process as you mentioned in the question.
首先,该错误QApplication: no such file or directory
意味着您的编译器无法找到此标头。它与您在问题中提到的链接过程无关。
The -I
flag (uppercase i) is used to specify the include (headers) directory (which is what you need to do), while the -L
flag is used to specify the libraries directory. The -l
flag (lowercase L) is used to link your application with a specified library.
该-I
标志(大写我)用于指定包括(头)目录(这是你需要做什么),而-L
标志用于指定库目录。该-l
标志(小写的L)用于将应用程序与指定的库链接。
But you can use Qt to your advantage: Qt has a build system named qmakewhich makes things easier. For instance, when I want to compile main.cppI create a main.profile. For educational purposes, let's say this source code is a simple project that uses only QApplication
and QDeclarativeView
. An appropriate .profile would be:
但是你可以使用 Qt 来发挥你的优势:Qt 有一个名为qmake的构建系统,它使事情变得更容易。例如,当我想编译main.cpp 时,我创建了一个main.pro文件。出于教育目的,假设此源代码是一个仅使用QApplication
和的简单项目QDeclarativeView
。一个合适的.pro文件是:
TEMPLATE += app
QT += gui declarative
SOURCES += main.cpp
Then, execute the qmake
inside that directory to create the Makefile that will be used to compile your application, and finally execute make
to get the job done.
然后,qmake
在该目录中执行以创建将用于编译应用程序的 Makefile,最后执行make
以完成工作。
On my system this make
outputs:
在我的系统上make
输出:
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_DECLARATIVE_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/opt/qt_47x/mkspecs/linux-g++ -I. -I/opt/qt_47x/include/QtCore -I/opt/qt_47x/include/QtGui -I/opt/qt_47x/include/QtDeclarative -I/opt/qt_47x/include -I/usr/X11R6/include -I. -o main.o main.cpp
g++ -Wl,-O1 -Wl,-rpath,/opt/qt_47x/lib -o main main.o -L/opt/qt_47x/lib -L/usr/X11R6/lib -lQtDeclarative -L/opt/qt_47x/lib -lQtScript -lQtSvg -L/usr/X11R6/lib -lQtSql -lQtXmlPatterns -lQtNetwork -lQtGui -lQtCore -lpthread
Note: I installed Qt in another directory --> /opt/qt_47x
注意:我在另一个目录中安装了 Qt --> /opt/qt_47x
Edit: Qt 5.x and later
编辑:Qt 5.x 及更高版本
Add QT += widgets
to the .pro file and solve this problem.
添加QT += widgets
到 .pro 文件并解决此问题。
回答by Jassim Talat
For QT 5
QT 5
Step1:.pro
(in pro file, add these 2 lines)
步骤1:(.pro
在pro文件中,添加这两行)
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
Step2:In main.cpp
replace code:
步骤2:在main.cpp
替换代码中:
#include <QtGui/QApplication>
with:
和:
#include <QApplication>
回答by Abhijeet Rastogi
Looks like you don't have the development libraries installed. Install them using:
看起来您没有安装开发库。使用以下方法安装它们:
sudo apt-get install libqt4-dev
As you said int the comments that you have them installed, just re-install it. Now. to update the locate's database, issue this command $sudo updatedb
正如您在安装它们的评论中所说的那样,只需重新安装即可。现在。要更新定位的数据库,请发出此命令$sudo updatedb
Then $locate QApplication
to check that you now have the header file installed.
然后$locate QApplication
检查您现在是否安装了头文件。
Now, goto the the folder where you have the code & type these commands
现在,转到您拥有代码的文件夹并键入这些命令
qmake -project
qmake
make
Then you can find the binary created.
然后你可以找到创建的二进制文件。
Alternatively, you can use Qt Creator if you want the GUI.
或者,如果需要 GUI,可以使用 Qt Creator。
See the official documentation for more info. http://developer.qt.nokia.com/doc/qt-4.8/gettingstartedqt.html
To learn how to use Qt Creator, use http://doc.qt.nokia.com/qtcreator-2.2/creator-qml-application.html
有关更多信息,请参阅官方文档。http://developer.qt.nokia.com/doc/qt-4.8/gettingstartedqt.html
要了解如何使用 Qt Creator,请使用http://doc.qt.nokia.com/qtcreator-2.2/creator-qml-application.html
回答by AliS
In Qt5you should use QtWidgets
instead of QtGui
在Qt5 中,您应该使用QtWidgets
而不是QtGui
#include <QtGui/QComboBox> // incorrect in QT5
#include <QtWidgets/QComboBox> // correct in QT5
Or
或者
#include <QtGui/QStringListModel> // incorrect in QT5
#include <QtCore/QStringListModel> // correct in QT5
回答by Loomchild
Please make sure that the version of qmake you are using corresponds to the version of QT you want to use.
请确保您使用的 qmake 版本与您要使用的 QT 版本相对应。
To be sure, you can just run :
可以肯定的是,您可以运行:
$qmake -v
Your problem seems to be a symptom of a version conflict between QT 3 and 4, as can be seen here :
您的问题似乎是 QT 3 和 4 之间版本冲突的症状,如下所示:
http://lists.trolltech.com/qt4-preview-feedback/2005-11/thread00013-0.html
http://lists.trolltech.com/qt4-preview-feedback/2005-11/thread00013-0.html
To fix this, you can either delete your old install of QT, or specifically point to qmake-qt4 in your Makefile.
要解决此问题,您可以删除旧的 QT 安装,或者在 Makefile 中专门指向 qmake-qt4。
回答by KABOURI ABDERRAZZAK
you have to add QT +=widgets in the .pro file before the first execution, if you execute before adding this line its not gonna working, so yo need to start file's creation from the beginning.
你必须在第一次执行之前在 .pro 文件中添加 QT +=widgets,如果你在添加这一行之前执行它不会工作,所以你需要从头开始创建文件。
回答by teresa
You can change build versiyon.For example i tried QT 5.6.1 but it didn't work.Than i tried QT 5.7.0 .So it worked , Good Luck! :)
您可以更改构建版本。例如,我尝试了 QT 5.6.1 但它没有用。我尝试了 QT 5.7.0。所以它有效,祝你好运!:)
回答by faressalem
Well, It's a bit late for this but I've just started learning Qt and maybe this could help somebody out there:
好吧,这有点晚了,但我刚刚开始学习 Qt,也许这可以帮助那里的人:
If you're using Qt Creator then when you've started creating the project you were asked to choose a kit to be used with your project, Let's say you chose Desktop Qt <version-here> MinGW 64-bit
. For Qt 5, If you opened the Qt folder of your installation, you'll find a folder with the version of Qt installed as its name inside it, here you can find the kits you can choose from.
如果您正在使用 Qt Creator,那么当您开始创建项目时,系统会要求您选择一个用于项目的工具包,假设您选择了Desktop Qt <version-here> MinGW 64-bit
. 对于Qt 5,如果您打开安装的 Qt 文件夹,您会在其中找到一个以安装的 Qt 版本为名称的文件夹,在这里您可以找到您可以选择的工具包。
You can go to /PATH/FOR/Qt/mingw<version>_64/include
and here you'll find all the includes you can use in your program, just search for QApplication
and you'll find it inside the folder QtWidgets
, So you can use #include <QtWidgets/QApplication>
since the path starts from the include
folder.
你可以去/PATH/FOR/Qt/mingw<version>_64/include
这里你会找到你可以在你的程序中使用的所有包含,只需搜索QApplication
,你就会在文件夹中找到它QtWidgets
,所以你可以使用,#include <QtWidgets/QApplication>
因为路径从include
文件夹开始。
The same goes for other headers if you're stuck with any and for other kits.
如果您坚持使用任何和其他套件,其他标题也是如此。
Note: "all the includes you can use"doesn't mean these are the only ones you can use, If you include iostream
for example then the compiler will include it from /PATH/FOR/Qt/Tools/mingw<version>_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/iostream
注意:“您可以使用的所有包含”并不意味着这些是您可以使用的唯一包含iostream
,例如,如果您包含,那么编译器将从/PATH/FOR/Qt/Tools/mingw<version>_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/iostream
回答by Denonth
I suggest you to update your SDK and start new project and recompile everything you have. It seems you have some inner program errors. Or you are missing package.
我建议您更新您的 SDK 并启动新项目并重新编译您拥有的所有内容。看来您有一些内部程序错误。或者你缺少包裹。
And ofc do what Abdijeek said.
并且按照 Abdijeek 所说的去做。