Page 1 of 1

Precompiled Headers (Fast build) Rate Topic: ****- 1 Votes

#1 pokiaka   User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 76
  • Joined: 05-August 11

Posted 03 June 2012 - 09:49 PM


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:

Creating stdafx.h
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!

Creating stdafx.cpp

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!

#include "stdafx.h"

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.
Navigate to:
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.

Good luck.
If you found any errors or mistakes, feel free to let me know.

Is This A Good Question/Topic? 6
  • +

Replies To: Precompiled Headers (Fast build)

#2 dartoscoder   User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 313
  • Joined: 15-May 09

Posted 06 July 2012 - 02:48 PM

That is really cool. thanks a lot.

I found out, though, that you need to switch that setting in VC++ 2010 to use precompiled headers after actually setting it to create.

This post has been edited by dartoscoder: 06 July 2012 - 05:59 PM

Was This Post Helpful? 0
  • +
  • -

#3 stackoverflow   User is offline

  • D.I.C Addict
  • member icon

Reputation: 166
  • View blog
  • Posts: 545
  • Joined: 06-July 11

Posted 06 July 2012 - 06:50 PM

Good writeup-- thanks for taking your time and writing this up!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1