views:

3672

answers:

6

Quick question: what is the compiler flag to allow g++ to spawn multiple instances of itself in order to compile large projects quicker (for example 4 source files at a time for a multi-core CPU)?

Many thanks.

+4  A: 

make will do this for you. Investigate the -j and -l switches in the man page. I don't think g++ is parallelizable.

rmeador
+2  A: 

I'm not sure about g++, but if you're using GNU Make then "make -j N" (where N is the number of threads make can create) will allow make to run multple g++ jobs at the same time (so long as the files do not depend on each other).

Andy
+7  A: 

There is no such flag, and having one runs against the Unix philosophy of having each tool perform just one function and perform it well. Spawning compiler processes is conceptually the job of the build system. What you are probably looking for is the -j (jobs) flag to GNU make, a la

make -j4

Or you can use pmake or similar parallel make systems.

Mihai Limbășan
+18  A: 

You can do this with make - with gnu make it is the -j flag (this will also help on a uniprocessor machine).

For example if you want 4 parallel jobs from make:

make -j 4

You can also run gcc in a pipe with

gcc -pipe

This will pipeline the compile stages, which will also help keep the cores busy.

If you have additional machines available too, you might check out distcc, which will farm compiles out to those as well.

frankodwyer
You're -j number should be 1.5x the number of cores you have.
Mark Beckwith
yes, something like that makes sense given there is I/O as well - although may need some tuning if using -pipe as well
frankodwyer
A: 

I see. I'm building my project from a SlickEdit solution which is building it without a makefile (managing it itself). If anyone knows of how to make SlickEdit have this functionality without using make please let me know. Otherwise I'll probably switch to having it use a makefile.

Thanks to all for the quick answers!

bsofman
+3  A: 

People have mentioned make but bjam also supports a similar concept. Using bjam -jx instructs bjam to build up to x concurrent commands.

We use the same build scripts on Windows and Linux and using this option halves our build times on both platforms. Nice.

MattyT