Terence Chow
Terence Chow

Reputation: 11153

Why doesn't Qt recognize my header file? cannot open include file No such file or directory

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

error Cannot open include file: 'StdAfx.h': No such file or directory.

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?

.pro file has:

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

Image showing it recognizes and doesn't recognize simultaneously

Upvotes: 17

Views: 30302

Answers (7)

Raphael States
Raphael States

Reputation: 11

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.

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.

Sometimes, restarting the QtCreator is necessary. Doing the deleting and restarting always solves this exact issue.

Have a nice day!

Upvotes: 1

kentropy
kentropy

Reputation: 123

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.

All the best!

Upvotes: 1

stephone
stephone

Reputation: 197

add in .pro INCLUDEPATH += $$_PRO_FILE_PWD_

Upvotes: 3

vitakot
vitakot

Reputation: 3844

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.

I thought qmake is re-maked each time I change something in the .pro file, but obviously not.

Just run Build->qmake, it should fix it.

Upvotes: 13

Sirga
Sirga

Reputation: 91

You need to update the qmake file.

Build-> Run qmake

Upvotes: 9

cmannett85
cmannett85

Reputation: 22346

It is most likely because of the previous C1189 error, see here.

Upvotes: 0

koan
koan

Reputation: 3686

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.

You also have a stray header (ewrappersubclass.h) because you forgot to escape the end of line.

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".

Upvotes: 0

Related Questions