Petr
Petr

Reputation: 14505

Is there a way to optimize g++ so that it build faster?

Note: this has absolutely nothing to do with optimization of produced binary code.

I have a project with more than 120 source code files. If I put everything into one huge cpp file, the compilation of the code take significantly shorter than running standard makefile build which spawn separate g++ process for every single cpp file. (I also noticed that running g++ on cpp file which is nearly empty, or empty at all, takes also some time, so running g++ on 2 cpp files that are relatively small takes significantly longer than running it on 1 cpp file that contains the code of both).

Given the size of the project, the compilation currently takes long time even on very fast hardware. Is there anything that could be done in order to speed up the time it takes for compiler to produce binary file? Given that g++ process is loaded for every cpp file with same options, I assume it is doing lot of same work for every file (it surely needs to load something from some libraries, etc, check same things over and over again and so on). Isn't there something that could make it skip doing the "same tasks repeatedly for every file" so that it produces the object files much faster?

EDIT: The project is Qt application, Makefile is generated using qmake

Upvotes: 2

Views: 288

Answers (4)

BЈовић
BЈовић

Reputation: 64273

There are several things to do :

  • when possible, forward declare in headers, instead of including headers
  • remove not needed includes
  • instead of using qmake and makefile, use cmake and ninja (see this)
  • get better hardware (ssd disk, more and faster memory, CPUs, ... )
  • spawn more jobs when building

If I put everything into one huge cpp file, the compilation of the code take significantly shorter than running standard makefile build which spawn separate g++ process for every single cpp file.

Most likely the culprit is bad hard disk. By getting a super fast ssd disk, you are going to see huge improvements.

Upvotes: 1

user2485710
user2485710

Reputation: 9811

The closest thing to a cache for gcc that I know of is ccache, you can use it when invoking gcc itself like so

ccache gcc [...]

But I would suggest you to switch to a building system such as make or cmake because this softwares are able to track dependencies and files that don't need to be re-compiled again.

Upvotes: 1

Mark B
Mark B

Reputation: 96291

There are a few things you can try:

  • Make sure that you aren't including every header in every source file, try to minimize coupling between files.
  • Utilize precompiled headers t o avoid reading system headers over and over.
  • Include related sets of .C files in .cxx files, which you then compile with your makefile. This has the effect of putting all the source in one file without making the code harder to read.

Upvotes: 2

Jepessen
Jepessen

Reputation: 12435

Did you try parallel compilation? You can add -jN flags to make, where N is number of compilation threads (usually 1.5 * number of cpu cores).

Upvotes: 2

Related Questions