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.
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.
Now select all source files (your .cpp files) in the Solution Explorer, right click and hit Properties. The Property Page should pop up.
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).
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.
// 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.
Bye for now.