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.

[code language=”cpp”]
#pragma once
#include "stdafx.h"
#include <iostream>
// etc …
[/code]

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.

Qt Creator color scheme (MSVS2012 style!)

Hello minions!

[A little note first]. It seems it has taken forever for me to publish this post. In actual fact, it was actually finished back many moons ago. What happened was that, for some god forsaken reason, there appeared to have been a roll back. A little while after I published the article, the article was unpublished and the saved drafts rolled back to when I was only half way through writing this. This totally put me off, and so I basically went away from WordPress for some time, until now.

Any how, lets move on to the actual stuff.

Ever since I got my hands on the new Microsoft Visual Studio 2012 and all it’s goodies and junks, I’ve fallen in love with one thing. It’s dark theme!

vs2012_colorTheme_example

It’s the next best thing for those who have super-uber-sensitive eyes. Besides chicken cooked in any possible yummy ways.

vs2012_colorTheme_option its enabled here.

Now. Along side C++, I’m also a big fan of the Qt framework (its pronounced ‘cute’ apparently, but I like to call it Q.T.), and it’s ways, and I’ve been learning it with the book “An Introduction to Design Patterns in C++ with Qt” 2nd ed, by Ezust. A great read to add to my newbie C++ and design patterns knowledge, alongside the obvious Qt materials.

Anyway, Qt comes with its own full feature, cross platform IDE, the Qt Creator (wiki link). It natively integrates with the Qt framework and I’ve been using it the past couple weeks.

Moving back on topic. The default color schemes for the text editor’s syntax highlighting are pretty ugly if you ask me. Thanks to my mega laziness, I’ve never bothered to fiddle with it, right until a couple days when I’ve finally bite the bullet, and decided to have a go at it!

qt_option setting the individual colour values

Screen Shot 2013-02-13 at 8.22.04 PMvs2012_colorTheme_example ta da, the final product!

Now, here comes the hardest part. locating where the color scheme file is placed/stored was a massive pain in the buttock, especially on the Mac.
Windows users  can find theirs in:
Users\<user name>\AppData\Roaming\QtProject\qtcreator\styles

It’s a bit trickier for Mac users. You would  have to first tell finder to show all the hidden files. Then you can go to here:
/<usr/mac name>/.config/QtProject/qtcreator/styles

There’s a couple tutorial that you can find on Google on how to get finder to show hidden files, so i won’t post it here.

Now of course, if you look closely and compare it with VS2012’s dark theme, they are not 100% the same. Beside the different ways the 2 IDE’s identify different syntax elements, I’ve added some personal touch to give more highlight and avoided as much white as possible.

At last, here’s the link to the final XML file for the color scheme: [click me!]

Ciao now.

[UPDATE: 2014/08/23]

  • primitive type colour has been set to blue as it is in MSVS.
  • doxygen comment colour has been set to the same green as regular comments but a little lighter.