# How to reduce time of compilation?

## Recommended Posts

Hello! I use C++ compiler from VS2003. I would like to ask you for some suggestions about reducing time of project compliation. Currently I am working on a bigger project (contains > 30 files) and it's making me terribly annoyed when I have to wait a couple of MINUTES until everything rebuilds. Is there any chance of speeding things up? OK, I understand that when I change a single header file it needs to be pre-compiled but why to rebuild the whole projects? If I simply SHIFT+F5 I get linking errors. Are there any settings in the compiler that might be helpful? Thanks in advance for your help, See ya'

##### Share on other sites
Minutes?

I can do a full build of my 21,000 lines of C++ code in about one minute on my P4 2.53ghz, and it uses Boost and STL in more than a few places, along with some of my own template classes.

If said header file is referenced in every header, then changing that file will have a ripple effect on compilation times. The contents of a precompiled header shouldn't change much after first setting it up, you'll gain the time back eventually. If you can, get a copy of John Lakos' Large-Scale C++ Software Design. It is probably one of the only books that covers physical layout of a project in great detail.

##### Share on other sites
Make sure your header files have specific, preferably fairly small groups of information of a common concept, and only include the files that you need. Be sure that each of your files includes the bare minimum required for functionality and nothing more. As well, remember that you don't need to include the full definition of a type if you are just working with it indirectly, so you don't necessarily have to include the file with the definition of whatever you are working with, you can often just forward declare I.E. when working with pointers or references to a class-type or when the class type only appears in a declared function's signature.

This is important because each translation unit (cpp file) parses through the files on their own, so if your cpp files include more headers than are needed, you have to remember that not only are you needlessly parsing through the data, but you are doing so in every single cpp that uses your files, whether directly or indirectly. If your headers include more information than are needed, your compile time is going to be needlessly inflated proportional to the number of translation units in which your files are included.

Another alternative way of decreasing compilation times is to use pre-compiled header files, though that's not really standard.

##### Share on other sites
- use pre-compiled headers (only put stuff that rarely changes in there);
- only include the headers you need;
- switch off antivirus software (or at least stop it from scanning your source- and objectfiles).
- make sure the code you change is in a cpp file (not a header) ;)

##### Share on other sites
use interfaces to sheild your code from the effects of implementation changes.

Its really helped us. Its also promotes loose coupling which can reduce your link times.

Cheers
Chris

##### Share on other sites
Get a new computer really. One of my projects with hundreds of files and tens of thousands of lines builds in under a minute.

Programmatically just make sure you don't have to rebuild everything every time. Better code division can make that happen easily; or the simplest solution is above (even just getting more RAM if you have very little or getting a better processor can make a big difference).

##### Share on other sites
i thought using makefiles helped?

##### Share on other sites
Couple of snippets from GoTW:

Compile time dependencies
The Pimpl idiom

HTH,
Jim.

##### Share on other sites
You can also try the digital mars compiler, its very very fast at compiling things. I find it especially usefull for when I'm just running the compile-every-5-minutes-to-test-for-mistakes thing.

##### Share on other sites
Also be sure to turn off any anti-virus software you may be running.

##### Share on other sites
Hey guys! Great thanks for your replies! :)

Quote:
 Original post by antareusMinutes?

Yeah, it happens sometimes. :/

Quote:

Hehe. Well, my situation is as follows - every cpp unit includes only a header corresponding to that unit BUT the header includes "Main.h" which actually includes all headers in the project. That's bad I guess ?

Quote:
 - use pre-compiled headers (only put stuff that rarely changes in there)

Could you tell me how can I do this? :)

Quote:
 - switch off antivirus software (or at least stop it from scanning your source- and objectfiles).

I've got only firewall running. Can it stay or should I switch it off ?

Quote:
 use interfaces to sheild your code from the effects of implementation changes.Its really helped us. Its also promotes loose coupling which can reduce your link times.

Frankly speaking, I have no idea what are these 'interfaces' you are talking about. Anyway, it seems it's really useful since I have a felling that my headers are being compiled a couple of times during one compilation.

Quote:
 Get a new computer really.

Well, it isn't that bad. For my purposes it's enough. Athlon 1200 256 RAM ...

+ I'll check links you gave me. :)

Thanks once again. :)

##### Share on other sites
Quote:
 the header includes "Main.h" which actually includes all headers in the project. That's bad I guess ?

Yes. Whenever you change ANY header, EVERY file needs to be recompiled, because they (indirectly) depend on every header.

The rule should be that headers should only include some base header which does not include other headers. The base header sets up basic typedefs and defines. The header itself forward declares what it needs (i e "class IFoo;" if it takes an IFoo argument). You also need to include headers for your base classes, if you use implementation inheritance (which is bad, because it causes tighter coupling).

C++ files should include all the headers necessary to actually compile the file, and no more.

##### Share on other sites
Quote:
Original post by clapton
Quote:
 Original post by antareusMinutes?
Yeah, it happens sometimes. :/
I wonder if his point was that some compiles take hours, or even days.

##### Share on other sites
Your computer is ancient; you can get a new CPU online that will fit your socket for under \$50 if I remember right (like an 1800 or something). The RAM seems to be the big issue because that's the minimum recommended for Windows alone, and compiling takes a LOT of RAM.

##### Share on other sites
Your problem of everything including Main.h, which then includes everything.... yeah that's really bad. :)

Like REALLY bad. It's something that a faster computer won't fix.

Pre-compiled Headers are a blessing from God. It's basically a header file that contains all your headers that only change rarely. And then all .cpp files include the PCH and whatever other .h files they need to compile. Then, the PCH gets compiled and re-used for every .cpp file. The caveat is that if you change any of the files included from the PCH, then your whole project has to be rebuilt (which essentially what you're doing now with Main.h as your PCH that isn't).

Remember, it's NOT bad to have a .cpp file include a whole bunch of headers. It IS bad (but necessary, in many cases) to have a .h file include other .h files. and it IS bad to put .h files in your PCH that change often.

Then, delete Main.h and eliminate it from all your headers (you can do this in one shot with a replace-in-files).

Then #include your PCH from the very top of each .cpp file (it has to be the first thing).

Then, for each header, #include the other headers it NEEDS (i.e. you need to include it if your object subclasses another object in a different header. Or if it makes a function call of an object declared in another header. If all you have is a function declaration or member variable that is a pointer to a type of object declared in another header, you can just forward declare the type).

Example:
#ifndef _FOO_H_#define _FOO_H_#include "Foo_super.h"    // subclass must include the super#include "Baz.h"          // we use an instance of Baz as memberclass Bar;   // pointers can be forward declared, which is good.class Foo : public Foo_super{    Bar* m_pBar;    Baz  m_Baz;};#endif

Then, go through and #include headers in your .cpp files until everything compiles. Expect it to take a whole day, but you'll definitely save time in the long run. Remember to forward declare things if possible, or #include them from .cpp files... but "use of undefined type" means that you need to #include the .h with the definition of the type.

##### Share on other sites
PCH's are great. Just remember that on vs.net the pch isn't automatically regenerated when one of its headers changes. This is different from what we see with OS X's XCode environment.

Cheers
Chris

##### Share on other sites
Quote:
Original post by Oluseyi
Quote:
Original post by clapton
Quote:
 Original post by antareusMinutes?
Yeah, it happens sometimes. :/
I wonder if his point was that some compiles take hours, or even days.

I just leave the computer working for a night. When I wake up it is rebuild (assuming there were no errors). <just kiddin'>

Quote:

Ow, come on! Considering requirements for my game it's a lot more than I need to have. :P

Quote:
 Pre-compiled Headers are a blessing from God.

Thanks ajas95 for sharing your knowledge. :) Well, I see that this PCH thing is a little more complicated than I thought. I will need to read your post once again (...very slowly). I guess that the abbility of using PCH is quite important. :)

Thank you all !

##### Share on other sites
Actually, if he only has thirty files, then his compile shouldn't take several minutes. But there are more than a few systems where compiles can take days to run. Good thing I haven't run into them yet professionally.

##### Share on other sites
There is something I've hidden from you (actually I forgot about it). Here is a part of buildlog :

Compiling...CYawSystem.cppf:\microsoft visual studio .net 2003\vc7\include\dx\dinput.h: DIRECTINPUT_VERSION undefined. Defaulting to version 0x0800f:\Microsoft Visual Studio .NET 2003\Vc7\include\FMOD\FMOD.H(35) : warning C4005: 'DLL_API' : macro redefinition        f:\Microsoft Visual Studio .NET 2003\Vc7\include\FreeImage.h(68) : see previous definition of 'DLL_API'CStatusBar.cppf:\microsoft visual studio .net 2003\vc7\include\dx\dinput.h: DIRECTINPUT_VERSION undefined. Defaulting to version 0x0800f:\Microsoft Visual Studio .NET 2003\Vc7\include\FMOD\FMOD.H(35) : warning C4005: 'DLL_API' : macro redefinition        f:\Microsoft Visual Studio .NET 2003\Vc7\include\FreeImage.h(68) : see previous definition of 'DLL_API'(...for each cpp unit...)

As you've noticed there is something strange. It seems that dxinput.h and fmod.h are being rebuilt for each cpp file. Maybe that's the cause... ? :P

Thx :)

##### Share on other sites
Use incredibuild - compare it with a renderfarm, where every computer renders one screen - but in this case all computers
on the network help compiling...

It's very cool - at work we are compiling our game with
23Ghz - goes very nice and quick, 3 mins instead of 15+
(actually never tried to compile it without it, so I don't
know how long it really takes :)

##### Share on other sites

You even don't have to have a compiler installed on
the client-computer, only the little incredibuild
program... So it's very nice to take cpu-power from
the graphics artists, which aren't rendering anything :)

And you can set it up so it doesn't try to use a client
if that clients is doing sth processor-intensive :)

take a look here: www.xoreax.com

Regards
Roger

##### Share on other sites
Quote:
 Original post by Kitt3ntake a look here: www.xoreax.com

Actually it's not what I am looking for but ... WOOOOOW ! :O Awesome! Totally rocks! I didn't know that such things exist! :D

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627711
• Total Posts
2978761

• 21
• 14
• 12
• 41