How to use precompiled headers in a Qt project

My IDE: Visual Studio 2010, I use the Qt add-in for VS, Qt ver. 4.8.1

I ran into a problem when trying to create a precompiled header (pch) in my Qt project.

My general approach for creating pch in a project without Qt:
1. Create a title,
2. Include files that will be precompiled in the header.
3. For each source file in the project state in its properties, if it will use pch; 4. For one source file in the state of creation of the pch project; 5. Include pch in all source files.

How these steps failed for the Qt project. I decided that this was due to pch and should be included in all files created by MOC.

I read the QtAssistant article on precompiled headers and did the following:
1. Created header file; 2. For all .cpp files in the project set option, use pch and for one create 3. Converted to the project created by qmake
4. I ran qmake -project
5. I modified the generated .pro file, here it is:

TEMPLATE = app TARGET = DEPENDPATH += . GeneratedFiles INCLUDEPATH += . PRECOMPILED_HEADER = StdAfx.h QT += network # Input HEADERS += server.h StdAfx.h FORMS += server.ui SOURCES += main.cpp server.cpp StdAfx.h.cpp RESOURCES += server.qrc 
  • I launched qmake
  • open the .pro file and tried to compile it and got an error:

    Error 2 error C1083: Cannot open include file: 'StdAfx.h': no ​​such file or directory

What am I doing wrong?

+6
source share
2 answers

I have found a solution.
The only thing that needed to be done to use the precompiled header in the project was to include the following statements in the .pro file:

 CONFIG += nameOfPrecompiledHeader.h PRECOMPILED_HEADER = nameOfPrecompiledHeader.h win32-msvc* { PRECOMPILED_SOURCE = nameOfFileInWhichCreateOptionWillBeSet.cpp /* other .cpp files will be with use option*/ } 

after changing .pro and running qmake, all .cpp files will be configured to use pch and one to create it (nameOfFileInWhichCreateOptionWillBeSet) and .pch will be generated

+3
source

Create a 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 parameters for the compiler.

+14
source

All Articles