Maik
Maik

Reputation: 549

Eclipse CDT - Precompiled Header

I am looking for a straight forward way to use precompiled headers for a C++ project using Eclipse / CDT. The stuff does work when running from command line but I am looking for an easy way to integrate it into Eclipse.

Any suggestions ?!

Upvotes: 4

Views: 3346

Answers (4)

Puddle
Puddle

Reputation: 3335

Just to help others who may stumble upon this (since i spent some time figuring this out)

The first thing to do is create a PCH folder (if you want) with a pch.cpp and pch.h file.

pch.cpp: (this file is to compile the .gch)

#include "pch.h"

pch.h:

#include <iostream>
#include <string>
// more stuff that's not changing anytime soon...

The second thing to do is create a PCH build configuration.

exclude all your project source files from this build. (we only need to compile pch.cpp)
select the files/folders > Right Click > Resource Configurations > Exclude from Build > PCH

Now we need to make it compile as a .gch (precompiled header)
pch.cpp > Right Click > Properties > C/C++ Build > Settings > Tool Settings
Compiler > Pattern: ${COMMAND} ${FLAGS} ${INPUTS} (remove all the output stuff)
Misc > Flags: -c -x c++-header -o "../src/PCH/pch.h.gch"

Now when you build with this configuration, it'll produce the pch.h.gch where pch.h is.
(it'll also give a meaningless error trying to create an exe. just ignore that)


Back on the normal Release build, exclude pch.cpp since you don't need to compile that.

Now it's time to test the .gch is being used over the .h

at the top of pch.h put #error "not using precompiled header file"

The .ghc is only used if the header is included first, in the compiling file.

"Only one precompiled header can be used in a particular compilation."

you should precompile different selections of headers best fit for different compilation units


COMPILE TESTS

without precompiled headers
took 34s.189ms output src=2,143,078 bytes exe=1,346,864 bytes

with -include ../src/PCH/pch.h (included in every compilation unit) (only the pch.h not the .gch)
took 48s.364ms output src=2,159,431 bytes exe=1,355,298 bytes
(this is why we #include manually where needed so it should still take around 34s to compile without .gch)

with -include ../src/PCH/pch.h and pch.h.gch (fast compilation, but still inefficient with -include)
took 22s.535ms output src=2,159,431 bytes exe=1,355,298 bytes

if the pch's are organized/utilized properly (correct choice of headers and files to include them in) (not used in every file) it should be much faster than 22s. (this test was just done on a project which weren't built with pch's in mind)


additional speedup which can triple compile time
Project > Properties > C/C++ Build > Behavior > Enable parallel build

Upvotes: 2

Serg Kryvonos
Serg Kryvonos

Reputation: 4677

Consider using cmake and cotire

Upvotes: 0

Nigel Dyer
Nigel Dyer

Reputation: 81

Another approach is to create a source file (e.g. pch.cpp) with build settings changed to create the precompiled header. It should be included in the build in order to create the precompiled header, and then excluded in order to build the final executable. It needs to be temporarily reincluded if the header file is changed.

This website provides more details, including a way of creating different precompiled headers for each build configuration.

Upvotes: 1

Nemikolh
Nemikolh

Reputation: 700

From what i've seen, currently Eclipse CDT doesn't support precompiled headers directly. I mean, you can't set an option like : "For this header, compile it". The same applies for headers of external libraries of your eclipse project.

As you know, you need to set the same compilers flag for the header compilation in order to get the compiler use it for the other compilation unit. (At least in the case of GCC).

So, you have several solutions :

  • You replace the default build command found in "Properties for -> C/C++ Build -> Builder Settings -> Build command" with a custom script that will modify the makefiles generated by eclipse and then call make. A ruby solution is proposed here. Note, that it doesn't allow the use of multiple precompiled headers.
  • You can use ccache instead of using GCC. It detects when the same compilation is done again, so it's a bit different from using precompiled headers.

EDIT :

To get the ruby script working, you need to have at least one .cpp file inside the same directory as your header file. Otherwise you'll get an error on a missing file "subdir.mk".

Upvotes: 1

Related Questions