JKQTPlotter trunk/v5.0.0
an extensive Qt5+Qt6 Plotter framework (including a feature-richt plotter widget, a speed-optimized, but limited variant and a LaTeX equation renderer!), written fully in C/C++ and without external dependencies
Loading...
Searching...
No Matches
Build using CMake

Running a Build with CMake

The preferred way to build JKQTPlotter is using CMake (at least using version 3.23). You can find a detailed explanation of CMake at https://cliutils.gitlab.io/modern-cmake/. The CMake-build is defined in CMakeLists.txt files, found in many of the directories of the code repository. Especially in the root directory and the two subdirectories ./lib/ and ./examples/ .

You can build JKQTPlotter (and also the examples) by either opening the file CMakeLists.txt in QTCreator (which has CMake integration), or by calling CMake by hand. How to do this depends on your local system und build environment.

Generic Configuring&Building

The first step is to configure the bild,using:

$ mkdir build
$ cd build
$ cmake -G "<GENERATOR>" "-DCMAKE_PREFIX_PATH=<path_to_your_qt_sources>" "-DCMAKE_BUILD_TYPE=Debug" ..

This configures the CMake build. The Parameter <GENERATOR> tells CMake, which build-file generator to use (e.g. use MinGW Makefiles for MinGW Makefiles on Windows or Unix Makefiles on Unix, see https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html for a list of available generators). For single-configuration generators (e.g. Unix Makefiles, ..., as opposed to multi-config, like Ninja, XCode or Visual Studio) you will have to explicitly set the build type using -DCMAKE_BUILD_TYPE=Debug (note, you can choose any other allowed build type instead of Debug, as e.g. Release or RelWithDepInfo ). The parameter <path_to_your_qt_sources> should be the path to your Qt installation. You can also add (separated by semicolon) paths to other libraries that JKQTPlotter should use, e.g. CImg or OpenCV. JKQTPlotter does not have any neccessary dependencies, apart from Qt5 or 6, but it contains binding code to somme popular 3rd party libraries (as e.g. CImmg or OpenCV). Also see Configuring a Build with CMake for a listing of additional configuration options for JKQTPlotter. You might e.g. want to set CMAKE_INSTALL_PREFIX to a path in which you want to install the build results. This also has to be done at this stage.

Of course you can also perform this step in the CMake GUI or ccmake.

Now you can build the library using:

$ cmake --build . --config "Debug"

Here we explicitly build the configuration Debug ... you can also build other configurations here, as e.g. Release . The final build will put all of these side-by-side into the install directory, sou can can generate a multi-configuration installation. When linking to JKQTBuilder, the matching condfiguration´should be selected automatically!

Configuring&Building with Visual Studio/MSVC

For Visual Studio / Visual C++ the build looks similar, but you will have to select one of the Visula Studio ... generators, e.g. :

$ mkdir build
$ cd build
$ cmake -G "Visual Studio 16 2019 Win64" "-DCMAKE_PREFIX_PATH=<path_to_your_qt_sources>" ..

Where <path_to_your_qt_sources> could be e.g. C:/Qt/6.5.0/msvc2019_64 . This call results in a Visual Studio solution build/JKQTPlotter.sln that you can load and compile from the Visual Studio IDE. Alternatively you can also build the solution directly calling:

$ cmake --build . --config "Debug"

Again we explicitly build the configuration Debug ... you can also build other configurations here, as e.g. Release . The final build will put all of these side-by-side into the install directory, sou can can generate a multi-configuration installation. When linking to JKQTBuilder, the matching condfiguration´should be selected automatically!

Configuring a Build with CMake

The CMake build system offers several configuration variables that you may set/change to modify the outcome of the build (in the examples above, CMAKE_PREFIX_PATH is already used, you may set the following options in the same way:

  • CMAKE_INSTALL_PREFIX : Install directory for the library
  • CMAKE_PREFIX_PATH : add the path to your Qt installatrion to this variable, so the find_package(Qt5...) commands find the libraries you want to use
  • BUILD_SHARED_LIBS : Build as shared library (default: ON ), if this is OFF the library will be built as a static library
  • JKQtPlotter_BUILD_INCLUDE_XITS_FONTS : Include XITS fonts as resources in library (default: ON )
  • JKQtPlotter_BUILD_INCLUDE_FIRAMATH_FONTS : Include Fira Math fonts as resources in library (default: ON )
  • JKQtPlotter_BUILD_FORCE_NO_PRINTER_SUPPORT : switches off print-support (when set to ON ), even if the current platform supports it (default: OFF )
  • JKQtPlotter_BUILD_DECORATE_LIBNAMES_WITH_BUILDTYPE : If set, the build-type is appended to the library name (default: ON )
  • JKQtPlotter_BUILD_WITH_PRECOMPILED_HEADERS : If set, the build uses precompiled headers to speed up (a bit) (default: ON )
  • JKQtPlotter_ENABLED_CXX20 : Build using C++20 (requires a compiler that supports this! (default: OFF )
  • JKQtPlotter_BUILD_WITH_TIMING_INFO_OUTPUT : If enabled (ON ), additional tiing code is activated (and built into the library), which outputs timming information for several functions of the library into qDebug() . This is intended for development putposes only, NOT for productive builds! (default: OFF )
  • JKQtPlotter_BUILD_EXAMPLES : Build examples (default: ON )
  • JKQtPlotter_BUILD_TOOLS : Build tools (default: ON )
  • JKQtPlotter_BUILD_TESTS : Build unit-tests (default: ON )
  • JKQtPlotter_BUILD_LIB_JKQTMATH : Build sub-library JKQTMath (default: ON )
  • JKQtPlotter_BUILD_LIB_JKQTMATHTEXT : Build sub-library JKQTMathText (default: ON )
  • DEPRECATED: JKQtPlotter_BUILD_LIB_JKQTFASTPLOTTER : Build sub-library JKQTFastPlotter (default: ON ) ... Note: This option may change to default=OFF in future ... and may be removed thereafter!
  • JKQtPlotter_BUILD_LIB_JKQTPLOTTER : Build sub-library JKQTPlotter (default: ON )

Installing with CMake

Finally, after a successful build, you can install the binaries, headers etc. into a directory, given by the CMake option CMAKE_INSTALL_PREFIX . Simply run:

$ cmake --build . --config "Debug" --target install

or:

$ cmake --install . --config "Debug"

in your build directory. Again we explicitly build the configuration Debug ... you can also build other configurations here, as e.g. Release . The final build will put all of these side-by-side into the install directory, sou can can generate a multi-configuration installation. When linking to JKQTBuilder, the matching condfiguration´should be selected automatically!

You will end up with an install directory that constains subdirectories, such as lib/ , include/ or bin/ . Thesecontain the build results. The next section Using a CMake Build describes how to use such an installation to link against JKQTPlotter.

Using a CMake Build

After building and installing JKQTPlotter you have all files that you need inside the instal directory:

  • <CMAKE_INSTALL_PREFIX>/include contains all required header files
  • <CMAKE_INSTALL_PREFIX>/bin contains the shared libraries, examples, tools
  • <CMAKE_INSTALL_PREFIX>/lib contains the link libraries
  • <CMAKE_INSTALL_PREFIX>/lib/cmake/JKQTPlotterX/ contains files necessary for CMake's find_package() to work

The build/install above generates several CMake targets that can be used to link against JKQTPlotter (or one of its sub libraries). The generated targets are (note that the Qt-version number appears the target names!!!):

  • JKQTPlotter6::JKQTPlotter6 / JKQTPlotter5::JKQTPlotter5 : the main plotting library, containing JKQTPlottter and JKQTBasePlotter
  • JKQTPlotter6::JKQTMathText6 / JKQTPlotter5::JKQTMathText5 : the LaTeX rendering library containing JKQTMathText
  • JKQTPlotter6::JKQTCommmon6 / JKQTPlotter5::JKQTCommon5 : basic tools and facilities for the other libraries
  • JKQTPlotter6::JKQTMath6 / JKQTPlotter5::JKQTMath5 : additional mathematical algorithms to be used with JKQTPlotter (e.g. a statistics library with histograming, KDE or regression)
  • DEPRECATED: JKQTPlotter6::JKQTFastPlotter6 / JKQTPlotter5::JKQTFastPlotter5 : an alternative plotting library with less features, but which is supposed to be fast,conatins JKQTFastPlotter

You can find an example project that uses a complete cmake-build here: Example (JKQTPlotter): CMake Example (online: examples/cmake_link_example).

Here is the CMakeLists.txt from that directory:

# set minimum required CMake-Version
cmake_minimum_required(VERSION 3.23)
# set Project name
project(simpletest_cmake LANGUAGES CXX)
# some basic configurations
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Configure project for usage of Qt5/Qt6
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Gui Widgets PrintSupport Svg Xml OpenGl REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Gui Widgets PrintSupport Svg Xml OpenGL REQUIRED)
# include JKQTPlotter
find_package(JKQTPlotter${QT_VERSION_MAJOR} REQUIRED)
# For Visual Studio, we need to set some additional compiler options
if(MSVC)
add_compile_options(/EHsc)
# To enable M_PI, M_E,...
add_definitions(/D_USE_MATH_DEFINES)
# To Prevent Errors with min() and max()
add_definitions(/DNOMINMAX)
# To fix error: C2338: va_start argument must not
# have reference type and must not be parenthesized
add_definitions(/D_CRT_NO_VA_START_VALIDATION)
endif()
# add the example executable
add_executable(${PROJECT_NAME} WIN32 simpletest.cpp)
# ... link against Qt5/6 and JKQTPlotterLib
# (you could use JKQTPlotterSharedLib if you don't want to link againast the
# static version, but against the shared/DLL version).
target_link_libraries(${PROJECT_NAME} Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Gui Qt${QT_VERSION_MAJOR}::PrintSupport Qt${QT_VERSION_MAJOR}::Svg Qt${QT_VERSION_MAJOR}::Xml)
# ... link against JKQTPlotter: As the Targets contain the Qt-Version-Number in their names, we can
# link against 'JKQTPlotter${QT_VERSION_MAJOR}::JKQTPlotter${QT_VERSION_MAJOR}' and it works
# for Qt5 AND Qt6 ...
# if you have a speific Qt-Version, you can also write e.g. 'JKQTPlotter6::JKQTPlotter6'
target_link_libraries(${PROJECT_NAME} JKQTPlotter${QT_VERSION_MAJOR}::JKQTPlotter${QT_VERSION_MAJOR})
# Installation
install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

As you can see this cooks down to first finding the library, using

find_package(JKQTPlotter${QT_VERSION_MAJOR} REQUIRED)

and then linking against the appropriate target from the list above:

target_link_libraries(${PROJECT_NAME} JKQTPlotter${QT_VERSION_MAJOR}::JKQTPlotter${QT_VERSION_MAJOR})

If you don't need to be Qt-version agnostic, you can simply write:

find_package(JKQTPlotter6 REQUIRED)
target_link_libraries(${PROJECT_NAME} JKQTPlotter6::JKQTPlotter6)

To build this example, you first need to make a subdirectory build inside the example directory examples/cmake_link_example and then call CMake form that subdirectory:

$ mkdir build
$ cd build
$ cmake .. -G "<GENERATOR>" "-DCMAKE_PREFIX_PATH=<path_to_your_qt_sources>;<JKQTPLOTTER_INSTALL_DIR>"

Again choose any CMake <GENERATOR> that is appropriate for your use-case. Replace <path_to_your_qt_sources> by the path of your Qt installation and <JKQTPLOTTER_INSTALL_DIR> by the CMAKE_INSTALL_PREFIX of your JKQTPlotter Build (see above).

Then you can again build the example either using the generated builf files (e.g. a Visual Studio solution), or by running

$ cmake --build . --config "Debug" --target install

Using JKQTPlotter with CMake's FetchContent-API

In addition to the method described above (i.e. build and install the library and then use it), you can also use JKQTPlotter via CMake's FetchContent-API. Also have a look at this blog post for a detailed explanation. Also see Example (JKQTPlotter): CMake FetchContent Example for a detailed example.

For this method, you need to add these lines to your CMake project:

include(FetchContent) # once in the project to include the module
# ... now declare JKQTPlotter5/6
FetchContent_Declare(JKQTPlotter${QT_VERSION_MAJOR}
GIT_REPOSITORY https://github.com/jkriege2/JKQtPlotter.git
# GIT_TAG v5.0.0)
# ... finally make JKQTPlotter5/6 available
FetchContent_MakeAvailable(JKQTPlotter${QT_VERSION_MAJOR})

These declare JKQTPlotter and make it available in your project. Afterwards you should be able to link against it, using

target_link_libraries(${PROJECT_NAME} JKQTPlotter${QT_VERSION_MAJOR}::JKQTPlotter${QT_VERSION_MAJOR})