A quick guide to using Precompiled Headers (Visual Studio)

Precompiled headers can be a very useful thing. Increase productivity by saving time otherwise would have spent on compiling.

This is a quick guide on how to setup precompiled headers in Visual Studio (specifically VS2012). I’m not going to explain what it is, or why you might want it. So let’s go.

I will assume that you currently have a project that does not have precompiled headers setup but you want it now, or later down the road.

Win32_Application_Wizard

When you create a new project in Visual Studio, and you select a template such as the “Win32 Console Application” template. The Win32 Application Wizard will open and attempt to guide you through the basic configurations of the project. A tickbox is provided for if you want Precompiled header. This is not the case when you create an empty project. The Wizard simply does not appear.

To setup Precompiled Header. First create two source files: “stdafx.h” and “stdafx.cpp” (For the tinkers. It actually doesn’t matter what you name them.) , and add them to your projects.

solution_explorer

Now select all source files (your .cpp files) in the Solution Explorer, right click and hit Properties. The Property Page should pop up.

property_page

In the Property Pages, head to “Configuration Properties” – > “C/C++” -> “Precompiled Headers”. Change “Precompiled Header” to “Use (/Yu)”. Click “Apply” at the bottom right and click “OK” to close the page. Next, go back to Solution Explorer. Select and right click on the file “stdafx.cpp” (or whatever you named it), and click on “Properties” again. Navigate to “Configuration Properties” – > “C/C++” -> “Precompiled Headers” (again).

select_create

This time, change “Precompiled Header” to “Create (/Yc)”. Again, click “Apply” at the bottom right and click “OK” to close the page.

Lastly. Do your usual “#ifndef #define #endif” or “#pragma once” include guard inside the “stdafx.h” file, and include this file in EVERY single file inside your project. That’s right. Every single one of them. Now, rebuild your solution and it should be all good.

#pragma once
#include "stdafx.h"
#include <iostream>
// etc ...

Remember that “#include “stdafx.h” ” must appear before every #include in every file. Otherwise the preprocessor/compiler will happily ignore anything before it and throw you errors.

That’s all. Hope this little guide will help those that were lost and confused like me.  Here’s a couple links that will explain what Precompiled Header is to some extent. It’s best to do your own Google foo on this topic.

http://en.wikipedia.org/wiki/Precompiled_header

http://www.ogre3d.org/tikiwiki/tiki-index.php?page=precompiled+headers

http://msdn.microsoft.com/en-us/library/szfdksca(v=vs.71).aspx

Bye for now.

Advertisements

3 thoughts on “A quick guide to using Precompiled Headers (Visual Studio)

Something to Say?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s