Page 1 of 1

C++ Precompiled Headers in VS2002

Posted: Mon Jan 26, 2009 9:16 pm
by Disch
Hey. I'm completely new to the whole precompiled header thing. Up until now I haven't been using them, and have just been enduring the compile times. Now I'm interested in trying out precompiled headers to see if they can cut down on compile time.

I checked the wiki and available documentation, and unless I'm misunderstanding... the only thing I have to do is this:

Code: Select all

#include "wx/wxprec.h"

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
Is that it? I'm doing that and not noticing a .pch file anywhere in my project. Compile time doesn't seem any shorter, either (I'm doing this in a virtually empty project for testing). Is there some compiler settings I need to set up? Do I need to compile wx with some options?

Any help appreciated.

Posted: Wed Feb 04, 2009 4:36 pm
by rodrigod
AFAIK the compile time will only make a difference if you have a project with many files that use the precompiled header.

I use that precompiled header also and you can find it in your intermediate files folder. It is by default in the DEBUG or RELEASE folder under the project folder.

Posted: Thu Feb 05, 2009 6:51 pm
by Disch
rodrigod wrote:I use that precompiled header also and you can find it in your intermediate files folder. It is by default in the DEBUG or RELEASE folder under the project folder.
I just checked again to be sure and there are no pch files in any of my project's folders. Do you mean *my* project folder? Or the wx project folder?

A full scan of my HD relvealed several .pch files in \build\msw\vc_msw*\. Could these be the pch files used? I find that somewhat unlikely.

Posted: Fri Feb 06, 2009 1:26 pm
by rodrigod
How did you create the precompiled header? Did you just use automatic? Have you already compiled your project?

It's better to set your .cpp files to use the precompiled header you want "example.h" and set "example.cpp" to create. you do this on the project settings.

And by your project folder I meant your current project folder not wx project folder.

Posted: Fri Feb 06, 2009 3:48 pm
by Disch
rodrigod wrote:How did you create the precompiled header? Did you just use automatic? Have you already compiled your project?
Well I guess this is my question. I didn't create the precompiled header. I'm totally new to this PCH stuff and I haven't been able to find any useful info on it in the wiki here or anything. I posted everything I know about the process to get a precompiled header working in my first post in this thread.
It's better to set your .cpp files to use the precompiled header you want "example.h" and set "example.cpp" to create. you do this on the project settings.
Okay -- this is what I was missing. I see this option in the project settings now. Tinkered with it a bit to generate then use the pch file -- and now I got it working. Compile time cut down dramatically! Excellent.

Although I'm not sure I understand the need for "example.cpp" here. It looks like I create the pch file from just a single .h file. I don't see the need to tie that .h file to a .cpp file. Is there a reason I should do this? Seems unnecessary to me.

Also it appears as though I have to include the header file in every source file in my project if I do this. Even in files which don't use wx. Failure to include results in an "unexpected end of file while looking for pch directive" error. Is there any way to avoid this? If someone tries to compile this without pch I'd hate for wx to be included in tons of files it doesn't need to be in.

Posted: Sun Feb 08, 2009 5:25 pm
by rodrigod
example.cpp would be just like this

Code: Select all

#include "example.h"
It is set to create the pch. At least it is how I do it, copied it from microsoft when it creates a project with mfc automatically.

If you dont want a file to use the pch you go to it's setting and set it not to use precompiled header

Posted: Sun Feb 08, 2009 9:41 pm
by Disch
Yeah I don't see the point to having that example.cpp. Seems like an extra needless file -- the PCH is created and included fine without it... all you need is to have any file in the project include the header (at least from what I've been able to tell).

Blech @ per-file settings, but I guess that'll do. I wonder how much all of this complicates things if you try to migrate to another compiler like g++.

Thanks a million, rodrigod! I appreciate the help!