简体   繁体   中英

fast compilation in c++

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.?

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 ah causes compilation of ao and bo 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 .

  • 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 (eg #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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM