Reputation: 5055
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.?
Upvotes: 3
Views: 592
Reputation: 47593
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
.
Upvotes: 6
Reputation: 46
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.
Upvotes: 0
Reputation: 14094
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.
Upvotes: 1