Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

KDE Frameworks - Part 2

Extra CMake Modules for Enhancing Your CMake Code

find_package(ECM REQUIRED)
set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH})
add_executable(test1)
target_sources(test1 PRIVATE test1.cpp)
target_link_libraries(test1 Qt::Core Qt::Test myapp)

add_executable(test2)
target_sources(test2 PRIVATE test2.cpp)
target_link_libraries(test2 Qt::Core Qt::Test myapp)

add_executable(test3)
target_sources(test3 PRIVATE test3.cpp)
target_link_libraries(test3 Qt::Core Qt::Test myapp)
include(ECMAddTests)

ecm_add_tests(
    test1.cpp
    test2.cpp
    test3.cpp
    LINK_LIBRARIES
    Qt::Core Qt::Test myapp
)
add_library(mylib)
target_link_libraries(mylib PUBLIC Qt::Core Qt::Widgets)

...

include(ECMGeneratePkgConfigFile)

ecm_generate_pkgconfig_file(BASE_NAME MyLib
      INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_INCLUDEDIR}/MyLib/
      DEPS "Qt5Core Qt5Widgets"
    INSTALL)
cmake_minimum_required(VERSION 3.16)
project(mylib VERSION 1.2.3)

...

include(ECMSetupVersion)
ecm_setup_version(PROJECT VERSION_HEADER mylib_version.h)

...

install(FILES ${CMAKE_BINARY_DIR}/mylib_version.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
#include "mylib_version.h"

...

qDebug() << "MyLib has version" << mylib_VERSION_STRING; // MyLib has version 1.2.3
#if mylib_VERSION < QT_VERSION_CHECK(1, 1, 0)
    myFunction();
#else
    myOtherFunction();
#endif
add_library(mylib)
target_link_libraries(mylib PUBLIC Qt::Core Qt::Widgets)

...

include(ECMGeneratePriFile)
ecm_generate_pri_file(BASE_NAME MyLib LIB_NAME MyLib DEPS "Core Widgets" FILENAME_VAR PRI_FILENAME INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_INCLUDEDIR}/MyLib)
install(FILES ${PRI_FILENAME} DESTINATION ${ECM_MKSPECS_INSTALL_DIR})
include(ECMQtDeclareLoggingCategory)

...

add_executable(myapp)

...

ecm_qt_declare_logging_category(myapp
    HEADER logging.h
    IDENTIFIER MYAPP
    CATEGORY_NAME com.mycompany.myapp
)
#include "logging.h"

...

qCDebug(MYAPP) << "Hello World!";

About KDAB


NicolasFella

Nicolas Fella

Software Engineer

Learn Modern C++

Learn more