4

I currently have a machine with an Opteron 275 (2.2Ghz), which is a dual core CPU, and 4GB of RAM, along with a very fast hard drive. I find that when compiling even somewhat simple projects that use C++ templates (think boost, etc.), my compile times can take quite a while (minutes for small things, much longer for bigger projects). Unfortunately only one of the cores is pegged at 100%, so I know it's not the I/O, and it would seem that there is no way to take advantage of the other core for C++ compilation?

ApplePieIsGood
  • 3,731
  • 6
  • 35
  • 51
  • Compiling C++ code is hard, and not very easy to make parallel, especially during the linking stage. I think it was jalf who made a good list of why compiling can be slow, let's see if it can be found. What are you using to compile? And which parts of boost? – GManNickG Mar 18 '10 at 03:11
  • 1
    That was easy, it's his most up voted answer: http://stackoverflow.com/questions/318398/why-does-c-compilation-take-so-long/318440#318440 – GManNickG Mar 18 '10 at 03:12
  • Some compilers, read IDE/compile system, allow to compile files in parallel. That might utilize your dual core system more. An example is GNU make in linux/unix, it can compile files in parallel. – jpyllman Mar 18 '10 at 03:19
  • I'm using VisualStudio 2008 and 2010. I could use the Intel compiler if it would make a difference. Suppose I could use GCC in VS.NET as well if it would really help, though I know little about it. – ApplePieIsGood Mar 18 '10 at 03:25
  • The intel compiler is a lot of things, but it is not fast. – Alan Mar 18 '10 at 05:17

3 Answers3

7

Are you using pre-compiled headers? They typically provide the biggest compilation speed boost I get with my C++ projects.

Also, depending on your compiler, you can enable multi-thread compiling. For example, with Visual C++, it's the /MP switch (see here for details), though enabling /MP isn't always possible, depending on what other command-line options you use.

Dean Harding
  • 71,468
  • 13
  • 145
  • 180
4

Compile time problems with templates are often link problems, rather than compilation problems.

Using templates internally in your .cpp files, but making sure that the headers don't actually include the template, is a good way to fix those. That can be done by either forward declaring the class, or wrapping your implementation class in an abstract base class that just declares the public members (the Pimpl Idiom, basically).

kyoryu
  • 12,848
  • 2
  • 29
  • 33
1

For taking advantage of multi-thread compiling with Makefile-based systems, take a look at the -j switch, the usual recommendation is to invoke

make -j<number of cores + 1>
Ramon Zarazua B.
  • 7,195
  • 4
  • 22
  • 26