QCustomPlot + FetchContent

According to the official website, “QCustomPlot is a Qt widget for plotting and data visualization.” Repository gitlab.

torque diagram most likely

torque diagram more likely

The library has existed since 2013 and is good documented. Pleased with the availability examples. Has no dependencies other than Qt itself, which makes it easier lifeporting to other platforms/cross-compilation. Current revision 2.1.1 supports qt 6.4.

During my institute days, I actively used to write all kinds of strength-of-material demos that we had as homework and, just as actively, I got my fellow students hooked on the subject. After the regular one, for that Delphi university with its windows forms, qcp seemed like mana from heaven.

games with terver

games with terver

Liba is good. The only BUT is that there is no integration into the cmake build system, or I couldn’t find ready-made ones. The archives from the official website contain qmake scripts, and github is filled with demos.
After sitting for an evening, I want to share my thoughts on how you can quickly connect this to your cmake project using standard means, without manually downloading the source code and using a couple of lines in the cmake script.

Cmake allows you to do some things during configuration: follow the url, go to the remote repo, unpack, build. This is what he does FetchContent. Textbook example suggests using a construction where we declare a resource:

FetchContent_Declare(qcustomplot
  URL https://www.qcustomplot.com/release/2.1.1/QCustomPlot-source.tar.gz
)

Then we make it available:

FetchContent_MakeAvailable(qcustomplot)

This pair of commands will allow you to download and unpack the sources. Further FetchContent will try to find the root CMakeLists.txt and, if present, execute add_submodule(some_extra_dir).

When should you not call FetchContent_MakeAvailable explicitly?

The mechanism is described on the same documentation page Integrating With find_package.
It makes the declared resource visible to find_package. In other words, if find_package(target) did not find target in CMAKE_PREFIX_PATH and others, it will turn to FetchContent_MakeAvailable.

QCustomPlot does not have a core cmake list. Accordingly, it will not be possible to import the library target. Let’s write a trivial add_library:

add_library(qcustomplot
  "${qcustomplot_SOURCE_DIR}/qcustomplot.h"
  "${qcustomplot_SOURCE_DIR}/qcustomplot.cpp"
)

qcustomplot_SOURCE_DIR will determine MakeAvailable for us.
The order of the commands seems to me consistent with their appearance in the article. An example from the documentation about Complex Dependency Hierarchies where someFile.c is copied before an explicit call to add_subdirectory.

The repo is ready for you to use link.
Example use via standard target_link_libraries:

target_link_libraries(<you_target>
  ...
  qcustomplot
)

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *