SlideShare a Scribd company logo
Integrated Computer Solutions Inc. www.ics.com
CMake for QMake Users
Chris Rizzitello, ICS
September 22, 2022
1
Integrated Computer Solutions Inc. www.ics.com
About ICS
Delivering Smart Devices for a Connected World
● Founded in 1987
● Largest source of independent Qt expertise in North America
● Trusted Qt Service Partner since 2002
● Exclusive Open Enrollment Training Partner in North America
● Provides integrated custom software development and user experience (UX) design
● Embedded, touchscreen, mobile and desktop applications
● HQ in Waltham, MA with offices in California, Canada, Europe
2
Boston UX
● Part of the ICS family, focusing on UX design
● Designs intuitive touchscreen interfaces for high-impact embedded
and connected medical, industrial and consumer devices
Integrated Computer Solutions Inc. www.ics.com
What is CMake
CMake is an open source, multi platform makefile tool that aids in the building
testing and packaging of the software.
- Generate native makefiles for the system
- Provide rules for deployment
- Easily find dependencies
- Highly configurable
- CMake projects can be opened in many IDEs including QtCreator
- QtCreator has basic support for CMake when adding and creating files
Qt6 will suggests CMake as its project type
3
Integrated Computer Solutions Inc. www.ics.com
Example CMake Project
#include <QtWidgets>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QPushButton button("Hello CMake");
QObject::connect(&button, &QPushButton::clicked, &app, &QApplication::exit);
button.show();
return app.exec();
}
main.cpp
We have the following code, let’s create a simple CMake project for it
4
Integrated Computer Solutions Inc. www.ics.com
Setting up a CMake project
5
Our Source Code folder looks like this:
Create a new file CMakeLists.txt (CaseSensitive)
our directory will now look like this:
NewProject/
NewProject/main.cpp
NewProject/
NewProject/CMakeLists.txt
NewProject/main.cpp
Integrated Computer Solutions Inc. www.ics.com
The First Lines of a main CMakeLists.txt
cmake_minimum_required(VERSION 3.21)
# This file will require cmake 3.21 to be processed
project(hello_cmake)
# Our project will be named "hello_cmake" (ONLY REQUIRED ITEM)
# Stored in a variable: CMAKE_PROJECT_NAME
6
The project name does not automatically set the name of any target.
Integrated Computer Solutions Inc. www.ics.com
Find Dependencies and Qt Modules
find_package(Qt6 REQUIRED COMPONENTS
Widgets
)
7
The COMPONENTS are the Qt Modules that will be used for the project. “core” is
imported by all other components.
In Qmake project file this is equivalent to QT+=widgets
Integrated Computer Solutions Inc. www.ics.com
Creating a Target
target_link_libraries(hello_cmake PRIVATE Qt6::Core Qt6::Widgets)
8
For Qt 5.15 and Qt6 can be linked dynamically based on the version in use
Link with versionless Qt statements to enable this I.E “Qt::Core” or “Qt::Widgets”
add_execuitable is a “app” project
add_library is a “lib” project
Both calls set the name of the that target.
Target names can be used several other places, only after defined
add_executable(hello_cmake main.cpp)
Integrated Computer Solutions Inc. www.ics.com
Generating the build and install info
install( TARGETS hello_cmake RUNTIME DESTINATION bin )
We can tell Cmake where to install stuff for us
9
TARGETS can have several
types
- ARCHIVE
- LIBRARY
- RUNTIME
- OBJECTS
- FRAMEWORK
- BUNDLE
- PRIVATE_HEADER
- PUBLIC_HEADER
- RESOURCE
Install
- TARGETS
- FILES
- DIRECTORY
Integrated Computer Solutions Inc. www.ics.com
CMake Install Prefix
All files are installed into the CMAKE_INSTALL_PREFIX (Default: /usr/local)
10
cmake -DCMAKE_INSTALL_PREFIX=<prefix_path> CMakeLists.txt
The prefix can be set in the CMakeLists.txt file. It will be used unless
overridden at configuration time.
set(CMAKE_INSTALL_PREFIX <prefix_path>)
At configuration time, the prefix can be changed with :
Integrated Computer Solutions Inc. www.ics.com
Setting Vars
#Set our C++ Standard to C++17, Project will use C++17 standard if possible
set(CMAKE_CXX_STANDARD 17)
# If the project uses features required from the standard, it needs to be set as required.
# In the event the system does not provide the selected standard, the configuration will fail.
set(CMAKE_CXX_STANDARD_REQUIRED ON)
11
The following sets the variable to the given value :
set(MyVar 5)
Setting the C++ Standard requirement :
message(“The Value is ${MyVar}”)
Later you can use it like this
Integrated Computer Solutions Inc. www.ics.com
Qt Specific CMake Helpers
Normally you would have to make and custom_commands for CMake needs to
run moc, rcc and/or uic. With CMake 3.10+ you simply set these variables
12
# Automatically run MOC on the files that need it
set(CMAKE_AUTOMOC ON)
# Automatically run RCC on the files that need it (resource files)
set(CMAKE_AUTORCC ON)
# Automatically run UIC on the files that need it (UI files)
set(CMAKE_AUTOUIC ON)
With the above options qrc and ui files can be used like any other source
files.
Integrated Computer Solutions Inc. www.ics.com
QML Plugins
The QmlImportScanner module for Qt can be used to check and link all the QML
plugins that are needed for the target:
13
find_package(Qt REQUIRED COMPONENTS
...
QmlImportScanner
)
qt_import_qml_plugins(target)
Integrated Computer Solutions Inc. www.ics.com
Creating configuration options
Options are easy to create and can change how a project is built.
Create an option in the CMakeLists.txt file
option(UseFooLib "Link with FooLib" OFF)
if (UseFooLib)
# Link with Foo lib
endif()
Set the option during configuration
cmake -DUseFooLib=ON CMakeLists.txt
14
Integrated Computer Solutions Inc. www.ics.com
Configuring files
CMake can also use templates to create files
project(MyCoolProject)
configure_file(file.in file.out @ONLY)
CMAKE_CURRENT_SOURCE_DIR/file.in
15
CMakeLists.txt
#define ProjectNAME “@CMAKE_PROJECT_NAME@”
CMAKE_CURRENT_BINARY_DIR/file.out
#define ProjectNAME “MyCoolProject”
CMake will will replace any valid CMake variable wrapped in ${VAR} or @VAR@ .
In the above example, since @ONLY is passed, CMake will only replace CMake variables
wrapped in the format @VAR@
Integrated Computer Solutions Inc. www.ics.com
Setting up a CMake project Recap
1. Set the minimum version of CMake needed to process the project
2. Set a project name
3. Find dependencies used to build the code
4. Add library or executable that uses the source code
5. Provide linking rules for the lib / exe
6. Optionally add install instructions for the new project
16
The CMakeLists.txt file in the project root folder should contain the following :
Integrated Computer Solutions Inc. www.ics.com
Setting up a CMake project: Sub folders?
1. Find new dependencies used to build the code in the directory
2. Set source files in this path
3. Add library or executable that uses the sources
4. Provide linking rules for the lib / exe
5. Optionally add install instructions this new component
6. Optionally link a library target with the another part of your project.
17
What about “subdir” projects? You can also use add_subdirectory(foo) to include a folder.
Any directory included with the add_subdirectory command must contain a CMakeLists.txt
File. This CMakeLists.txt will tell you how handle the source files in that directory.
Integrated Computer Solutions Inc. www.ics.com
Using the cmake command
The cmake command is used to parse the CMakeLists.txt files in your project.
Its most basic use is: This will create build files in source.
18
cmake CMakeLists.txt
Option Description
-S <path> Sets the source directory (-H before version 3.13)
-B <path> Set build path, default path is the current directory
-D<var>=<value> Set a variable's value to passed
-G <generator> Forces the use of a specific generator
--graphviz=<file> Create a graphviz of project dependencies
--help Show the help for cmake command (easy way to see generators on the system)
Integrated Computer Solutions Inc. www.ics.com
Shadow Building Steps
Shadow building is easy with cmake
1. cd <sourcePath>
2. cmake -S. -B<buildpath>
3. cmake --build <buildpath>
This will create all the generated build files in the “buildpath” , Make will build all files into
the build path when it’s run
19
Integrated Computer Solutions Inc. www.ics.com
CMake Times
20
Integrated Computer Solutions Inc. www.ics.com
Localization With Qt: Generating Ts Files CMAKE
21
set_directory_properties(PROPERTIES
CLEAN_NO_CUSTOM 1) #Do not remove *.ts on clean
find_package(QtLinguistTools)
set(needsTranslation
Mainwindow.cpp
… More source files
)
set(languageFiles
myApp_en.ts
myApp_fr.ts
myApp_de.ts
)
qt_create_translation(TSFiles
needsTranslation
languageFiles
)
add_custom_target(TRS ALL DEPENDS ${TSFiles})
find_package(QtLinguistTools)
qt_add_translation(QMFiles
mainwindow.cpp
myApp_en.ts
myApp_fr.ts
myApp_de.ts
)
add_custom_target(QMS ALL DEPENDS
${QMFiles}
)
21
qt_add_translation only runs lrelease,
requires pre-existing .ts files
qt_create_translation runs both lupdate
and lrelease
Integrated Computer Solutions Inc. www.ics.com
Find Dependencies and Qt Modules Revisited
##using Qt6?
find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE Qt::Widgets)
## using Qt5?
find_package(Qt5 REQUIRED COMPONENTS Widgets)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE Qt::Widgets)
## Qt6 but Qt5 if its not found
find_package(Qt NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
## Qt6, unless the user sets it to 5 when they configure
if(NOT QT_DEFAULT_MAJOR_VERSION)
set(QT_DEFAULT_MAJOR_VERSION 6 CACHE STRING "" FORCE)
endif()
find_package(Qt${QT_DEFAULT_MAJOR_VERSION} REQUIRED COMPONENTS Widgets)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE Qt::Widgets)
22
Integrated Computer Solutions Inc. www.ics.com
CPack Making Packages
23
Integrated Computer Solutions Inc. www.ics.com
How Can I Be “CPack Ready”
Everything you want to install has install instructions
1. Targets use install(TARGET …)
2. Support files use install(FILES | DIRECTORY …)
24
Integrated Computer Solutions Inc. www.ics.com
Find macdeployqt / windeployqt
get_target_property(_qmake_executable Qt5::qmake IMPORTED_LOCATION)
get_filename_component(_qt_bin_dir "${_qmake_executable}" DIRECTORY)
find_program(MACDEPLOYQT macdeployqt HINTS "${_qt_bin_dir}")
find_program(WINDEPLOYQT windeployqt HINTS "${_qt_bin_dir}")
25
*Cmake has some native ways to find dependencies unfortunately they all currently fail to find the Qt
Plugins and at very least force you to make a list of all the plug-ins you need to ship.
For this reason its recommended that you use the *deployQt to gather the dependencies. This alone will
not run them for you but will let you use them in a post build or pre deployment via
add_custom_command similar to how we did w/ the translations in the cmake.
Integrated Computer Solutions Inc. www.ics.com
Generic CPack
set(CPACK_STRIP_FILES [TRUE | FALSE] )
set(CPACK_PACKAGE_NAME “PACKAGENAME”)
set(CPACK_RESOURCE_FILE_LICENSE “PATH TO LICENSE FILE”)
set(CPACK_PACKAGE_VERSION ${CMAKE_PROJECT_VERSION})
set(CPACK_GENERATOR “TGZ;....”)
…
INCLUDE (CPack) <- Last Line
26
Integrated Computer Solutions Inc. www.ics.com
CPack Archive Type Packages
Several types of archives can be generated.
27
Generator String Description
7Z Generate a 7-Zip Archive
TBZ2 Generate a B Zipped Tarball
TGZ Generate a Gnu Zipped Tarball
TXS Generate a LZMA Compressed Tarball
TZ Generate a Zip Compressed Tarball
ZIP Generate a Zip Archive
CPACK_ARCHIVE_FILE_NAME and CPACK_ARCHIVE_COMPONENT_INSTALL
Integrated Computer Solutions Inc. www.ics.com
CPACK Qt Installer Framework
Qt Installer can generate installer for Unix, MacOs and Windows.
1. Generator Name: “IFW”
Useful Vars to set:
● CPACK_IFW_PACKAGE_TITLE
● CPACK_IFW_PACKAGE_PUBLISHER
● CPACK_IFW_PACKAGE_ICON
● CPACK_IFW_PACKAGE_WINDOW_ICON
● CPACK_IFW_PACKAGE_LOGO
● CPACK_IFW_PACKAGE_RESOURCES
● CPACK_IFW_DOWNLOAD_ALL
● CPACK_IFW_PACKAGE_FILE_EXTENSTION
28
IFW Docs: https://cmake.org/cmake/help/latest/cpack_gen/ifw.html
Integrated Computer Solutions Inc. www.ics.com
CPACK DMG
DMG files are common on mac os as a method of deployment
1. Generator Name: “DragNDrop”
2. For single app install your .app to “.”
3. Run macdeployqt on the app to prepare for deployment
4. Backgrounds are possible Requires:
i. IMAGES VOLUME NAME MUST NOT CHANGE
ii. CPACK_DMG_BACKGROUND_IMAGE
iii. CPACK_DMG_DS_STORE <- DS_Store must be generated by Finder
29
Integrated Computer Solutions Inc. www.ics.com
CPACK NSIS
Nullsoft Scriptable Install System 3.0+ has to be installed.
Several Options are unique to the NSIS generator
set(CPACK_NSIS_MENU_LINKS “EXE” “LINKNAME”) to generate start menu links
CPACK_PACKAGE_INSTALL_DIRECTORY - The path to use for install
CPACK_NSIS_EXTRA_INSTALL_COMMANDS - addition nsis commands for install
CMAKE_NSIS_EXTRA_UNINSTALL_COMMANDS - additional uninstall nsis
commands
30
Integrated Computer Solutions Inc. www.ics.com
CPACK DEB
Build DEB Packages
Several Options are unique to the DEB generator
Set CPACK_DEBIAN_FILE_NAME to DEB-DEFAULT . This will set the generated name(s) to the
default scheme.
CPACK_DEBIAN_PACKAGE_SHLIBDEPS [ON | OFF] automatically generate packages depends list
CPACK_DEB_COMPONENT_INSTALL [ TRUE | FALSE ] Allow building of split packages
CPACK_DEBIAN_<COMPONENT>PACKAGE_NAME - set name for a components package
Many other component related vars.
REQUIRES:
VERSION MUST BE a valid semantic version
A CPACK_PACKAGE CONTACT or CPACK_DEBIAN_PACKAGE_MAINTAINER is set
31
Integrated Computer Solutions Inc. www.ics.com
Thanks for Attending
32
Any questions?

More Related Content

PDF
Qt Installer Framework
 
PDF
An Introduction to CMake
 
PDF
CMake - Introduction and best practices
PDF
Cmake kitware
PPT
JUnit 4
PPTX
Introduction to Node js
PDF
Introduction to CMake
PDF
パターンでわかる! .NET Coreの非同期処理
Qt Installer Framework
 
An Introduction to CMake
 
CMake - Introduction and best practices
Cmake kitware
JUnit 4
Introduction to Node js
Introduction to CMake
パターンでわかる! .NET Coreの非同期処理

What's hot (20)

PDF
C++ Template Meta Programming の紹介@社内勉強会
PDF
ARM Trusted FirmwareのBL31を単体で使う!
PDF
github-actions.pdf
PDF
constexpr関数はコンパイル時処理。これはいい。実行時が霞んで見える。cpuの嬌声が聞こえてきそうだ
PDF
cmake.pdf
PDF
Svelte JS introduction
PDF
[COSCUP 2022] Kotlin Collection 遊樂園
PDF
使用 laravel 的前與後
PDF
Introduction to GNU Make Programming Language
PDF
Logging system of Android
PDF
Go入門
PDF
Application development with c#, .net 6, blazor web assembly, asp.net web api...
PDF
Android Boot Time Optimization
PDF
イマドキC++erのモテカワリソース管理術
PPTX
Git utilisation quotidienne
PPTX
Introduction to java 8 stream api
PDF
PDF
【BS3】Visual Studio 2022 と .NET 6 での Windows アプリ開発技術の紹介
PDF
View customize1.2.0の紹介
PDF
Tutoriel GIT
C++ Template Meta Programming の紹介@社内勉強会
ARM Trusted FirmwareのBL31を単体で使う!
github-actions.pdf
constexpr関数はコンパイル時処理。これはいい。実行時が霞んで見える。cpuの嬌声が聞こえてきそうだ
cmake.pdf
Svelte JS introduction
[COSCUP 2022] Kotlin Collection 遊樂園
使用 laravel 的前與後
Introduction to GNU Make Programming Language
Logging system of Android
Go入門
Application development with c#, .net 6, blazor web assembly, asp.net web api...
Android Boot Time Optimization
イマドキC++erのモテカワリソース管理術
Git utilisation quotidienne
Introduction to java 8 stream api
【BS3】Visual Studio 2022 と .NET 6 での Windows アプリ開発技術の紹介
View customize1.2.0の紹介
Tutoriel GIT
Ad

Similar to Basic Cmake for Qt Users (20)

PDF
CMake_Tutorial.pdf
PDF
Effective CMake
PDF
CMake best practices
PDF
C make tutorial
PDF
CMake: Improving Software Quality and Process
PDF
CMake Tutorial
PDF
IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)
PPTX
short_intro_to_CMake_(inria_REVES_team)
PPTX
CiklumCPPSat: Alexey Podoba "Automatic assembly. Cmake"
PDF
Basicsof c make and git for a hello qt application
PDF
Kitware: Qt and Scientific Computing
PDF
LOSS_C11- Programming Linux 20221006.pdf
PPTX
Autotools pratical training
PPT
ITK Tutorial Presentation Slides-944
PDF
Porting Qt 5 QML Modules to Qt 6 Webinar
 
PPTX
PDF
Build Systems with autoconf, automake and libtool [updated]
PPTX
Consuming Libraries with CMake
TXT
C make cache
CMake_Tutorial.pdf
Effective CMake
CMake best practices
C make tutorial
CMake: Improving Software Quality and Process
CMake Tutorial
IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)
short_intro_to_CMake_(inria_REVES_team)
CiklumCPPSat: Alexey Podoba "Automatic assembly. Cmake"
Basicsof c make and git for a hello qt application
Kitware: Qt and Scientific Computing
LOSS_C11- Programming Linux 20221006.pdf
Autotools pratical training
ITK Tutorial Presentation Slides-944
Porting Qt 5 QML Modules to Qt 6 Webinar
 
Build Systems with autoconf, automake and libtool [updated]
Consuming Libraries with CMake
C make cache
Ad

More from ICS (20)

PDF
Understanding the EU Cyber Resilience Act
 
PDF
Medical Device Cybersecurity Threat & Risk Scoring
 
PDF
Exploring Wayland: A Modern Display Server for the Future
 
PDF
Threat Modeling & Risk Assessment Webinar: A Step-by-Step Example
 
PDF
8 Mandatory Security Control Categories for Successful Submissions
 
PDF
Future-Proofing Embedded Device Capabilities with the Qt 6 Plugin Mechanism.pdf
 
PDF
Choosing an Embedded GUI: Comparative Analysis of UI Frameworks
 
PDF
Medical Device Cyber Testing to Meet FDA Requirements
 
PDF
Threat Modeling and Risk Assessment Webinar.pdf
 
PDF
Secure-by-Design Using Hardware and Software Protection for FDA Compliance
 
PDF
Webinar On-Demand: Using Flutter for Embedded
 
PDF
A Deep Dive into Secure Product Development Frameworks.pdf
 
PDF
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
PDF
Practical Advice for FDA’s 510(k) Requirements.pdf
 
PDF
Accelerating Development of a Safety-Critical Cobot Welding System with Qt/QM...
 
PDF
Overcoming CMake Configuration Issues Webinar
 
PDF
Enhancing Quality and Test in Medical Device Design - Part 2.pdf
 
PDF
Designing and Managing IoT Devices for Rapid Deployment - Webinar.pdf
 
PDF
Quality and Test in Medical Device Design - Part 1.pdf
 
PDF
Creating Digital Twins Using Rapid Development Techniques.pdf
 
Understanding the EU Cyber Resilience Act
 
Medical Device Cybersecurity Threat & Risk Scoring
 
Exploring Wayland: A Modern Display Server for the Future
 
Threat Modeling & Risk Assessment Webinar: A Step-by-Step Example
 
8 Mandatory Security Control Categories for Successful Submissions
 
Future-Proofing Embedded Device Capabilities with the Qt 6 Plugin Mechanism.pdf
 
Choosing an Embedded GUI: Comparative Analysis of UI Frameworks
 
Medical Device Cyber Testing to Meet FDA Requirements
 
Threat Modeling and Risk Assessment Webinar.pdf
 
Secure-by-Design Using Hardware and Software Protection for FDA Compliance
 
Webinar On-Demand: Using Flutter for Embedded
 
A Deep Dive into Secure Product Development Frameworks.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Practical Advice for FDA’s 510(k) Requirements.pdf
 
Accelerating Development of a Safety-Critical Cobot Welding System with Qt/QM...
 
Overcoming CMake Configuration Issues Webinar
 
Enhancing Quality and Test in Medical Device Design - Part 2.pdf
 
Designing and Managing IoT Devices for Rapid Deployment - Webinar.pdf
 
Quality and Test in Medical Device Design - Part 1.pdf
 
Creating Digital Twins Using Rapid Development Techniques.pdf
 

Recently uploaded (20)

PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
Online Work Permit System for Fast Permit Processing
PDF
System and Network Administration Chapter 2
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Nekopoi APK 2025 free lastest update
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
medical staffing services at VALiNTRY
How to Migrate SBCGlobal Email to Yahoo Easily
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Odoo POS Development Services by CandidRoot Solutions
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Which alternative to Crystal Reports is best for small or large businesses.pdf
Online Work Permit System for Fast Permit Processing
System and Network Administration Chapter 2
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
CHAPTER 2 - PM Management and IT Context
Nekopoi APK 2025 free lastest update
Softaken Excel to vCard Converter Software.pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
2025 Textile ERP Trends: SAP, Odoo & Oracle
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
VVF-Customer-Presentation2025-Ver1.9.pptx
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Design an Analysis of Algorithms I-SECS-1021-03
medical staffing services at VALiNTRY

Basic Cmake for Qt Users

  • 1. Integrated Computer Solutions Inc. www.ics.com CMake for QMake Users Chris Rizzitello, ICS September 22, 2022 1
  • 2. Integrated Computer Solutions Inc. www.ics.com About ICS Delivering Smart Devices for a Connected World ● Founded in 1987 ● Largest source of independent Qt expertise in North America ● Trusted Qt Service Partner since 2002 ● Exclusive Open Enrollment Training Partner in North America ● Provides integrated custom software development and user experience (UX) design ● Embedded, touchscreen, mobile and desktop applications ● HQ in Waltham, MA with offices in California, Canada, Europe 2 Boston UX ● Part of the ICS family, focusing on UX design ● Designs intuitive touchscreen interfaces for high-impact embedded and connected medical, industrial and consumer devices
  • 3. Integrated Computer Solutions Inc. www.ics.com What is CMake CMake is an open source, multi platform makefile tool that aids in the building testing and packaging of the software. - Generate native makefiles for the system - Provide rules for deployment - Easily find dependencies - Highly configurable - CMake projects can be opened in many IDEs including QtCreator - QtCreator has basic support for CMake when adding and creating files Qt6 will suggests CMake as its project type 3
  • 4. Integrated Computer Solutions Inc. www.ics.com Example CMake Project #include <QtWidgets> int main(int argc, char* argv[]) { QApplication app(argc, argv); QPushButton button("Hello CMake"); QObject::connect(&button, &QPushButton::clicked, &app, &QApplication::exit); button.show(); return app.exec(); } main.cpp We have the following code, let’s create a simple CMake project for it 4
  • 5. Integrated Computer Solutions Inc. www.ics.com Setting up a CMake project 5 Our Source Code folder looks like this: Create a new file CMakeLists.txt (CaseSensitive) our directory will now look like this: NewProject/ NewProject/main.cpp NewProject/ NewProject/CMakeLists.txt NewProject/main.cpp
  • 6. Integrated Computer Solutions Inc. www.ics.com The First Lines of a main CMakeLists.txt cmake_minimum_required(VERSION 3.21) # This file will require cmake 3.21 to be processed project(hello_cmake) # Our project will be named "hello_cmake" (ONLY REQUIRED ITEM) # Stored in a variable: CMAKE_PROJECT_NAME 6 The project name does not automatically set the name of any target.
  • 7. Integrated Computer Solutions Inc. www.ics.com Find Dependencies and Qt Modules find_package(Qt6 REQUIRED COMPONENTS Widgets ) 7 The COMPONENTS are the Qt Modules that will be used for the project. “core” is imported by all other components. In Qmake project file this is equivalent to QT+=widgets
  • 8. Integrated Computer Solutions Inc. www.ics.com Creating a Target target_link_libraries(hello_cmake PRIVATE Qt6::Core Qt6::Widgets) 8 For Qt 5.15 and Qt6 can be linked dynamically based on the version in use Link with versionless Qt statements to enable this I.E “Qt::Core” or “Qt::Widgets” add_execuitable is a “app” project add_library is a “lib” project Both calls set the name of the that target. Target names can be used several other places, only after defined add_executable(hello_cmake main.cpp)
  • 9. Integrated Computer Solutions Inc. www.ics.com Generating the build and install info install( TARGETS hello_cmake RUNTIME DESTINATION bin ) We can tell Cmake where to install stuff for us 9 TARGETS can have several types - ARCHIVE - LIBRARY - RUNTIME - OBJECTS - FRAMEWORK - BUNDLE - PRIVATE_HEADER - PUBLIC_HEADER - RESOURCE Install - TARGETS - FILES - DIRECTORY
  • 10. Integrated Computer Solutions Inc. www.ics.com CMake Install Prefix All files are installed into the CMAKE_INSTALL_PREFIX (Default: /usr/local) 10 cmake -DCMAKE_INSTALL_PREFIX=<prefix_path> CMakeLists.txt The prefix can be set in the CMakeLists.txt file. It will be used unless overridden at configuration time. set(CMAKE_INSTALL_PREFIX <prefix_path>) At configuration time, the prefix can be changed with :
  • 11. Integrated Computer Solutions Inc. www.ics.com Setting Vars #Set our C++ Standard to C++17, Project will use C++17 standard if possible set(CMAKE_CXX_STANDARD 17) # If the project uses features required from the standard, it needs to be set as required. # In the event the system does not provide the selected standard, the configuration will fail. set(CMAKE_CXX_STANDARD_REQUIRED ON) 11 The following sets the variable to the given value : set(MyVar 5) Setting the C++ Standard requirement : message(“The Value is ${MyVar}”) Later you can use it like this
  • 12. Integrated Computer Solutions Inc. www.ics.com Qt Specific CMake Helpers Normally you would have to make and custom_commands for CMake needs to run moc, rcc and/or uic. With CMake 3.10+ you simply set these variables 12 # Automatically run MOC on the files that need it set(CMAKE_AUTOMOC ON) # Automatically run RCC on the files that need it (resource files) set(CMAKE_AUTORCC ON) # Automatically run UIC on the files that need it (UI files) set(CMAKE_AUTOUIC ON) With the above options qrc and ui files can be used like any other source files.
  • 13. Integrated Computer Solutions Inc. www.ics.com QML Plugins The QmlImportScanner module for Qt can be used to check and link all the QML plugins that are needed for the target: 13 find_package(Qt REQUIRED COMPONENTS ... QmlImportScanner ) qt_import_qml_plugins(target)
  • 14. Integrated Computer Solutions Inc. www.ics.com Creating configuration options Options are easy to create and can change how a project is built. Create an option in the CMakeLists.txt file option(UseFooLib "Link with FooLib" OFF) if (UseFooLib) # Link with Foo lib endif() Set the option during configuration cmake -DUseFooLib=ON CMakeLists.txt 14
  • 15. Integrated Computer Solutions Inc. www.ics.com Configuring files CMake can also use templates to create files project(MyCoolProject) configure_file(file.in file.out @ONLY) CMAKE_CURRENT_SOURCE_DIR/file.in 15 CMakeLists.txt #define ProjectNAME “@CMAKE_PROJECT_NAME@” CMAKE_CURRENT_BINARY_DIR/file.out #define ProjectNAME “MyCoolProject” CMake will will replace any valid CMake variable wrapped in ${VAR} or @VAR@ . In the above example, since @ONLY is passed, CMake will only replace CMake variables wrapped in the format @VAR@
  • 16. Integrated Computer Solutions Inc. www.ics.com Setting up a CMake project Recap 1. Set the minimum version of CMake needed to process the project 2. Set a project name 3. Find dependencies used to build the code 4. Add library or executable that uses the source code 5. Provide linking rules for the lib / exe 6. Optionally add install instructions for the new project 16 The CMakeLists.txt file in the project root folder should contain the following :
  • 17. Integrated Computer Solutions Inc. www.ics.com Setting up a CMake project: Sub folders? 1. Find new dependencies used to build the code in the directory 2. Set source files in this path 3. Add library or executable that uses the sources 4. Provide linking rules for the lib / exe 5. Optionally add install instructions this new component 6. Optionally link a library target with the another part of your project. 17 What about “subdir” projects? You can also use add_subdirectory(foo) to include a folder. Any directory included with the add_subdirectory command must contain a CMakeLists.txt File. This CMakeLists.txt will tell you how handle the source files in that directory.
  • 18. Integrated Computer Solutions Inc. www.ics.com Using the cmake command The cmake command is used to parse the CMakeLists.txt files in your project. Its most basic use is: This will create build files in source. 18 cmake CMakeLists.txt Option Description -S <path> Sets the source directory (-H before version 3.13) -B <path> Set build path, default path is the current directory -D<var>=<value> Set a variable's value to passed -G <generator> Forces the use of a specific generator --graphviz=<file> Create a graphviz of project dependencies --help Show the help for cmake command (easy way to see generators on the system)
  • 19. Integrated Computer Solutions Inc. www.ics.com Shadow Building Steps Shadow building is easy with cmake 1. cd <sourcePath> 2. cmake -S. -B<buildpath> 3. cmake --build <buildpath> This will create all the generated build files in the “buildpath” , Make will build all files into the build path when it’s run 19
  • 20. Integrated Computer Solutions Inc. www.ics.com CMake Times 20
  • 21. Integrated Computer Solutions Inc. www.ics.com Localization With Qt: Generating Ts Files CMAKE 21 set_directory_properties(PROPERTIES CLEAN_NO_CUSTOM 1) #Do not remove *.ts on clean find_package(QtLinguistTools) set(needsTranslation Mainwindow.cpp … More source files ) set(languageFiles myApp_en.ts myApp_fr.ts myApp_de.ts ) qt_create_translation(TSFiles needsTranslation languageFiles ) add_custom_target(TRS ALL DEPENDS ${TSFiles}) find_package(QtLinguistTools) qt_add_translation(QMFiles mainwindow.cpp myApp_en.ts myApp_fr.ts myApp_de.ts ) add_custom_target(QMS ALL DEPENDS ${QMFiles} ) 21 qt_add_translation only runs lrelease, requires pre-existing .ts files qt_create_translation runs both lupdate and lrelease
  • 22. Integrated Computer Solutions Inc. www.ics.com Find Dependencies and Qt Modules Revisited ##using Qt6? find_package(Qt6 REQUIRED COMPONENTS Widgets) target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE Qt::Widgets) ## using Qt5? find_package(Qt5 REQUIRED COMPONENTS Widgets) target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE Qt::Widgets) ## Qt6 but Qt5 if its not found find_package(Qt NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets) target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE Qt${QT_VERSION_MAJOR}::Widgets) ## Qt6, unless the user sets it to 5 when they configure if(NOT QT_DEFAULT_MAJOR_VERSION) set(QT_DEFAULT_MAJOR_VERSION 6 CACHE STRING "" FORCE) endif() find_package(Qt${QT_DEFAULT_MAJOR_VERSION} REQUIRED COMPONENTS Widgets) target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE Qt::Widgets) 22
  • 23. Integrated Computer Solutions Inc. www.ics.com CPack Making Packages 23
  • 24. Integrated Computer Solutions Inc. www.ics.com How Can I Be “CPack Ready” Everything you want to install has install instructions 1. Targets use install(TARGET …) 2. Support files use install(FILES | DIRECTORY …) 24
  • 25. Integrated Computer Solutions Inc. www.ics.com Find macdeployqt / windeployqt get_target_property(_qmake_executable Qt5::qmake IMPORTED_LOCATION) get_filename_component(_qt_bin_dir "${_qmake_executable}" DIRECTORY) find_program(MACDEPLOYQT macdeployqt HINTS "${_qt_bin_dir}") find_program(WINDEPLOYQT windeployqt HINTS "${_qt_bin_dir}") 25 *Cmake has some native ways to find dependencies unfortunately they all currently fail to find the Qt Plugins and at very least force you to make a list of all the plug-ins you need to ship. For this reason its recommended that you use the *deployQt to gather the dependencies. This alone will not run them for you but will let you use them in a post build or pre deployment via add_custom_command similar to how we did w/ the translations in the cmake.
  • 26. Integrated Computer Solutions Inc. www.ics.com Generic CPack set(CPACK_STRIP_FILES [TRUE | FALSE] ) set(CPACK_PACKAGE_NAME “PACKAGENAME”) set(CPACK_RESOURCE_FILE_LICENSE “PATH TO LICENSE FILE”) set(CPACK_PACKAGE_VERSION ${CMAKE_PROJECT_VERSION}) set(CPACK_GENERATOR “TGZ;....”) … INCLUDE (CPack) <- Last Line 26
  • 27. Integrated Computer Solutions Inc. www.ics.com CPack Archive Type Packages Several types of archives can be generated. 27 Generator String Description 7Z Generate a 7-Zip Archive TBZ2 Generate a B Zipped Tarball TGZ Generate a Gnu Zipped Tarball TXS Generate a LZMA Compressed Tarball TZ Generate a Zip Compressed Tarball ZIP Generate a Zip Archive CPACK_ARCHIVE_FILE_NAME and CPACK_ARCHIVE_COMPONENT_INSTALL
  • 28. Integrated Computer Solutions Inc. www.ics.com CPACK Qt Installer Framework Qt Installer can generate installer for Unix, MacOs and Windows. 1. Generator Name: “IFW” Useful Vars to set: ● CPACK_IFW_PACKAGE_TITLE ● CPACK_IFW_PACKAGE_PUBLISHER ● CPACK_IFW_PACKAGE_ICON ● CPACK_IFW_PACKAGE_WINDOW_ICON ● CPACK_IFW_PACKAGE_LOGO ● CPACK_IFW_PACKAGE_RESOURCES ● CPACK_IFW_DOWNLOAD_ALL ● CPACK_IFW_PACKAGE_FILE_EXTENSTION 28 IFW Docs: https://cmake.org/cmake/help/latest/cpack_gen/ifw.html
  • 29. Integrated Computer Solutions Inc. www.ics.com CPACK DMG DMG files are common on mac os as a method of deployment 1. Generator Name: “DragNDrop” 2. For single app install your .app to “.” 3. Run macdeployqt on the app to prepare for deployment 4. Backgrounds are possible Requires: i. IMAGES VOLUME NAME MUST NOT CHANGE ii. CPACK_DMG_BACKGROUND_IMAGE iii. CPACK_DMG_DS_STORE <- DS_Store must be generated by Finder 29
  • 30. Integrated Computer Solutions Inc. www.ics.com CPACK NSIS Nullsoft Scriptable Install System 3.0+ has to be installed. Several Options are unique to the NSIS generator set(CPACK_NSIS_MENU_LINKS “EXE” “LINKNAME”) to generate start menu links CPACK_PACKAGE_INSTALL_DIRECTORY - The path to use for install CPACK_NSIS_EXTRA_INSTALL_COMMANDS - addition nsis commands for install CMAKE_NSIS_EXTRA_UNINSTALL_COMMANDS - additional uninstall nsis commands 30
  • 31. Integrated Computer Solutions Inc. www.ics.com CPACK DEB Build DEB Packages Several Options are unique to the DEB generator Set CPACK_DEBIAN_FILE_NAME to DEB-DEFAULT . This will set the generated name(s) to the default scheme. CPACK_DEBIAN_PACKAGE_SHLIBDEPS [ON | OFF] automatically generate packages depends list CPACK_DEB_COMPONENT_INSTALL [ TRUE | FALSE ] Allow building of split packages CPACK_DEBIAN_<COMPONENT>PACKAGE_NAME - set name for a components package Many other component related vars. REQUIRES: VERSION MUST BE a valid semantic version A CPACK_PACKAGE CONTACT or CPACK_DEBIAN_PACKAGE_MAINTAINER is set 31
  • 32. Integrated Computer Solutions Inc. www.ics.com Thanks for Attending 32 Any questions?