C++ 为什么 Qt 不能识别我的头文件?无法打开包含文件 没有这样的文件或目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16257521/
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
Why doesn't Qt recognize my header file? cannot open include file No such file or directory
提问by Terence Chow
I have the below in my .pro file and I have files that #include "headerhere".
For example: #include "StdAfx.h"
. However I'm getting an
我的 .pro 文件中有以下内容,并且我有#include "headerhere".
例如: #include "StdAfx.h"
. 但是我得到了一个
error Cannot open include file: 'StdAfx.h': No such file or directory.
错误无法打开包含文件:'StdAfx.h':没有这样的文件或目录。
I get the same error whether I use #include "StdAfx.h"
or #include "Shared/StdAfx.h"
. This is extremely frustrating and I can't do any actual work unless Qt starts recognizing my headers. I've found online no solution for this. What is going on?
无论我使用#include "StdAfx.h"
还是#include "Shared/StdAfx.h"
. 这非常令人沮丧,除非 Qt 开始识别我的标题,否则我无法做任何实际工作。我在网上发现没有解决方案。到底是怎么回事?
.pro file has:
.pro 文件具有:
HEADERS += ibproject.h \
Shared/StdAfx.h \
Shared/TwsSocketClientErrors.h \
Shared/TagValue.h \
Shared/shared_ptr.h \
Shared/ScannerSubscription.h \
Shared/OrderState.h \
Shared/Order.h \
Shared/IBString.h \
Shared/HScrollListBox.h \
Shared/Execution.h \
Shared/EWrapper.h \
Shared/EClientSocketBaseImpl.h \
Shared/EClientSocketBase.h \
Shared/EClient.h \
Shared/Contract.h \
Shared/CommonDefs.h \
Shared/CommissionReport.h \
SocketClient/src/EClientSocket.h
ewrappersubclass.h
INCLUDEPATH += $$PWD/SocketClient
DEPENDPATH += $$PWD/SocketClient
EDIT: why I am getting downvotes? This is a legitimate problem I'm having
编辑:为什么我得到downvotes?这是我遇到的合法问题
回答by vitakot
I had the same problem as well. The reason was that I use two computers in parallel and the makefile tried to find files at paths as they are set on the previous one. But everything seemed to be fine - as in your case, tooltip when hovering over the include showed me the correct path, also F2 (follow symbol under cursor) navigated me to the correct header.
我也有同样的问题。原因是我并行使用两台计算机,makefile 试图在上一个设置的路径中查找文件。但一切似乎都很好 - 就像你的情况一样,将鼠标悬停在包含上时的工具提示向我显示了正确的路径,而且 F2(跟随光标下的符号)将我导航到正确的标题。
I thought qmake is re-maked each time I change something in the .pro file, but obviously not.
我认为每次更改 .pro 文件中的内容时都会重新制作 qmake,但显然不是。
Just run Build->qmake, it should fix it.
只需运行 Build->qmake,它就会修复它。
回答by Sirga
You need to update the qmake file.
您需要更新 qmake 文件。
Build-> Run qmake
回答by stephone
add in .pro INCLUDEPATH += $$_PRO_FILE_PWD_
添加 .pro INCLUDEPATH += $$_PRO_FILE_PWD_
回答by kennentropy
I know this post is very old but It just happened to me.
我知道这篇文章很旧,但它只是发生在我身上。
INCLUDEPATH += $$PWD
did the trick. Don't forget to do qmake and then build all.
成功了。不要忘记做 qmake 然后构建所有。
All the best!
祝一切顺利!
回答by Raphael States
I have this issue from time to time, mostly when I make a pull request or copy code around.
我不时遇到这个问题,主要是在我提出拉取请求或复制代码时。
The easy solution is to delete all qt generated files, and Build->qmake then Rebuild all.
简单的解决方法是删除所有qt生成的文件,然后Build->qmake然后Rebuild all。
This is a Qt Bug that it does not properly identify files that need to be regenerated and, even though the IDE links everything nicely, that error happens at compile time. The same happens when it generates ui_formname.h header files, some changes are not made effective straight away.
这是一个 Qt 错误,它没有正确识别需要重新生成的文件,即使 IDE 很好地链接了所有内容,该错误也会在编译时发生。生成 ui_formname.h 头文件时也会发生同样的情况,有些更改不会立即生效。
Sometimes, restarting the QtCreator is necessary. Doing the deleting and restarting always solves this exact issue.
有时,需要重新启动 QtCreator。执行删除和重新启动总是可以解决这个确切的问题。
Have a nice day!
祝你今天过得愉快!
回答by koan
The file is not in your include path.
该文件不在您的包含路径中。
The HEADERS part of a pro file lists header files that the project depends on. These files are considered for processing by moc if they have the Q_OBJECT macro in the class definition. Adding a file to HEADERS does not put it in the include search path.
pro 文件的 HEADERS 部分列出了项目所依赖的头文件。如果这些文件在类定义中具有 Q_OBJECT 宏,则它们会被 moc 考虑处理。将文件添加到 HEADERS 不会将其放入包含搜索路径中。
You also have a stray header (ewrappersubclass.h) because you forgot to escape the end of line.
您还有一个杂散的标题 (ewrappersubclass.h),因为您忘记转义行尾。
I suspect that the reason for this is a Windows case sensitivity problem. The compiler is case sensitive but the file system is not; or vice-versa. Such that you #include "shared/stdafx.h"
when you should #include "Shared/StdAfx.h"
.
我怀疑这是 Windows 区分大小写的问题。编译器区分大小写,但文件系统不区分;或相反亦然。这样的你,#include "shared/stdafx.h"
当你应该#include "Shared/StdAfx.h"
。