Okay, so if you're programming in C++, and you don't know what Precompiled Headers are, chances are that compiling a project takes you something like 4 seconds, and on bigger projects it can even take 20 seconds, and much more just for a simple build.
Obviously, this gets very frustrating.
So if this is the case, this tutorial is a gold-mine for you. this knowledge was for me when I learned it.
Precompiled Headers have literal meaning.
They are headers, that the compiler compiles, then saving them in a file (which I'll describe soon), and uses that file when compiling (which leads to saving you a lot of time).
Instead of building the header files over and over again, at each build, we just hold that information inside a file.
Usualy it ends with the extension .pch and .gch.
So how can we do it?
We create a header file. usually called stdafx.h. it will hold the header files we want to precompile.
Those header files would usually be specific headers that we use frequently in the project.
We'll then create a cpp file that will #include the stdafx.h file we created.
We'll let the compiler know we want to use precompiled headers.
And finally, we'll use them, simply by including stdafx.h.
So let's begin:
Create a header file, name it stdafx.h for learning purposes. you can actually name it however you want but it's usually named like so.
In the file use the directive #pragma once, and then you can begin inserting the header files you're using. for this instance I'll use Windows.h
#pragma once #include <Windows.h> // You can #include much more header here of course. this is the place!
Now you'll need to create another file, that will just include the header file we just created.
Name it stdafx.cpp for now (once again, you can name it however you want).
You just include stdafx.h here! YOU DO NOT INCLUDE ANY MORE HEADER FILES HERE!
That's it.. that's the whole file.
Setting the compiler
* I'm using Visual Studio. if you use a different compiler, find out how it is done by yours.
Now, we got our files but they're no different from any other files in our project (and in their essence - they are).
So we need to let the compiler know they are actually precompiled headers, and not just source code.
So in Visual Studio to do this.
Press Alt+F7 or at the Visual Studio's menu go to Project --> Project Properties.
Configuration Properties --> C/C++ --> Precompiled Headers
Set the Precompiled Headers property value to Create (/Yc).
And make sure the Precompiled Header File property value holds the correct name for your precompiled header file. (in our case stdafx.h).
Using the headers
Now you #include stdafx.h to your project's files (and remove any #include you already had there which exists in stdafx.h, of course).
And that's it. have a first build so the compiler will create the file so it won't be very fast. but from there your build would take 1 or less then a second.
If you found any errors or mistakes, feel free to let me know.