My IDE: Visual Studio 2010, I use Qt add-in for VS, Qt ver. 4.8.1
I have faced with the problem while trying to create precompiled header(pch) in my Qt project.
My usuall approach for creating pch in non Qt project is:
As those action failed for Qt project I decided what it happens due to pch should be included to all files generated by MOC.
I read the article in QtAssistant on precompiled headers and did the following:
Created header file;
For all .cpp files in project set option use pch and for one create
Converted to qmake generated project
I ran qmake -project
I modified generated .pro file, here it is:
TEMPLATE = app TARGET = DEPENDPATH += . GeneratedFiles INCLUDEPATH += . PRECOMPILED_HEADER = StdAfx.h QT += network
HEADERS += server.h StdAfx.h FORMS += server.ui SOURCES += main.cpp server.cpp StdAfx.h.cpp RESOURCES += server.qrc
I ran qmake
open .pro file and tried to build it and got the error:
Error 2 error C1083: Cannot open include file: 'StdAfx.h': No such file or directory
What I am doing wrong?
Precompiled headers (PCH) are a performance feature supported by some compilers to compile a stable body of code, and store the compiled state of the code in a binary file. During subsequent compilations, the compiler will load the stored state, and continue compiling the specified file.
Usage of precompiled headers may significantly reduce compilation time, especially when applied to large header files, header files that include many other header files, or header files that are included in many translation units.
The compiler options for precompiled headers are /Y . In the project property pages, the options are located under Configuration Properties > C/C++ > Precompiled Headers. You can choose to not use precompiled headers, and you can specify the header file name and the name and path of the output file.
To turn off precompiled headersSelect the Configuration properties > C/C++ > Precompiled Headers property page. In the property list, select the drop-down for the Precompiled Header property, and then choose Not Using Precompiled Headers.
Create your precompiled header file and include the desired headers.
pch.hpp:
// precompiled headers
// add C includes here
#ifdef __cplusplus
// add C++ includes here
#include <iostream>
#include <QtGui>
#endif // __cplusplus
Then in your .pro file:
CONFIG += precompile_header
PRECOMPILED_HEADER = pch.hpp
HEADERS += pch.hpp
Qmake will now automatically set the correct options for the compiler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With