PDA

View Full Version : PCH and QtCreator warning of not finding the type name



kornicameister
31st October 2010, 10:50
recently I found that I can make my compilation faster using PCH file, so decided to go on it, prepared suitable _pch.h header file when I put all includes I use in my whole project and I keep repeating the same includes in many header files (I assume this is one of the additional-feature of pch.h, make me correct, if I'm wrong). I also modified my *.pro file (basing on docs), below is the snippet from it


CONFIG += precompile_header
PRECOMPILED_HEADER = _pch.h ####it is a precompiled headear file
precompile_header:!isEmpty(PRECOMPILED_HEADER) {
DEFINES += USING_PCH
}

and here is _pch.h

#ifndef _PCH_H
#define _PCH_H
#if defined __cplusplus
#include <QtCore>
#include <QtGui>
#include <QDebug>
#include <QList>
#include <QPointer>
#include <QObject>
#include <QString>
#endif
#endif // _PCH_H

and from now on it works pretty well, nevertheless in some headers (not all) I have a warning from QtCreator that for example QString or QPointer is not a type name, where in other headers this warning is not produced

it is both very interesting that in some files there is no warning and very annoying because underlined lines of code are like red rag though it does not affect on compilation process

perhaps I did something wrong or assumed something which in obvious way is a mistake and my code is a time bomb from now on ?

wysota
31st October 2010, 11:10
If you have a pch file then you don't need to put the actual include statements in source files and still the compilation succeeds. But since Creator doesn't use the pch file in any way, it needs those include statements to be there to properly parse your code.

kornicameister
31st October 2010, 11:26
so I see solution with pch is rather tricky, as compiler knows everything is OK in opposite to Creator.

but let me ask this question, because this part is not so obvious to me and I've not encountered this ever before. If I add includes to headers separately from what's in pch, won't it be like overriding and including something twice, which is obviously unnecessary?

wysota
2nd November 2010, 18:07
so I see solution with pch is rather tricky, as compiler knows everything is OK in opposite to Creator.
It's not tricky if you remember that having a precompiled header doesn't allow you to not include individual files in your code. It works because of the way precompiled headers work but syntactically this is incorrect. You are just abusing a side effect of PCH.


If I add includes to headers separately from what's in pch, won't it be like overriding and including something twice, which is obviously unnecessary?
The compiler will either know a particular file is part of the pch and will not parse it again (which I doubt is the case) or it will encounter a #ifndef GUARD_H, #define GUARD_H, #endif clause that will prevent the file from being parsed again. Actually I think there is probably some hybrid solution to this but this might be compiler dependent.