I have read tutorials using my google-fu on how to set up Pre-Compiled Headers, and now I wanted to clear up a few questions:

If all the 'to be pre-compiled' headers go in one file - "stdafx.h" then how do you say you only want 1 of those files in your 'unstable' class headers/implementations? 
If the said PCH header only goes at the top of your implementation (.cpp) file then how is your class in the header (.h) file supposed to know about objects? 
Does the compiler know because it is included before the implementation includes its own header?
Arrrh there any pitfalls I should know about?

Thanks in advance...

Recommended Answers

All 6 Replies

I assume you are using one of the Microsoft c++ compilers. If you want precompiled headers then every *.cpp file in the project must include stdafx.h as the first header file. You can include any others you want after that.

#include "stdafx.h" // precompiled header file goes first
#include "myclass.h" // now all others you want 
#include "anotherclass.h"

Your project will also have stdafx.cpp, which is the first *.cpp file that the comopiler will compile, and the compiler generates the *.pch file at that time. The *.pch file contains all the pre-processed files that are in stdafx.h. This greatly speeds up the compilation of large projects because the compiler doesn't have to read all the includes for every *.cpp file.

Precompiled headers do not work the C programs, it's C++ only. If your project is a mixture of both c++ and C files then you will have to manually turn off precompiled header options for all the C files.

Thank you Ancient Dragon.
Do i need to specify which, out of the entire list of headers in stdafx.h, that I want to include or do I get the whole shabang? Does the compiler only link in what I call/reference/instatiate ?

Everything in stdafx.h is included in precompiled headers.

Yes, the linker only links into the executable the functions etc. that are actually referenced in the program or one of the libraries that the program uses. It's just standard, normal linking and is the same with or without precompiled headers.

So why shouldn't I put everything in PCHs?

anotherthing: don't forget the compiler read everything by order from top to bottom(inclued that header files). for exemple: if you use 1 function before been created, you will get an error that don't exists or something.

"So why shouldn't I put everything in PCHs?"
you can make your own headers files and include their everything that you neeed. then you just need include these header file to your main.cpp file.

So why shouldn't I put everything in PCHs?

That is a compiler-generated file, you can't do anything with it except delete it when it's no longer needed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.