views:

63

answers:

2

I want to speed up the build time of my c++ project, and I am wondering if my current structure may cause unnecessary recompilations.

I have *.cc and corresponding *.h files, but all my *.cc files include a single header file which is main.h.

In main.h, I include everything necessary and extern global variables and declare the functions I use. Basically, I'm not using any namespaces.

Is this a bad design that could cause unnecessary recompiles and slow builds?

+4  A: 

It depends. If main.h is seldom modified, you could use precompiled headers, which will greatly improve compilation time. On the other hand, if main.h is regularly used, it's probably not a good design.

An additional problem introduced by putting everything in one include file is that it doesn't really promote structure in your application. In well-designed applications you often have a layered structure. By putting everything in one include file, you obfuscate the structure in your application. This may work for a small application, but if your application grows, you will end up one day with a complete spaghetti, where everything depends on everything else.

Try to split the include file in multiple parts. Typically you will have one .cpp and one .h file per class. Try to use forward declarations as much as possible in your include file, and only include (in .h and .cpp) what's really needed.

Patrick
+1  A: 

That design will definitely lead to slow build time. What make files and IDEs do when you start a build is they check which source (cc) files have been modified since the last time you compiled. It also checks whether any files that a source file depends on have been modified. A source file depends on all the header files it includes, and all the header files those header files include, etc. If it detects any modifications then it recompiles that source file.

Since your set up means that each source files includes every single header file, any time you modify even a single header file you need to recompile every source file.

You'll definitely want to try and separate things a bit more and get rid of your main.h file. Usually people try and minimize the number of header files included in a header file and prefer to keep the includes in source files, by the way.

Niki Yoshiuchi