Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use precompiled headers in Qt project

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:

  1. Create header;
  2. Include files which will be precompiled in header;
  3. For every source file in the project state in it`s properties if it will use pch;
  4. For one source file in project state creation of pch;
  5. Include pch in all source files.

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:

  1. Created header file;

  2. For all .cpp files in project set option use pch and for one create

  3. Converted to qmake generated project

  4. I ran qmake -project

  5. I modified 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

  6. I ran qmake

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

like image 733
spin_eight Avatar asked Dec 04 '12 19:12

spin_eight


People also ask

How does a precompiled header work?

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.

Should you use precompiled headers?

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.

How do I precompile a header file?

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.

How do I not use precompiled headers?

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.


1 Answers

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.

like image 139
Gordon Freeman Avatar answered Sep 28 '22 00:09

Gordon Freeman