views:

150

answers:

8

What are the best ways to speed up compilation time in Visual Studio 2005 for a solution containing mainly C++ projects?

+2  A: 

Precompiled headers can be helpful if you include complex headers (STL, Boost for example) directly in a lot of files.

Make sure your build does not access the network inadvertently, or intentionally.

Steve Townsend
+5  A: 

You can create a pre-compiled header of the files which normally wont change frequently. This can dramatically increase your compilation time.

Naveen
I never noticed this when I started using Visual Studio. I switched from Code::Blocks which seems to do it automagically for you.
GigaWatt
+5  A: 

Besides pre-compiling headers, there are number of other things that could be slowing you down:

  • Virus checking software - can have a nasty impact on builds. If you have a virus checker running try to turn it off and see what sort of improvement you get.
  • Low RAM - Not enough RAM will cause many more disc reads and slow you down. cont ->
  • Slow HDD - You have to write to disc regardless and a slow drive, like those present in many laptops and lower-end systems, will kill your builds. You can get a faster drive, a RAID array or SSD
  • Slow processor(s)... of course.
  • Unlikely, but: Check and see if your projects are referencing network shares. Having to pull files across the network with each build would a big slowdown.

EDIT Few more thoughts:

  • If your solution contains a large number of projects you could consider creating other "Sub" solutions that contain only the projects that you're actively working on. This possibility depends on how interrelated your projects are.
  • The project builds can have pre and post build event commands associated with them. Check the properties of your projects to see if there are any costly build events specified.
Paul Sasik
If you don't want to completely turn off virus checking, many scanners allow you to specify exceptions. Adding `*.c, *.cpp, *.h *.hpp, *.obj, *.lib, *.pch, *.ncb` to the exception list gets you 95% of the speedup, and still catches all common viruses.
MSalters
+2  A: 

If you have multicore cpu, use /MP compiler option.

Edit: What techniques can be used to speed up C++ compilation times?

barism
the /MP flag is only in VS2008 and VS2010. I seem to remember VS2005 supporting it in the beta stages, but the release doesn't as far as I know.
birryree
i use g++ so i'm not sure but as far as i know vs2005 also support this option but it's undocumented.
barism
I believe that VS2005 DOES support /MP but it's undocumented.
DeadMG
VS2005 will already perform concurrent compilations on MP machines, within the constraints of project dependencies.
Steve Townsend
A: 

If you have a lot of projects in the solution remove some of them. If they're referenced as projects you can reference the binary output instead (use the browse button in the add reference dialog). This removes a lot of dependency checking in the solution. It's not ideal but it does speed things up.

Jay
+3  A: 
  • Unity builds.
  • Precompiled headers.
  • Windows 7 builds a lot faster than Windows XP (if that's relevant to you).
  • Turn off anti-virus scanners.
  • Minimise dependencies (i.e. #include's from header files).
  • Minimise amount of template code.
Peter Alexander
+3  A: 

At the code level, it's helpful to minimize the amount of headers included by other headers. Repeatedly loading and reading files can have a big impact. Instead, forward declare things wherever possible. For example:

#include <iosfwd>
#include <string>
#include "M.h"
#include "R2.h"
#include "A2.h"

class M2;
class A;
class R;
template<class C> class T;

class X{
  M member; // definition of M is required
  M2 *member2; // forward declaration is sufficient for pointers & references
public:
  // forward declaration of argument type A is sufficient
  X(const A &arg);

  // definition required for most std templates and specializations
  X(const std::string &arg);

  // forward declaration of your own templates is usually okay...
  void f(T<int> t);

  // unless you're defining a new one. The complete definition
  // must be present in the header
  template<class Z>
  void whatever(){
    // blah blah
  }

  R f(); // forward declaration of return type R is sufficient

  // definitions of instantiated types R2 and A2 are required
  R2 g(A2 arg){
    return arg.h();
  }

  // ostream is forward-declared in iosfwd
  friend std::ostream &operator << (std::ostream &o, const X &x);
};

Users of this class will have to #include whatever files provide the class definitions if they actually call any of X(const A&), f(t<int>), f(), or operator <<.

Templates will almost certainly add to inclusion overhead. This is generally worth their power, in my opinion. When you make your own templates, the complete definition must reside in the header file; it cannot go in a cpp file for separate compilation. Standard templates can't be forward declared, because implementations are allowed to provide additional template parameters (with default arguments) and forward declarations of templates must list all of the template parameters. The iosfwd header forward-declares all of the standard stream templates and classes, among other things. Your own templates can be forward-declared as long as you don't instantiate any specializations.

Precompiled headers can also help, but most compilers limit the quantity that you can include in a source file (Visual Studio limits you to one), so they aren't a panacea.

Steve M
A: 

The solution we've used was IncrediBuild. Just throw more hardware at the problem. In addition to all the developer machines (fairly powerful themselves) we had a number of 4-core servers doing nothing but compiling.

MSalters