3

I have a c++ program which has many many functions and I have different .cpp files for each of the function. From the main program, I only supply a few parameters and just call the functions. However, the compilation of the full thing takes a lot of time. For each compilation I only change a few parameters in the main program and leave all the functions as it is. Is there anyway to speed up the compilation.?

lovespeed
  • 4,835
  • 15
  • 41
  • 54
  • 2
    What is your command line to compile? Are you using Makefile or some other build tool? – RedX Jul 02 '12 at 13:20
  • There may be some platform/compiler specific changes that could be suggested, but you don't specify what you are using. – crashmstr Jul 02 '12 at 13:20
  • What compiler are you using? You'll probably get more targeted answers if you add that detail. – Eran Jul 02 '12 at 13:20
  • I have a compile.sh file which has g++ -o3 mainprog.cpp func1.cpp func2.cpp ..... -o output.out and everytime I just run the compile.sh from the commandline – lovespeed Jul 02 '12 at 13:20
  • 2
    If you are using gcc, consider precompiled headers: http://stackoverflow.com/questions/58841/precompiled-headers-with-gcc Also as others have mentioned use make. And then run it with multiple threads: make -j N – anio Jul 02 '12 at 13:22

3 Answers3

6

You are recompiling unnecessary code. Usually IDEs handle this automatically. Otherwise, it depends on how you compile your code. For example lines like this:

g++ *.cpp

or

g++ -o program a.cpp b.cpp c.cpp

are terribly slow, because on every compilation, you recompile everything.

If you are writing Makefiles, you should carefully write it to avoid recompilation. For example:

.PHONY: all
all: program

program: a.o b.o c.o
    g++ -o $@ $^ $(LDFLAGS)
%.o: %.cpp
    g++ $(CXXFLAGS) -o $@ $<
# other dependencies:
a.o: a.h
b.o: b.h a.h
c.o: c.h

In the above example, changing c.cpp causes compilation of c.cpp and linking of the program. Changing a.h causes compilation of a.o and b.o and linking of the program. That is, on each build, you compile the minimum number of files possible to make the program up-to-date.

Side note: be careful when writing Makefiles. If you miss a dependency, you will may not compile enough files and you may end up getting hard-to-spot segmentation faults (at best). Take a look also at the manual of gcc for -M* options where you can use gcc itself to generate the dependencies and then include the generated output in the Makefile.

Shahbaz
  • 46,337
  • 19
  • 116
  • 182
  • yes, I understand that I am un-necessarily recompiling all the function files. I only change a few values in the main program. I have never written a make file, it will be very helpful if you can tell me how to write one, thank you. – lovespeed Jul 02 '12 at 13:30
  • @SthitadhiRoy, just trying searching "Makefile tutorial" and you will get many sources on that. If you want a thorough, yet easy to read explanation, you can checkout the [GNU make manual](http://www.gnu.org/software/make/manual/make.html#Introduction) itself. – Shahbaz Jul 02 '12 at 13:33
  • 2
    He gave you a complete example makefile, right there. Save it to a file called `Makefile`, set the filenames correctly (`func1.o` instead of `a.o`, or whatever), look up the documentation, and then ask any specific questions you have. – Useless Jul 02 '12 at 13:33
  • @Shahbaz: regarding the dependencies, maybe indicating a tool to generate them automatically would be useful. It's so easy to get them wrong... – Matthieu M. Jul 02 '12 at 13:57
  • @MatthieuM. I agree. I believe I read somewhere that gcc (g++) can produce that, or some other program, but I don't remember. Do you know of any? – Shahbaz Jul 02 '12 at 13:58
  • @Shahbaz: We have a perl wrapper script around gcc at work... not something I would recommend to the faint of heart. I was hoping you would know a magical solution :) – Matthieu M. Jul 02 '12 at 14:37
  • @MatthieuM. I found it [here(pdf)](http://aegis.sourceforge.net/auug97.pdf). It uses `gcc -MM ` to generate the dependencies. (Parent http page [here](http://miller.emu.id.au/pmiller/books/rmch/?ref=DDiyet.Com)) – Shahbaz Jul 02 '12 at 14:51
  • @MatthieuM., looking back at this conversation, the manual of `gcc` includes many `-M` variants to control what dependencies get included. For example include or exclude standard headers, missing headers etc. – Shahbaz Oct 08 '13 at 12:22
1
  • Try to minimize the code impacted by your parameter changes, ideally only change one source file no one depens on (main.cpp).
  • Check your includes: do you really need it all? Use forward declaration where possible (e.g. #include instead of ), for your own classes, forward declare what you can.
  • Try using the clang (llvm.org) compiler. It sometimes compiles faster than gcc (assuming you're on linux/unix) and gives more readable errors.

Edit: I was assuming you were only recompiling what's needed. As others suggested, use a buildsystem (Makefile, IDE, CMake...) to run a minimal number of compiles.

Antoine
  • 13,494
  • 6
  • 40
  • 52
0

Maybe this will or won't help much, but I run code through ssh and I know that it takes forever to run/compile. If you are reading from data files, instead of running entire sets of data, run only over a file or two to see your intended result. This will be a sample of your final result, but should still be accurate (just with less statistics). Once you've tweaked your code to work to your satisfaction, then run everything. usually you will have no problems that way, and your compile time is much quicker comparatively speaking.

wpochron
  • 46
  • 1
  • 10