Archived

This topic is now archived and is closed to further replies.

PCH -- newbie question

This topic is 5650 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m not sure how exactly a PCH works, though I have read some things on MSDN (probably not enough, though) about PCH files, my main question is are they necessary? If they aren''t is the main benefit reduced compile time? What is needed to create a PreCompiled header anyway? I''m still looking for the answers, but if anybody could dial it down a notch from MSDN for a rookie, I would appreciate it. Thanks Luke

Share this post


Link to post
Share on other sites
quote:
Original post by Lucidquiet
I'm not sure how exactly a PCH works, though I have read some things on MSDN (probably not enough, though) about PCH files, my main question is are they necessary?


No. You can live without them. And many people in fact do.
quote:

If they aren't is the main benefit reduced compile time?


That is probably the only benefit, but it is very significant. Once you start including a bunch of stuff, like any of those DX headers, it will take a while to compile the headers alone. If you have any non-tiny project, the difference between using and not using PCH compile time-wise will be very noticeable.
quote:

What is needed to create a PreCompiled header anyway?


First, create two files: stdafx.h and stdafx.cpp. Put all your standard includes in stdafx.h (even if not all .cpp files use them), and make sure all your .cpp files include stdafx.h as the first non-comment line. Then, go to project settings, C++, precompiled headers page. For stdafx.cpp, select "create precompiled header file" and enter stdafx.h as the header. For all other files, select "use precompiled header file" and enter stdafx.h as the header again. Enjoy faster build times. Of course, you can replace stdafx.h/cpp with any other names to your liking.

[edited by - IndirectX on June 26, 2002 7:12:17 PM]

Share this post


Link to post
Share on other sites
.. and be careful when you use the method StdAfx.pch. Sometimes you got lots of errors saying undefined types or re-defined types when you add few more includes into your .cpp/.h. Just ReBuild All will do the trick.

For me, I would prefer having just a include (without StdAfx.cpp), say StdAfx.h, and then in every top of .cpp files, I would do this:

#include "stdafx.h"

#pragma hdrstop

#include <iostream> // just example, usually I put this under stdafx.h
#include "someother.h"


So even when I move to other compiler (e.g. Borland), I still have the advantages of pre-compiled header.

Share this post


Link to post
Share on other sites
It cut my compile times from 5 mins to about 1.5 mins for my ~600 source files. All thats in my PCH file is this:


  
#pragma once
#ifndef STDAFX_H
#define STDAFX_H

#include <string>
#include <list>
#include <vector>
#include <map>
#include <algorithm>
#include <sstream>
#include <stdexcept>

#endif //STDAFX_H



...and I don''t even think the inclusion guard is even necessary as I only include stdafx at the beginning of each .cpp file.

Personally I''d suggest only putting library type code in to the pch file, STL in particular. You can play with optimisations, like if you have some other common code you always use but never change I guess that could be put in there as well. I don''t do that as if I did actually change one of those files it would force a recompile of everything in the project.

For a small project don''t bother, I mean who cares if you get an addition 1 second off the compile time of a 3 file project...

HTH



Chris Brodie
http:\\fourth.flipcode.com

Share this post


Link to post
Share on other sites
quote:
Original post by IndirectX
First, create two files: stdafx.h and stdafx.cpp. Put all your standard includes in stdafx.h (even if not all .cpp files use them), and make sure all your .cpp files include stdafx.h as the first non-comment line. Then, go to project settings, C++, precompiled headers page. For stdafx.cpp, select "create precompiled header file" and enter stdafx.h as the header. For all other files, select "use precompiled header file" and enter stdafx.h as the header again. Enjoy faster build times. Of course, you can replace stdafx.h/cpp with any other names to your liking.





I LOVE YOU. I''ve been looking for a clear cut explanation like this for months.

THANK YOU.

Share this post


Link to post
Share on other sites