Federico
Federico

Reputation: 748

PRECOMPILED_HEADER and Subdirs template on Qt

I have a big Qt project, splitted on several static libraries (around 70) and one application, and for that I'm using a .pro file with the subdirs template.

To speed up the compilations time, I want to use precompiled headers and found that using the PRECOMPILED_HEADER on each sub-project does the trick, BUT, every project compiles the precompiled header separately (and that one is the slowest step).

There is a way to "share" the precompiled header between all the subprojects included on the subdirs template?, so the precompiled header can be built once, and be used by all subprojects?

Regards

Upvotes: 8

Views: 1137

Answers (5)

metizik
metizik

Reputation: 1

There is no way to do this if you are using qmake.exe. qmake binary has built in flags that say to CREATE (-Yc) pch for each project. There is no way to cut it out.

Upvotes: 0

Trass3r
Trass3r

Reputation: 6287

No official way unfortunately. You can use some hacks though to at least get basic PCH support.

I used

PRECOMPILED_HEADER = ../includes.h

in one project that just creates the .pch and then "simulate" the 2nd half of PRECOMPILED_HEADER in some common .pri file for the real projects. E.g. with VS you'd use something like:

QMAKE_CXXFLAGS += /FIincludes.h /Yuincludes.h /Fppath/to/1stproj/intdir/projname_pch.pch
#QMAKE_CXXFLAGS += /wd4651
QMAKE_LFLAGS += path/to/1stproj/intdir/projname_pch.obj

You may run into different problems, e.g.

  • if compiler flags differ too much (see wd4651 above).
  • It also doesn't work ootb with pdb files. Use /Z7 instead.
  • With gcc you might run into incompatible -fPIC options.
  • etc.

Upvotes: 2

artm
artm

Reputation: 3678

After some experimentation I found no way to share a precompiled header between subprojects. I think the rationale behind the lack of this functionality is that each subproject may change compiler / preprocessor flags which would make precompiled headers built for one subproject incompatible with other subprojects.

Shared precompiled header could be useful for some simple projects where compiler flags are also shared, but I guess detecting and handling deviations from this condition would be too much hassle for the qmake.

Upvotes: 3

IggShaman
IggShaman

Reputation: 601

Just a suggestion - you might want to try to use cmake. It does have both qt and pch support, and it's current pch support suffers from the same issue. The pch-related macros come in a separate easy to understand file, and can be rewritten to do just one big pch file for many subfolders. This is how I intend to solve the same issue for my qt project.

Upvotes: 0

fmuecke
fmuecke

Reputation: 8712

If each header would be compiled separately to its own pch, then this could work. Unfortunately, as far as I know, in c++ projects there is only one translation unit for the precompiled headers that can be used globally per project. Well, you could use one global file for all your projects, but that is leas effective: this can lead to increased build times, because every time precompiled header is changed EVERYTHING has to be rebuilt.

Upvotes: 0

Related Questions