How to reduce the build time associated with using a c++ library (QCustomPlot)?

243 views Asked by At

I'm writing a Qt6 QWidget app. The build time is really long after I include the QCustomPlot library. I want to do something to the library to reduce the build time.

QCustomPlot consists of two files: qcustomplot.h and qcustomplot.cpp. It depends on the library Qt::PrintSupport.

I've done some experiments to analyze the build time problem. I use QtCreator's template MainWindow project, which only has an empty window with no functionality. I have more concern about the incremental build time, so I build the whole project, then add a newline in mainwindow.cpp, then build again and measure the incremental build time.

Following is what I get. The time is the incremental build time in Debug or Release build mode.

  1. Create template MainWindow project, add link library Qt6::PrintSupport (for better comparison to later cases)

    • Debug 2s
    • Release 2s
  2. Add qcustomplot.h and qcustomplot.cpp to the source files in CMakeLists. mainwindow.cpp doesn't have #include "qcustomplot.h"

    • Debug 46s
    • Release 3s
  3. Add #include "qcustomplot.h" to mainwindow.cpp, without using anything in it.

    • Debug 52s
    • Release 8s
  4. Build QCustomPlot in release mode as a dll. In the original project, use qcustomplot.dll instead of qcusomplot.cpp and Qt::PrintSupport. mainwindow.cpp doesn't have #include "qcustomplot.h"

    • Debug 10s
    • Release 3s
  5. Add #include "qcustomplot.h" to mainwindow.cpp, without using anything in it.

    • Debug 16s
    • Release 8s

From the experiment data, compiling qcustomplot.h takes about 6s, which I think can be prevented by using wrapper classes to expose smaller interface. The linking phase also seems to take a lot of time, especially in Debug build. 10s in Debug build is still too much for a nearly empty project. The Qt libraries are more complicated than QCustomPlot, but using Qt libraries doesn't increase build time much. Why does build with QCustomPlot takes so much time? How can I reduce it?

The library/compiler version: Qt6.4.0 in dll, mingw64 gcc 11.2.0. QCustomPlot 2.1.1

2

There are 2 answers

0
Jackoo On

Here's what I find to be useful.

  1. Build qcustomplot as a shared library in release mode. Link to the shared library instead of using qcustomplot.cpp in other projects.

  2. Use a wrapper class and pImpl idiom to encapsulate qcustomplot.h. Alternatively, make a precompiled header from qcustomplot.h. The cmake command target_precompile_headers is very handy.

Step 1 reduces the link time greatly. Step 2 reduces the compile time for the header. The incremental build time is reduced to 2~3 seconds on my computer.

1
Hristo Iliev On

You can try also:

  • use generator like Ninja which really speeds up recompilation
  • use CCache as a CMAKE_CXX_COMPILER_LAUNCHER