Developer forums (C::B DEVELOPMENT STRICTLY!) > Development

Precompiled headers (PCH) support in Code::Blocks

(1/8) > >>

mandrav:
After reading this post by tiwag, I sat down to try gcc precompiled headers once again. I had tried before, but I always stumped on a "Bad file descriptor" error when including the precompiled header...

Anyway, this time I had total success  :lol: 8)

I 'm commiting the changes soon. All *.cpp files have changed but don't be shocked! They were only changed to include the precompiled header ;)
OK.

The current CVS version is not aware of the PCH support, i.e. compiling the PCH from within C::B is not possible. So you 'll have to create the PCH manually. Here's how:

Create the file src/gen_pch.bat and paste the following in it:

--- Code: ---set WXBASEPATH=C:\Devel\wxWidgets-2.6.1

@rem
@rem SDK-used PCH
@rem
@echo Generating precompiled headers for SDK internal usage
@mingw32-g++.exe   -Wall -g -pipe -mthreads -fmessage-length=0 -fexceptions -DHAVE_W32API_H -D__WXMSW__ -DWXUSINGDLL -DcbDEBUG -DTIXML_USE_STL -DCB_PRECOMP -DWX_PRECOMP -DEXPORT_LIB -DEXPORT_EVENTS -Isdk -I%WXBASEPATH%\include -I%WXBASEPATH%\lib\gcc_dll\msw -I%WXBASEPATH%\lib\gcc_dllNonUnicode\msw -I%WXBASEPATH%\contrib\include -Isdk\wxscintilla\include  -IC:\MinGW\include -c sdk\sdk_precomp.h

@rem
@rem App and plugins used PCH
@rem
@echo Generating precompiled headers for plugins usage
@mingw32-g++.exe   -Wall -g -pipe -mthreads -fmessage-length=0 -fexceptions -DHAVE_W32API_H -D__WXMSW__ -DWXUSINGDLL -DcbDEBUG -DTIXML_USE_STL -DCB_PRECOMP -DWX_PRECOMP -DBUILDING_PLUGIN -Isdk -I%WXBASEPATH%\include -I%WXBASEPATH%\lib\gcc_dll\msw -I%WXBASEPATH%\lib\gcc_dllNonUnicode\msw -I%WXBASEPATH%\contrib\include -Isdk\wxscintilla\include  -IC:\MinGW\include -c sdk\sdk.h

--- End code ---
(you might have to edit the WXBASEPATH variable)

What? Two PCHs? That's right. We need two of them, because we have two different sets of compiler options: one for the sdk and one for everything else.

*EDIT* Run a 'cvs update' to update your sources.

Now run the batch file. It is important that it doesn't produce any errors. If it does, double check the include dirs etc.
When the PCHs are ready, fire up C::B, press "Build" and behold the wonder!
Full C::B compilation takes me now about 3:30, including tinyxml, wxscintilla and wxdockit. These targets do not use PCH so they 're compiled at the usual PCH-less speed.

The C::B version that is now built, supports gcc precompiled headers natively. This means that if you check the file properties of sdk/sdk.h and sd/sdk_precomp.h (the two PCHs) you 'll see they 're set to compile. If any change is made in any of these, the relevant PCH will be rebuilt automatically :)

Notes to C::B devs:
The files under sdk/ use sdk/sdk_precomp.h as a PCH. All other targets (i.e. the app and plugins) must use the sdk/sdk.h PCH. I 've updated all C::B targets to use the correct PCH.

Notes to everyone wishing to use PCH for their projects:
[*] Create a header file in your project and #include there all your rare-changing header files.
[*] In C::B, right-click this header file in the project manager and select "Properties".
[*] Click the "Compile" checkbox and make sure it's checked.
[*] In all C++ source files (not headers), add #include "yourpch.h" as the very first non-comment line.
[*] Click "Build" :)
[/list]
Worked like a charm for me. If you 're having problems, post them here and I 'll be glad to help you solve them ;)

I hope I haven't forgotten anything...
Yiannis.

mandrav:
I forgot to add that I have only updated codeblocks-wx2.6.0.cbp.
I will update the wx2.4 version later...

Ceniza:
Hmmmm, are you sure about the order?

Shouldn't the cvs checkout/update be ran before executing the BAT?

I just made a cvs checkout, ran the BAT and now I'm going to try it :)

thomas:
There is a way to do it without changing the .cpp files. I did that yesterday (although I think you need more than two .pch files. I created one for every target to be sure.).

Create the .pch files exactly as you did, but do not touch any of the .cpp files. Then add -include "sdk_precomp.h" to "other compiler options". Done.

The headers are all protected against recursive inclusion, so they're skipped immediately after the #ifdef. This is 99% as efficient, but does not require you to change a single line of code.
Compiles the whole thing in under 3 minutes. And... it is failsafe and backwards-compatible. If someone does not like precompiled headers, he can still compile normally :)

I have been thinking about a plugin that does this automatically for any kind of project, but haven't figured how to add an option to a project (have figured how to get to the commandline).

mandrav:

--- Quote from: ceniza ---Shouldn't the cvs checkout/update be ran before executing the BAT?
--- End quote ---

Nice catch ;)
(edited the post)

Navigation

[0] Message Index

[#] Next page

Go to full version