Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to put in precompiled header? (MSVC)

What are the best candidates for a precompiled header file? Can I put STL and Boost headers there, even though they have templates? And will that reduce compile times? Also, what are the best IDE settings to reduce compile times?

like image 385
rlbond Avatar asked Mar 27 '09 00:03

rlbond


People also ask

How do I set up precompiled headers?

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.

What should be in CPP header?

the header file (. h) should be for declarations of classes, structs and its methods, prototypes, etc. The implementation of those objects are made in cpp.

Are precompiled headers worth it?

The advantage of precompiled headers is that the huge system library header files and other files that do not change at all or infrequently only have to be parsed once per build. As all C compilers (that I know of) work on every .

What should C header files contain?

A header file is a file with extension . h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.


2 Answers

The quick answer: the STL and Boost headers do indeed belong in the precompiled header file, even though these header files define template classes.

When generating a precompiled header file, a compiler parses the header text (a significant task!), and converts it into a binary format that is optimised for the compiler's benefit.

Even though the template classes will be instantiated when other .cpp files are compiled, they will be instantiated from information in the precompiled header, which is significantly faster for the compiler to read.


(later addition)

One thing that you should not include in a precompiled header are files that are part of your project and are changed frequently, even if every single .CPP file includes these files.

The reason is this - the generation of the precompiled header can take a long time, because the boost, stl and windows libraries are very large.

You might have a simple file (eg "StringDefs.h") that everything uses. If StringDefs.h is included in stdafx.h, and one developer touches StringDefs.h, then every developer has to wait until the entire precompiled header recompiles. It would be much faster if StringDefs.h was left out of the precompiled header, and parsed along with each .CPP file.

like image 80
Andrew Shepherd Avatar answered Sep 22 '22 10:09

Andrew Shepherd


One addition to Andrew Shepherd's answer. Use the precompiled header for header files that are external to your project, for files that change infrequently. If you're changing the header files in the current project all the time, it's probably not worth precompiling them.

like image 28
George V. Reilly Avatar answered Sep 24 '22 10:09

George V. Reilly