Building a simple "hello world" Ogre application can take several seconds on a modern machine. Even when you haven't added your own code yet. 
This waste of time

  • breaks your workflow
  • makes experimentation difficult
  • is unnecessary

The solution: use precompiled headers in Visual Studio. 
On an average laptop computer (WinXP, VC++2008 express, Core2Duo 1.5GHz, 2GB RAM) the build time for the Ogre Basic Tutorial 1 goes from 20 seconds, to 1 second with precompiled headers. So that's 20 times faster!  

What are precompiled headers?

We won't explain here what precompiled headers are exactly, there are better places for that on the net.

Just know that precompiled headers are most useful when your project has lots of large includes to process. It is the case with Ogre. You tell Visual Studio which included headers rarely or never change, then Visual Studio will pre-compile them once, will save them in a file in your project folder, with a file extension of .pch (guess why ), and will use this temporary output everytime you compile your project.

There will be no speed benefit when the headers are being pre-compiled, that is when you first compile your project, or when one of the headers to pre-compile has been changed (which should rarely or never happen), but after that, the gain is generally huge.

The good thing is, if one of the headers that have been pre-compiled change, Visual Studio will recreate the .pch file automatically for you. So once setup, precompiled headers won't get in your way.

How to setup precompiled headers

Setting up precompiled headers is easy and takes only a few steps, especially if you're just starting a new project.

Create stdafx.h

stdafx.h is the header file where you include all those headers you need in your project that rarely or never change. These could be Ogre headers, or anything else you want. The name of this header file doesn't matter, but by convention we name it stdafx.h.

// stdafx.h
 
// Ogre headers you need
#include <OgreException.h>
#include <OgreRoot.h>
#include <OgreConfigFile.h>
#include <OgreCamera.h>
#include <OgreViewport.h>
#include <OgreSceneManager.h>
#include <OgreRenderWindow.h>
#include <OgreEntity.h>
#include <OgreWindowEventUtilities.h>
 
// any other header can be included, as usual
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif

Create stdafx.cpp

Then, you need the associated .cpp file. 
Create stdafx.cpp in your project. Again the name could be anything, but it is always best to follow conventions. 
You can put anything you want in your stdafx.cpp, but the only requirement is to include stdafx.h as the first statement.

// stdafx.cpp
#include "stdafx.h"
 
// anything you want after that

Include stdafx.h in every file of your project

Include stdafx.h

  • as the first statement
  • in ALL your .h and .cpp files

With large projects where you have hundreds of files, use some scripting, for example Python, to make sure this is done automatically for you.

In visual studio, you can do the following (make sure you select "All Configurations"):

Configuration Properties -> C/C++ -> Advanced 
Type "stdafx.h" in the "Force Include File" field; this will make the visual studio compiler insert the header file at the beginning of each file without having to include it manually.

Configure Visual Studio to use precompiled headers

Go to your project settings, make sure you select "All Configurations", then go to 
Configuration Properties -> C/C++ -> Precompiled Headers, 
and choose "Use Precompiled Header". 
Make sure stdafx.h is the header file to be used, and leave the rest to the default value. 
 
So now, Visual Studio will try to use precompiled headers the next time you build your project. But it won't work, since we don't have the precompiled header (the .pch file) yet. So let's tell Visual Studio to create it for us.

Configure Visual Studio to create precompiled headers

Finally, in the solution explorer, right-click on stdafx.cpp -> properties 
choose "All Configurations", 
then go to C/C++ -> Precompiled Headers, 
choose "Create Precompiled Header", make sure stdafx.h is the header file to use, and leave the rest to the default value. 
Click "OK".

We've just told Visual Studio to create the precompiled header when compiling stdafx.cpp. 
The precompiled header will be (re)created with the next build:

  • if it does not exist yet
  • when one of the headers to precompile has been modified
  • when stdafx.cpp has been modified

Conclusion

Re-build your project. You shouldn't notice any difference since the precompiled header is first generated. It might even be slower than usual. 
So try making a small change to one of your source files (for example add a space or semi colon in the main function), then build again: 
enjoy the performance boost! 

VC创建预编译文件的更多相关文章

  1. Xcode6之后创建Pch预编译文件

    在Xcode6之前,创建一个新工程xcode会在Supporting files文件夹下面自动创建一个“工程名-Prefix.pch”文件,也是一个头文件,pch头文件的内容能被项目中的其他所有源文件 ...

  2. vs2008 vc90.pdb 不是创建此预编译头时使用的 pdb 文件,请重新创建预编译头

    解决方案: 找到项目中的stdafx.cpp,右键属性,找到C/C++->预编译头, 设置为创建预编译头, 重新生成

  3. 创建预编译头 Debug 正常 Release Link Error:预编译头已存在,使用第一个 PCH

    创建预编译头 Debug 正常 Release Link Error Main.obj : error LNK2005: ___@@_PchSym_@00@UmfilkilqUdrmzkkUkilqU ...

  4. VC++ 使用预编译头

    一.使用默认的预编译头       要使用预编译头,我们必须指定一个头文件,这个头文件包含我们不会经常改变的代码和其他的头文件,然后我们用这个头文件来生成一个预编译头文件(.pch文件),想必大家都知 ...

  5. vs2010 创建预编译头 Debug 正常 Release Link Error问题解决

    问题:创建预编译头 Debug 正常 Release Link Error Main.obj : error LNK2005: ___@@_PchSym_@00@UmfilkilqUdrmzkkUki ...

  6. 怎么向Xcode6 IOS8之后向项目中添加预编译文件

    苹果的XCode在6版本之后新建项目时取消了自动创建预编译头文件pch,该文件里存放的工程中一些不常被修改的代码,比如常用的框架头文件,这样做的目的提高编译器编译速度.我们可以往里面加入一些项目中都要 ...

  7. 问题处理:找不到Pch预编译文件?

    提醒:Xcode6之后就不再自动创建Pch预编译文件 在Xcode6之前,创建一个新工程xcode会在Supporting files文件夹下面自动创建一个“工程名-Prefix.pch”文件,也是一 ...

  8. iOS开发之Xcode6 之手动添加Pch预编译文件

    参考文档 http://blog.csdn.net/crazyzhang1990/article/details/44243343 红色部分为本人自己补充注意事项 在Xcode6之前,创建一个新工程x ...

  9. iOS xcode6添加预编译文件

    在xcode6以后,由于苹果不建议开发者乱用预编译文件,所以,在项目创建之后 就不会自动生成预编译文件. 那么如果我们想要使用预编译文件,就需要自己动手来添加.那到底该如何为我们的项目添加预编译文件呢 ...

随机推荐

  1. Android 字体和颜色

        对于能够显示文字的控件(如TextView EditText RadioButton Button CheckBox Chronometer等等),你有时需要控制字体的大小.Android平台 ...

  2. 在VMware上面安装Solaris 10

    导读 Oracle Solaris 11 是世界上最先进的企业操作系统,提供安全.速度.简单的企业云环境和DevOps.在这篇文章中我们将使用Solaris 10版本,但您可以按照同样的步骤,来安装刚 ...

  3. 基于贪心算法的几类区间覆盖问题 nyoj 12喷水装置(二) nyoj 14会场安排问题

    1)区间完全覆盖问题 问题描述:给定一个长度为m的区间,再给出n条线段的起点和终点(注意这里是闭区间),求最少使用多少条线段可以将整个区间完全覆盖 样例: 区间长度8,可选的覆盖线段[2,6],[1, ...

  4. python 下载文件 & 防盗链

    偶然下载一种类型的资源,发现好多翻页,右键另存什么的,不胜其烦. 决定用python写几句代码搞定.核心代码如下: from urllib import urlretrieve from urllib ...

  5. 【持续集成】[Jenkins]Job中如何传递自定义变量

    [Jenkins]Job中如何传递自定义变量 来自dweiwei   2015-06-27 18:37:19|  分类: 自动化测试 |举报 |字号大中小 订阅 用微信  “扫一扫” 将文章分享到朋友 ...

  6. poj 1833

    http://poj.org/problem?id=1833 next_permutation这个函数是用来全排列的,按字典的序进行排列,当排列无后继的最大值时,会执行字典升序排列,相当于排序: 当排 ...

  7. Greedy:Fence Repair(POJ 3252)

    Fence Repair 问题大意:农夫约翰为了修理栅栏,要将一块很长的木块切割成N块,准备切成的木板的长度为L1,L2...LN,未切割前的木板的长度恰好为切割后木板的长度的总和,每次切断木板的时候 ...

  8. CodeForces - 426B(对称图形)

    Sereja and Mirroring Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64 ...

  9. Codeforces 424C(异或)

    Magic Formulas Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Subm ...

  10. [Android Pro] 监听WIFI 打开广播

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-perm ...