SlideShare a Scribd company logo
© Integrated Computer Solutions, Inc. All Rights Reserved
An Introduction to CMake
Jeff Tranter <jtranter@ics.com> February, 2019
1
© Integrated Computer Solutions, Inc. All Rights Reserved
Agenda
What is CMake?
Features
A Simple Example
A More Complex Example
Qt Support
Qt Example
Qt Creator Support
CTest and CPack
Misc. Tips
Summary
Q&A
References
2
© Integrated Computer Solutions, Inc. All Rights Reserved
What is CMake?
• Cross-platform build system
• Sits on top of and leverages native build system
• Funded by KitWare
• Written in C++
• Support for Qt
• Current version 3.13.3
3
© Integrated Computer Solutions, Inc. All Rights Reserved
Features
• Supports:
• in place and out of place builds
• enabling several builds from the same source tree
• cross-compilation
• support for executables, static and dynamic libraries, generated files
• support for common build systems and SDKs
4
© Integrated Computer Solutions, Inc. All Rights Reserved
A Simple Example
CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(Demo1)
add_executable(Demo1 demo1.cpp)
5
© Integrated Computer Solutions, Inc. All Rights Reserved
A Simple Example
demo1.cpp:
#include <iostream>
int main()
{
std::cout << "Hello, world!" << std::endl;
return 0;
}
6
© Integrated Computer Solutions, Inc. All Rights Reserved
A Simple Example
In source build:
% cd demo1
% ls
CMakeLists.txt demo1.cpp
7
© Integrated Computer Solutions, Inc. All Rights Reserved
A Simple Example
% cmake .
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/tranter/git/jtranter/webinars/CMake/demo1
8
© Integrated Computer Solutions, Inc. All Rights Reserved
A Simple Example
% make
Scanning dependencies of target Demo1
[ 50%] Building CXX object
CMakeFiles/Demo1.dir/demo1.cpp.o
[100%] Linking CXX executable Demo1
[100%] Built target Demo1
% ./Demo1
Hello, world!
9
© Integrated Computer Solutions, Inc. All Rights Reserved
A Simple Example
Generated files:
CMakeCache.txt
CMakeFiles/
cmake_install.cmake
Demo1
Makefile
10
© Integrated Computer Solutions, Inc. All Rights Reserved
A Simple Example
Out of source build:
% cd ..
% mkdir build
% cd build
11
© Integrated Computer Solutions, Inc. All Rights Reserved
A Simple Example
% cmake ../demo1
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/tranter/git/jtranter/webinars/CMake/build
12
© Integrated Computer Solutions, Inc. All Rights Reserved
A Simple Example
% make
Scanning dependencies of target Demo1
[ 50%] Building CXX object
CMakeFiles/Demo1.dir/demo1.cpp.o
[100%] Linking CXX executable Demo1
[100%] Built target Demo1
% ./Demo1
Hello, world!
13
© Integrated Computer Solutions, Inc. All Rights Reserved
A Simple Example
Using cmake-gui:
% cd demo1
% cmake-gui .
14
© Integrated Computer Solutions, Inc. All Rights Reserved
A More Complex Example
To add more source files:
add_executable(Demo2 demo2.cpp hello.cpp)
Or if you have many files:
add_executable(Demo2
demo2.cpp
hello.cpp
foo.cpp
bar.cpp
baz.cpp
)
15
© Integrated Computer Solutions, Inc. All Rights Reserved
A More Complex Example
Add a generated config file, in CMakeLists.txt:
# The version number.
set(Demo2_VERSION_MAJOR 1)
set(Demo2_VERSION_MINOR 0)
# Configure a header file to pass some of the CMake settings to source code
configure_file(
"${PROJECT_SOURCE_DIR}/config.h.in"
"${PROJECT_BINARY_DIR}/config.h"
)
# Add the binary tree to the search path for include files
# so that we will find config.h
include_directories("${PROJECT_BINARY_DIR}")
16
© Integrated Computer Solutions, Inc. All Rights Reserved
A More Complex Example
config.h.in:
// The configured options and settings for Demo2
#define Demo2_VERSION_MAJOR @Demo2_VERSION_MAJOR@
#define Demo2_VERSION_MINOR @Demo2_VERSION_MINOR@
17
© Integrated Computer Solutions, Inc. All Rights Reserved
A More Complex Example
After running cmake, generated config.h:
// The configured options and settings for Demo2
#define Demo2_VERSION_MAJOR 1
#define Demo2_VERSION_MINOR 0
18
© Integrated Computer Solutions, Inc. All Rights Reserved
A More Complex Example
hello.h:
void hello();
hello.cpp:
#include <iostream>
void hello()
{
std::cout << "Hello, world!" << std::endl;
}
19
© Integrated Computer Solutions, Inc. All Rights Reserved
A More Complex Example
demo2.cpp:
#include <iostream>
#include "hello.h"
#include "config.h"
int main()
{
std::cout << "Demo2 version " << Demo2_VERSION_MAJOR
<< "." << Demo2_VERSION_MINOR << std::endl;
hello();
return 0;
}
20
© Integrated Computer Solutions, Inc. All Rights Reserved
A More Complex Example
Add a shared library:
• create mathlib folder for library functions
• create mathlib/mysqrt.cpp, mathlib/mysqrt.h
mathlib/CMakeLists.txt:
# Build a library of math functions
project(mathlib)
add_library(mathlib SHARED mysqrt.cpp)
21
© Integrated Computer Solutions, Inc. All Rights Reserved
A More Complex Example
Additions to top level CMakeLists.txt:
# Add library to include path
include_directories("${PROJECT_SOURCE_DIR}/mathlib")
add_subdirectory(mathlib)
# Link with math library
target_link_libraries(Demo2 mathlib)
22
© Integrated Computer Solutions, Inc. All Rights Reserved
A More Complex Example
Additions to demo2.cpp (highlighted):
#include <iostream>
#include "hello.h"
#include "config.h"
#include "mysqrt.h"
int main()
{
std::cout << "Demo2 version " << Demo2_VERSION_MAJOR << "." << Demo2_VERSION_MINOR <<
std::endl;
hello();
for (double n = 1; n <= 10; n++) {
std::cout << "mysqrt(" << n << ") = " << mysqrt(n) << std::endl;
}
return 0;
}
23
© Integrated Computer Solutions, Inc. All Rights Reserved
A More Complex Example
Add support for install. Additions to CMakeLists.txt:
# Add the install targets
install(TARGETS Demo2 DESTINATION bin)
install(FILES "${PROJECT_BINARY_DIR}/config.h" DESTINATION include)
Additions to mathlib/CMakeLists.txt:
# Install targets
install(TARGETS mathlib DESTINATION lib)
install(FILES mysqrt.h DESTINATION include)
24
© Integrated Computer Solutions, Inc. All Rights Reserved
A More Complex Example
Building it (out of source):
% mkdir build
% cd build/
% cmake ../demo2
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/tranter/git/jtranter/webinars/CMake/build
25
© Integrated Computer Solutions, Inc. All Rights Reserved
A More Complex Example
% make
Scanning dependencies of target mathlib
[ 20%] Building CXX object mathlib/CMakeFiles/mathlib.dir/mysqrt.cpp.o
[ 40%] Linking CXX shared library libmathlib.so
[ 40%] Built target mathlib
Scanning dependencies of target Demo2
[ 60%] Building CXX object CMakeFiles/Demo2.dir/demo2.cpp.o
[ 80%] Building CXX object CMakeFiles/Demo2.dir/hello.cpp.o
[100%] Linking CXX executable Demo2
[100%] Built target Demo2
26
© Integrated Computer Solutions, Inc. All Rights Reserved
A More Complex Example
% ./Demo2
Demo2 version 1.0
Hello, world!
mysqrt(1) = 1
mysqrt(2) = 1.41421
mysqrt(3) = 1.73205
mysqrt(4) = 2
mysqrt(5) = 2.23607
mysqrt(6) = 2.44949
mysqrt(7) = 2.64575
mysqrt(8) = 2.82843
mysqrt(9) = 3
mysqrt(10) = 3.16228
27
© Integrated Computer Solutions, Inc. All Rights Reserved
A More Complex Example
% sudo make install
[ 40%] Built target mathlib
[100%] Built target Demo2
Install the project...
-- Install configuration: ""
-- Installing: /usr/local/bin/Demo2
-- Set runtime path of "/usr/local/bin/Demo2" to ""
-- Installing: /usr/local/include/config.h
-- Installing: /usr/local/lib/libmathlib.so
-- Installing: /usr/local/include/mysqrt.h
28
© Integrated Computer Solutions, Inc. All Rights Reserved
A More Complex Example
% ls
CMakeCache.txt CMakeFiles cmake_install.cmake
config.h Demo2 Makefile mathlib
% ls mathlib
CMakeFiles cmake_install.cmake libmathlib.so Makefile
29
© Integrated Computer Solutions, Inc. All Rights Reserved
Qt Support
• Part of standard CMake
• Requires CMake 3.1.0 or later
• Knows how to find Qt libraries
• Knows how to handle moc, UI files, resources
30
© Integrated Computer Solutions, Inc. All Rights Reserved
Qt Support
• CMake can find and use Qt 4 and Qt 5 libraries
• Automatically invokes moc, uic, rcc as needed
• AUTOMOC property controls automatic generation of moc
• AUTOUIC property controls automatic invocation of uic for UI files
• AUTORCC property controls automatic invocation of rcc for resource
(.qrc) files
31
© Integrated Computer Solutions, Inc. All Rights Reserved
Qt Example
Simple Qt Creator wizard generated application:
main.cpp mainwindow.cpp mainwindow.h mainwindow.ui
32
© Integrated Computer Solutions, Inc. All Rights Reserved
Qt Example
CMakeLists.txt file:
cmake_minimum_required(VERSION 3.1.0)
project(Demo3)
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed
set(CMAKE_AUTOMOC ON)
# Create code from a list of Qt designer ui files
set(CMAKE_AUTOUIC ON)
33
© Integrated Computer Solutions, Inc. All Rights Reserved
Qt Example
CMakeLists.txt file (continued):
# Find the QtWidgets library
find_package(Qt5Widgets CONFIG REQUIRED)
# Populate a CMake variable with the sources
set(demo3_SRCS
mainwindow.ui
mainwindow.cpp
main.cpp
)
# Tell CMake to create the Demo3 executable
add_executable(Demo3 WIN32 ${demo3_SRCS})
# Use the Widgets module from Qt 5
target_link_libraries(Demo3 Qt5::Widgets)
34
© Integrated Computer Solutions, Inc. All Rights Reserved
Qt Example
% cmake ../demo3
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/tranter/git/jtranter/webinars/CMake/build
35
© Integrated Computer Solutions, Inc. All Rights Reserved
Qt Example
% make
Scanning dependencies of target Demo3_autogen
[ 20%] Automatic MOC and UIC for target Demo3
[ 20%] Built target Demo3_autogen
Scanning dependencies of target Demo3
[ 40%] Building CXX object CMakeFiles/Demo3.dir/mainwindow.cpp.o
[ 60%] Building CXX object CMakeFiles/Demo3.dir/main.cpp.o
[ 80%] Building CXX object CMakeFiles/Demo3.dir/Demo3_autogen/mocs_compilation.cpp.o
[100%] Linking CXX executable Demo3
[100%] Built target Demo3
% ./Demo3
36
© Integrated Computer Solutions, Inc. All Rights Reserved
Qt Example
• Larger spreadsheet program
• Qt 5 port of program from C++ GUI Programming with Qt 4 book
• Widget-based, 13 source files, 2 UI files, resource file, 1300 LOC
37
© Integrated Computer Solutions, Inc. All Rights Reserved
Qt Example
Original qmake project file spreadsheet.pro:
lessThan(QT_MAJOR_VERSION, 5): error(This project requires Qt 5 or later)
TEMPLATE = app
QT += widgets
HEADERS = cell.h 
finddialog.h 
gotocelldialog.h 
mainwindow.h 
sortdialog.h 
spreadsheet.h
SOURCES = cell.cpp 
finddialog.cpp 
gotocelldialog.cpp 
main.cpp 
mainwindow.cpp 
sortdialog.cpp 
spreadsheet.cpp
FORMS = gotocelldialog.ui 
sortdialog.ui
RESOURCES = spreadsheet.qrc
38
© Integrated Computer Solutions, Inc. All Rights Reserved
Qt Example
CMakeLists.txt file:
cmake_minimum_required(VERSION 3.1.0)
project(spreadsheet)
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed
set(CMAKE_AUTOMOC ON)
# Create code from a list of Qt designer ui files
set(CMAKE_AUTOUIC ON)
# Automatically handle resource files
set(CMAKE_AUTORCC ON)
39
© Integrated Computer Solutions, Inc. All Rights Reserved
Qt Example
CMakeLists.txt file (continued):
# Find the QtWidgets library
find_package(Qt5Widgets CONFIG REQUIRED)
# Populate a CMake variable with the sources
set(spreadsheet_SRCS
cell.cpp
finddialog.cpp
gotocelldialog.cpp
main.cpp
mainwindow.cpp
sortdialog.cpp
spreadsheet.cpp
spreadsheet.qrc
)
# Tell CMake to create the spreadsheet executable
add_executable(spreadsheet WIN32 ${spreadsheet_SRCS})
# Use the Widgets module from Qt 5
target_link_libraries(spreadsheet Qt5::Widgets)
40
© Integrated Computer Solutions, Inc. All Rights Reserved
Qt Example
% cmake ~/git/spreadsheet/
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/tranter/git/jtranter/webinars/CMake/build/work
41
© Integrated Computer Solutions, Inc. All Rights Reserved
Qt Example
% make
Scanning dependencies of target spreadsheet_autogen
[ 8%] Automatic MOC, UIC, and RCC for target spreadsheet
[ 8%] Built target spreadsheet_autogen
[ 16%] Generating qrc_spreadsheet.cpp
Scanning dependencies of target spreadsheet
[ 25%] Building CXX object CMakeFiles/spreadsheet.dir/cell.cpp.o
[ 33%] Building CXX object CMakeFiles/spreadsheet.dir/finddialog.cpp.o
[ 41%] Building CXX object CMakeFiles/spreadsheet.dir/gotocelldialog.cpp.o
[ 50%] Building CXX object CMakeFiles/spreadsheet.dir/main.cpp.o
[ 58%] Building CXX object CMakeFiles/spreadsheet.dir/mainwindow.cpp.o
[ 66%] Building CXX object CMakeFiles/spreadsheet.dir/sortdialog.cpp.o
[ 75%] Building CXX object CMakeFiles/spreadsheet.dir/spreadsheet.cpp.o
[ 83%] Building CXX object CMakeFiles/spreadsheet.dir/qrc_spreadsheet.cpp.o
[ 91%] Building CXX object
CMakeFiles/spreadsheet.dir/spreadsheet_autogen/mocs_compilation.cpp.o
[100%] Linking CXX executable spreadsheet
[100%] Built target spreadsheet
42
© Integrated Computer Solutions, Inc. All Rights Reserved
Localization Support
FIND_PACKAGE(Qt5LinguistTools)
# Translation files
SET(TS_FILES
spreadsheet_de.ts
spreadsheet_fr.ts
)
qt5_add_translation(QM_FILES ${TS_FILES})
# Tell CMake to create the spreadsheet executable
add_executable(spreadsheet WIN32 ${spreadsheet_SRCS} ${QM_FILES})
43
© Integrated Computer Solutions, Inc. All Rights Reserved
Localization Support
% cmake ../spreadsheet
...
[ 15%] Generating spreadsheet_fr.qm
Updating '/home/tranter/git/inactive/build/spreadsheet_fr.qm'...
Generated 1 translation(s) (1 finished and 0 unfinished)
Ignored 97 untranslated source text(s)
[ 23%] Generating spreadsheet_de.qm
Updating '/home/tranter/git/inactive/build/spreadsheet_de.qm'...
Generated 1 translation(s) (1 finished and 0 unfinished)
Ignored 97 untranslated source text(s)
...
% LANGUAGE=de ./spreadsheet
44
© Integrated Computer Solutions, Inc. All Rights Reserved
Qt Creator Support
• New project wizard can create a non-Qt C++ CMake project, but Qt-based
projects all default to qmake
• You can open an existing CMake-based project and use it
• Can edit CMake variables in project settings pane
• Keeps it's settings in a CMakeLists.txt.user file (similar to .pro.user files
with qmake)
• Make sure you enable the CMakeProjectManager plugin in Qt Creator
• Also check settings under Tools / Options... Kits/ CMake
45
© Integrated Computer Solutions, Inc. All Rights Reserved
Qt Creator Support
46
© Integrated Computer Solutions, Inc. All Rights Reserved
Qt Creator Support
47
© Integrated Computer Solutions, Inc. All Rights Reserved
Qt Creator Support
48
© Integrated Computer Solutions, Inc. All Rights Reserved
CTest
• CMake has support for defining and running unit tests
• Uses the ctest program
• See the documentation for details
49
© Integrated Computer Solutions, Inc. All Rights Reserved
CPack
• Support for packaging applications via the "cpack" program and
associated configuration files
• See the documentation for details
50
© Integrated Computer Solutions, Inc. All Rights Reserved
Misc. Tips
• For more verbose output when building: make VERBOSE=1
• Example of a sub-directories only project:
cmake_minimum_required(VERSION 3.0)
add_subdirectory(demo1)
add_subdirectory(demo2)
add_subdirectory(demo3)
51
© Integrated Computer Solutions, Inc. All Rights Reserved
Summary
• Use of CMake is increasing
• May be Qt's build system in Qt 6
• Relatively easy to use with Qt projects and Qt Creator
52
© Integrated Computer Solutions, Inc. All Rights Reserved
References
• https://en.wikipedia.org/wiki/CMake
• https://cmake.org/
• https://cmake.org/cmake-tutorial/
• https://cmake.org/cmake/help/latest/index.html
• https://cmake.org/download/
• https://cmake.org/cmake/help/latest/manual/cmake-qt.7.html#manual:
cmake-qt(7)
• http://doc.qt.io/qt-5/cmake-manual.html
• http://doc.qt.io/qtcreator/creator-project-cmake.html
• https://github.com/tranter/webinars/tree/master/CMake
• https://wiki.qt.io/CMake_Port
53
© Integrated Computer Solutions, Inc. All Rights Reserved
Q&A
• Questions?
• Any tips to share?
54

More Related Content

PDF
Introduction to CMake
Dimitrios Platis
 
PDF
CMake - Introduction and best practices
Daniel Pfeifer
 
PDF
Effective CMake
Daniel Pfeifer
 
PDF
Cmake kitware
achintyalte
 
PDF
Tutoriel GIT
Francois ANDRE
 
PDF
Modern c++ (C++ 11/14)
Geeks Anonymes
 
PPTX
02 terraform core concepts
zekeLabs Technologies
 
PDF
Qemu Introduction
Chiawei Wang
 
Introduction to CMake
Dimitrios Platis
 
CMake - Introduction and best practices
Daniel Pfeifer
 
Effective CMake
Daniel Pfeifer
 
Cmake kitware
achintyalte
 
Tutoriel GIT
Francois ANDRE
 
Modern c++ (C++ 11/14)
Geeks Anonymes
 
02 terraform core concepts
zekeLabs Technologies
 
Qemu Introduction
Chiawei Wang
 

What's hot (20)

PPTX
QEMU - Binary Translation
Jiann-Fuh Liaw
 
PDF
ARM Trusted FirmwareのBL31を単体で使う!
Mr. Vengineer
 
PDF
Terraform introduction
Jason Vance
 
PDF
Reactive Extensionsで非同期処理を簡単に
Yoshifumi Kawai
 
PDF
Git and github 101
Senthilkumar Gopal
 
PDF
kubernetes, pourquoi et comment
Jean-Baptiste Claramonte
 
PDF
Spring Boot & Containers - Do's & Don'ts
Julien Wittouck
 
PDF
[143] Modern C++ 무조건 써야 해?
NAVER D2
 
PPTX
Rust vs C++
corehard_by
 
PDF
Chapitre 6 traitement des exceptions
Amir Souissi
 
PPT
Shell Scripting
Gaurav Shinde
 
PDF
Qt programming-using-cpp
Emertxe Information Technologies Pvt Ltd
 
PDF
Rust system programming language
robin_sy
 
PDF
Introduction to Rust
Jean Carlo Machado
 
PPTX
Unix shell scripting basics
Manav Prasad
 
PPTX
Introduction to Makefile
Zakaria El ktaoui
 
PDF
Rust: Systems Programming for Everyone
C4Media
 
PDF
The Rust Programming Language: an Overview
Roberto Casadei
 
PPTX
Introduction to Qt
Puja Pramudya
 
PDF
Conan.io - The C/C++ package manager for Developers
Uilian Ries
 
QEMU - Binary Translation
Jiann-Fuh Liaw
 
ARM Trusted FirmwareのBL31を単体で使う!
Mr. Vengineer
 
Terraform introduction
Jason Vance
 
Reactive Extensionsで非同期処理を簡単に
Yoshifumi Kawai
 
Git and github 101
Senthilkumar Gopal
 
kubernetes, pourquoi et comment
Jean-Baptiste Claramonte
 
Spring Boot & Containers - Do's & Don'ts
Julien Wittouck
 
[143] Modern C++ 무조건 써야 해?
NAVER D2
 
Rust vs C++
corehard_by
 
Chapitre 6 traitement des exceptions
Amir Souissi
 
Shell Scripting
Gaurav Shinde
 
Rust system programming language
robin_sy
 
Introduction to Rust
Jean Carlo Machado
 
Unix shell scripting basics
Manav Prasad
 
Introduction to Makefile
Zakaria El ktaoui
 
Rust: Systems Programming for Everyone
C4Media
 
The Rust Programming Language: an Overview
Roberto Casadei
 
Introduction to Qt
Puja Pramudya
 
Conan.io - The C/C++ package manager for Developers
Uilian Ries
 
Ad

Similar to An Introduction to CMake (20)

PDF
Basic Cmake for Qt Users
ICS
 
PDF
CMake_Tutorial.pdf
Thejeswara Reddy
 
PDF
cmake.pdf
Thejeswara Reddy
 
PDF
C make tutorial
zeng724
 
PDF
CMake best practices
Henry Schreiner
 
PDF
CMake Tutorial
Fu Haiping
 
PDF
Basicsof c make and git for a hello qt application
Dinesh Manajipet
 
PPTX
Feature and platform testing with CMake
Richard Thomson
 
PDF
CMake: Improving Software Quality and Process
Marcus Hanwell
 
PPTX
short_intro_to_CMake_(inria_REVES_team)
Jérôme Esnault
 
PDF
IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)
Alexandre Gouaillard
 
PPTX
Autotools pratical training
Thierry Gayet
 
PPTX
CiklumCPPSat: Alexey Podoba "Automatic assembly. Cmake"
Ciklum Ukraine
 
PDF
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
Ahmed El-Arabawy
 
PDF
GNU Compiler Collection - August 2005
Saleem Ansari
 
PDF
Compiler design notes phases of compiler
ovidlivi91
 
TXT
C make cache
lauro reyes
 
PPTX
Build process in ST Visual Develop
Gourav Kumar
 
PDF
Overcoming CMake Configuration Issues Webinar
ICS
 
Basic Cmake for Qt Users
ICS
 
CMake_Tutorial.pdf
Thejeswara Reddy
 
cmake.pdf
Thejeswara Reddy
 
C make tutorial
zeng724
 
CMake best practices
Henry Schreiner
 
CMake Tutorial
Fu Haiping
 
Basicsof c make and git for a hello qt application
Dinesh Manajipet
 
Feature and platform testing with CMake
Richard Thomson
 
CMake: Improving Software Quality and Process
Marcus Hanwell
 
short_intro_to_CMake_(inria_REVES_team)
Jérôme Esnault
 
IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)
Alexandre Gouaillard
 
Autotools pratical training
Thierry Gayet
 
CiklumCPPSat: Alexey Podoba "Automatic assembly. Cmake"
Ciklum Ukraine
 
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
Ahmed El-Arabawy
 
GNU Compiler Collection - August 2005
Saleem Ansari
 
Compiler design notes phases of compiler
ovidlivi91
 
C make cache
lauro reyes
 
Build process in ST Visual Develop
Gourav Kumar
 
Overcoming CMake Configuration Issues Webinar
ICS
 
Ad

More from ICS (20)

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

Recently uploaded (20)

PDF
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
PDF
vAdobe Premiere Pro 2025 (v25.2.3.004) Crack Pre-Activated Latest
imang66g
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
PPT
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PDF
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
PDF
Exploring AI Agents in Process Industries
amoreira6
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PPTX
oapresentation.pptx
mehatdhavalrajubhai
 
PPTX
Explanation about Structures in C language.pptx
Veeral Rathod
 
PDF
Protecting the Digital World Cyber Securit
dnthakkar16
 
PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PDF
New Download MiniTool Partition Wizard Crack Latest Version 2025
imang66g
 
PDF
Bandai Playdia The Book - David Glotz
BluePanther6
 
PDF
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
PPTX
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
PDF
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
PDF
New Download FL Studio Crack Full Version [Latest 2025]
imang66g
 
PDF
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
 
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
vAdobe Premiere Pro 2025 (v25.2.3.004) Crack Pre-Activated Latest
imang66g
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
Exploring AI Agents in Process Industries
amoreira6
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
oapresentation.pptx
mehatdhavalrajubhai
 
Explanation about Structures in C language.pptx
Veeral Rathod
 
Protecting the Digital World Cyber Securit
dnthakkar16
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
New Download MiniTool Partition Wizard Crack Latest Version 2025
imang66g
 
Bandai Playdia The Book - David Glotz
BluePanther6
 
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
New Download FL Studio Crack Full Version [Latest 2025]
imang66g
 
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
 

An Introduction to CMake

  • 1. © Integrated Computer Solutions, Inc. All Rights Reserved An Introduction to CMake Jeff Tranter <[email protected]> February, 2019 1
  • 2. © Integrated Computer Solutions, Inc. All Rights Reserved Agenda What is CMake? Features A Simple Example A More Complex Example Qt Support Qt Example Qt Creator Support CTest and CPack Misc. Tips Summary Q&A References 2
  • 3. © Integrated Computer Solutions, Inc. All Rights Reserved What is CMake? • Cross-platform build system • Sits on top of and leverages native build system • Funded by KitWare • Written in C++ • Support for Qt • Current version 3.13.3 3
  • 4. © Integrated Computer Solutions, Inc. All Rights Reserved Features • Supports: • in place and out of place builds • enabling several builds from the same source tree • cross-compilation • support for executables, static and dynamic libraries, generated files • support for common build systems and SDKs 4
  • 5. © Integrated Computer Solutions, Inc. All Rights Reserved A Simple Example CMakeLists.txt: cmake_minimum_required(VERSION 3.0) project(Demo1) add_executable(Demo1 demo1.cpp) 5
  • 6. © Integrated Computer Solutions, Inc. All Rights Reserved A Simple Example demo1.cpp: #include <iostream> int main() { std::cout << "Hello, world!" << std::endl; return 0; } 6
  • 7. © Integrated Computer Solutions, Inc. All Rights Reserved A Simple Example In source build: % cd demo1 % ls CMakeLists.txt demo1.cpp 7
  • 8. © Integrated Computer Solutions, Inc. All Rights Reserved A Simple Example % cmake . -- The C compiler identification is GNU 7.3.0 -- The CXX compiler identification is GNU 7.3.0 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: /home/tranter/git/jtranter/webinars/CMake/demo1 8
  • 9. © Integrated Computer Solutions, Inc. All Rights Reserved A Simple Example % make Scanning dependencies of target Demo1 [ 50%] Building CXX object CMakeFiles/Demo1.dir/demo1.cpp.o [100%] Linking CXX executable Demo1 [100%] Built target Demo1 % ./Demo1 Hello, world! 9
  • 10. © Integrated Computer Solutions, Inc. All Rights Reserved A Simple Example Generated files: CMakeCache.txt CMakeFiles/ cmake_install.cmake Demo1 Makefile 10
  • 11. © Integrated Computer Solutions, Inc. All Rights Reserved A Simple Example Out of source build: % cd .. % mkdir build % cd build 11
  • 12. © Integrated Computer Solutions, Inc. All Rights Reserved A Simple Example % cmake ../demo1 -- The C compiler identification is GNU 7.3.0 -- The CXX compiler identification is GNU 7.3.0 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: /home/tranter/git/jtranter/webinars/CMake/build 12
  • 13. © Integrated Computer Solutions, Inc. All Rights Reserved A Simple Example % make Scanning dependencies of target Demo1 [ 50%] Building CXX object CMakeFiles/Demo1.dir/demo1.cpp.o [100%] Linking CXX executable Demo1 [100%] Built target Demo1 % ./Demo1 Hello, world! 13
  • 14. © Integrated Computer Solutions, Inc. All Rights Reserved A Simple Example Using cmake-gui: % cd demo1 % cmake-gui . 14
  • 15. © Integrated Computer Solutions, Inc. All Rights Reserved A More Complex Example To add more source files: add_executable(Demo2 demo2.cpp hello.cpp) Or if you have many files: add_executable(Demo2 demo2.cpp hello.cpp foo.cpp bar.cpp baz.cpp ) 15
  • 16. © Integrated Computer Solutions, Inc. All Rights Reserved A More Complex Example Add a generated config file, in CMakeLists.txt: # The version number. set(Demo2_VERSION_MAJOR 1) set(Demo2_VERSION_MINOR 0) # Configure a header file to pass some of the CMake settings to source code configure_file( "${PROJECT_SOURCE_DIR}/config.h.in" "${PROJECT_BINARY_DIR}/config.h" ) # Add the binary tree to the search path for include files # so that we will find config.h include_directories("${PROJECT_BINARY_DIR}") 16
  • 17. © Integrated Computer Solutions, Inc. All Rights Reserved A More Complex Example config.h.in: // The configured options and settings for Demo2 #define Demo2_VERSION_MAJOR @Demo2_VERSION_MAJOR@ #define Demo2_VERSION_MINOR @Demo2_VERSION_MINOR@ 17
  • 18. © Integrated Computer Solutions, Inc. All Rights Reserved A More Complex Example After running cmake, generated config.h: // The configured options and settings for Demo2 #define Demo2_VERSION_MAJOR 1 #define Demo2_VERSION_MINOR 0 18
  • 19. © Integrated Computer Solutions, Inc. All Rights Reserved A More Complex Example hello.h: void hello(); hello.cpp: #include <iostream> void hello() { std::cout << "Hello, world!" << std::endl; } 19
  • 20. © Integrated Computer Solutions, Inc. All Rights Reserved A More Complex Example demo2.cpp: #include <iostream> #include "hello.h" #include "config.h" int main() { std::cout << "Demo2 version " << Demo2_VERSION_MAJOR << "." << Demo2_VERSION_MINOR << std::endl; hello(); return 0; } 20
  • 21. © Integrated Computer Solutions, Inc. All Rights Reserved A More Complex Example Add a shared library: • create mathlib folder for library functions • create mathlib/mysqrt.cpp, mathlib/mysqrt.h mathlib/CMakeLists.txt: # Build a library of math functions project(mathlib) add_library(mathlib SHARED mysqrt.cpp) 21
  • 22. © Integrated Computer Solutions, Inc. All Rights Reserved A More Complex Example Additions to top level CMakeLists.txt: # Add library to include path include_directories("${PROJECT_SOURCE_DIR}/mathlib") add_subdirectory(mathlib) # Link with math library target_link_libraries(Demo2 mathlib) 22
  • 23. © Integrated Computer Solutions, Inc. All Rights Reserved A More Complex Example Additions to demo2.cpp (highlighted): #include <iostream> #include "hello.h" #include "config.h" #include "mysqrt.h" int main() { std::cout << "Demo2 version " << Demo2_VERSION_MAJOR << "." << Demo2_VERSION_MINOR << std::endl; hello(); for (double n = 1; n <= 10; n++) { std::cout << "mysqrt(" << n << ") = " << mysqrt(n) << std::endl; } return 0; } 23
  • 24. © Integrated Computer Solutions, Inc. All Rights Reserved A More Complex Example Add support for install. Additions to CMakeLists.txt: # Add the install targets install(TARGETS Demo2 DESTINATION bin) install(FILES "${PROJECT_BINARY_DIR}/config.h" DESTINATION include) Additions to mathlib/CMakeLists.txt: # Install targets install(TARGETS mathlib DESTINATION lib) install(FILES mysqrt.h DESTINATION include) 24
  • 25. © Integrated Computer Solutions, Inc. All Rights Reserved A More Complex Example Building it (out of source): % mkdir build % cd build/ % cmake ../demo2 -- The C compiler identification is GNU 7.3.0 -- The CXX compiler identification is GNU 7.3.0 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: /home/tranter/git/jtranter/webinars/CMake/build 25
  • 26. © Integrated Computer Solutions, Inc. All Rights Reserved A More Complex Example % make Scanning dependencies of target mathlib [ 20%] Building CXX object mathlib/CMakeFiles/mathlib.dir/mysqrt.cpp.o [ 40%] Linking CXX shared library libmathlib.so [ 40%] Built target mathlib Scanning dependencies of target Demo2 [ 60%] Building CXX object CMakeFiles/Demo2.dir/demo2.cpp.o [ 80%] Building CXX object CMakeFiles/Demo2.dir/hello.cpp.o [100%] Linking CXX executable Demo2 [100%] Built target Demo2 26
  • 27. © Integrated Computer Solutions, Inc. All Rights Reserved A More Complex Example % ./Demo2 Demo2 version 1.0 Hello, world! mysqrt(1) = 1 mysqrt(2) = 1.41421 mysqrt(3) = 1.73205 mysqrt(4) = 2 mysqrt(5) = 2.23607 mysqrt(6) = 2.44949 mysqrt(7) = 2.64575 mysqrt(8) = 2.82843 mysqrt(9) = 3 mysqrt(10) = 3.16228 27
  • 28. © Integrated Computer Solutions, Inc. All Rights Reserved A More Complex Example % sudo make install [ 40%] Built target mathlib [100%] Built target Demo2 Install the project... -- Install configuration: "" -- Installing: /usr/local/bin/Demo2 -- Set runtime path of "/usr/local/bin/Demo2" to "" -- Installing: /usr/local/include/config.h -- Installing: /usr/local/lib/libmathlib.so -- Installing: /usr/local/include/mysqrt.h 28
  • 29. © Integrated Computer Solutions, Inc. All Rights Reserved A More Complex Example % ls CMakeCache.txt CMakeFiles cmake_install.cmake config.h Demo2 Makefile mathlib % ls mathlib CMakeFiles cmake_install.cmake libmathlib.so Makefile 29
  • 30. © Integrated Computer Solutions, Inc. All Rights Reserved Qt Support • Part of standard CMake • Requires CMake 3.1.0 or later • Knows how to find Qt libraries • Knows how to handle moc, UI files, resources 30
  • 31. © Integrated Computer Solutions, Inc. All Rights Reserved Qt Support • CMake can find and use Qt 4 and Qt 5 libraries • Automatically invokes moc, uic, rcc as needed • AUTOMOC property controls automatic generation of moc • AUTOUIC property controls automatic invocation of uic for UI files • AUTORCC property controls automatic invocation of rcc for resource (.qrc) files 31
  • 32. © Integrated Computer Solutions, Inc. All Rights Reserved Qt Example Simple Qt Creator wizard generated application: main.cpp mainwindow.cpp mainwindow.h mainwindow.ui 32
  • 33. © Integrated Computer Solutions, Inc. All Rights Reserved Qt Example CMakeLists.txt file: cmake_minimum_required(VERSION 3.1.0) project(Demo3) # Find includes in corresponding build directories set(CMAKE_INCLUDE_CURRENT_DIR ON) # Instruct CMake to run moc automatically when needed set(CMAKE_AUTOMOC ON) # Create code from a list of Qt designer ui files set(CMAKE_AUTOUIC ON) 33
  • 34. © Integrated Computer Solutions, Inc. All Rights Reserved Qt Example CMakeLists.txt file (continued): # Find the QtWidgets library find_package(Qt5Widgets CONFIG REQUIRED) # Populate a CMake variable with the sources set(demo3_SRCS mainwindow.ui mainwindow.cpp main.cpp ) # Tell CMake to create the Demo3 executable add_executable(Demo3 WIN32 ${demo3_SRCS}) # Use the Widgets module from Qt 5 target_link_libraries(Demo3 Qt5::Widgets) 34
  • 35. © Integrated Computer Solutions, Inc. All Rights Reserved Qt Example % cmake ../demo3 -- The C compiler identification is GNU 7.3.0 -- The CXX compiler identification is GNU 7.3.0 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: /home/tranter/git/jtranter/webinars/CMake/build 35
  • 36. © Integrated Computer Solutions, Inc. All Rights Reserved Qt Example % make Scanning dependencies of target Demo3_autogen [ 20%] Automatic MOC and UIC for target Demo3 [ 20%] Built target Demo3_autogen Scanning dependencies of target Demo3 [ 40%] Building CXX object CMakeFiles/Demo3.dir/mainwindow.cpp.o [ 60%] Building CXX object CMakeFiles/Demo3.dir/main.cpp.o [ 80%] Building CXX object CMakeFiles/Demo3.dir/Demo3_autogen/mocs_compilation.cpp.o [100%] Linking CXX executable Demo3 [100%] Built target Demo3 % ./Demo3 36
  • 37. © Integrated Computer Solutions, Inc. All Rights Reserved Qt Example • Larger spreadsheet program • Qt 5 port of program from C++ GUI Programming with Qt 4 book • Widget-based, 13 source files, 2 UI files, resource file, 1300 LOC 37
  • 38. © Integrated Computer Solutions, Inc. All Rights Reserved Qt Example Original qmake project file spreadsheet.pro: lessThan(QT_MAJOR_VERSION, 5): error(This project requires Qt 5 or later) TEMPLATE = app QT += widgets HEADERS = cell.h finddialog.h gotocelldialog.h mainwindow.h sortdialog.h spreadsheet.h SOURCES = cell.cpp finddialog.cpp gotocelldialog.cpp main.cpp mainwindow.cpp sortdialog.cpp spreadsheet.cpp FORMS = gotocelldialog.ui sortdialog.ui RESOURCES = spreadsheet.qrc 38
  • 39. © Integrated Computer Solutions, Inc. All Rights Reserved Qt Example CMakeLists.txt file: cmake_minimum_required(VERSION 3.1.0) project(spreadsheet) # Find includes in corresponding build directories set(CMAKE_INCLUDE_CURRENT_DIR ON) # Instruct CMake to run moc automatically when needed set(CMAKE_AUTOMOC ON) # Create code from a list of Qt designer ui files set(CMAKE_AUTOUIC ON) # Automatically handle resource files set(CMAKE_AUTORCC ON) 39
  • 40. © Integrated Computer Solutions, Inc. All Rights Reserved Qt Example CMakeLists.txt file (continued): # Find the QtWidgets library find_package(Qt5Widgets CONFIG REQUIRED) # Populate a CMake variable with the sources set(spreadsheet_SRCS cell.cpp finddialog.cpp gotocelldialog.cpp main.cpp mainwindow.cpp sortdialog.cpp spreadsheet.cpp spreadsheet.qrc ) # Tell CMake to create the spreadsheet executable add_executable(spreadsheet WIN32 ${spreadsheet_SRCS}) # Use the Widgets module from Qt 5 target_link_libraries(spreadsheet Qt5::Widgets) 40
  • 41. © Integrated Computer Solutions, Inc. All Rights Reserved Qt Example % cmake ~/git/spreadsheet/ -- The C compiler identification is GNU 7.3.0 -- The CXX compiler identification is GNU 7.3.0 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: /home/tranter/git/jtranter/webinars/CMake/build/work 41
  • 42. © Integrated Computer Solutions, Inc. All Rights Reserved Qt Example % make Scanning dependencies of target spreadsheet_autogen [ 8%] Automatic MOC, UIC, and RCC for target spreadsheet [ 8%] Built target spreadsheet_autogen [ 16%] Generating qrc_spreadsheet.cpp Scanning dependencies of target spreadsheet [ 25%] Building CXX object CMakeFiles/spreadsheet.dir/cell.cpp.o [ 33%] Building CXX object CMakeFiles/spreadsheet.dir/finddialog.cpp.o [ 41%] Building CXX object CMakeFiles/spreadsheet.dir/gotocelldialog.cpp.o [ 50%] Building CXX object CMakeFiles/spreadsheet.dir/main.cpp.o [ 58%] Building CXX object CMakeFiles/spreadsheet.dir/mainwindow.cpp.o [ 66%] Building CXX object CMakeFiles/spreadsheet.dir/sortdialog.cpp.o [ 75%] Building CXX object CMakeFiles/spreadsheet.dir/spreadsheet.cpp.o [ 83%] Building CXX object CMakeFiles/spreadsheet.dir/qrc_spreadsheet.cpp.o [ 91%] Building CXX object CMakeFiles/spreadsheet.dir/spreadsheet_autogen/mocs_compilation.cpp.o [100%] Linking CXX executable spreadsheet [100%] Built target spreadsheet 42
  • 43. © Integrated Computer Solutions, Inc. All Rights Reserved Localization Support FIND_PACKAGE(Qt5LinguistTools) # Translation files SET(TS_FILES spreadsheet_de.ts spreadsheet_fr.ts ) qt5_add_translation(QM_FILES ${TS_FILES}) # Tell CMake to create the spreadsheet executable add_executable(spreadsheet WIN32 ${spreadsheet_SRCS} ${QM_FILES}) 43
  • 44. © Integrated Computer Solutions, Inc. All Rights Reserved Localization Support % cmake ../spreadsheet ... [ 15%] Generating spreadsheet_fr.qm Updating '/home/tranter/git/inactive/build/spreadsheet_fr.qm'... Generated 1 translation(s) (1 finished and 0 unfinished) Ignored 97 untranslated source text(s) [ 23%] Generating spreadsheet_de.qm Updating '/home/tranter/git/inactive/build/spreadsheet_de.qm'... Generated 1 translation(s) (1 finished and 0 unfinished) Ignored 97 untranslated source text(s) ... % LANGUAGE=de ./spreadsheet 44
  • 45. © Integrated Computer Solutions, Inc. All Rights Reserved Qt Creator Support • New project wizard can create a non-Qt C++ CMake project, but Qt-based projects all default to qmake • You can open an existing CMake-based project and use it • Can edit CMake variables in project settings pane • Keeps it's settings in a CMakeLists.txt.user file (similar to .pro.user files with qmake) • Make sure you enable the CMakeProjectManager plugin in Qt Creator • Also check settings under Tools / Options... Kits/ CMake 45
  • 46. © Integrated Computer Solutions, Inc. All Rights Reserved Qt Creator Support 46
  • 47. © Integrated Computer Solutions, Inc. All Rights Reserved Qt Creator Support 47
  • 48. © Integrated Computer Solutions, Inc. All Rights Reserved Qt Creator Support 48
  • 49. © Integrated Computer Solutions, Inc. All Rights Reserved CTest • CMake has support for defining and running unit tests • Uses the ctest program • See the documentation for details 49
  • 50. © Integrated Computer Solutions, Inc. All Rights Reserved CPack • Support for packaging applications via the "cpack" program and associated configuration files • See the documentation for details 50
  • 51. © Integrated Computer Solutions, Inc. All Rights Reserved Misc. Tips • For more verbose output when building: make VERBOSE=1 • Example of a sub-directories only project: cmake_minimum_required(VERSION 3.0) add_subdirectory(demo1) add_subdirectory(demo2) add_subdirectory(demo3) 51
  • 52. © Integrated Computer Solutions, Inc. All Rights Reserved Summary • Use of CMake is increasing • May be Qt's build system in Qt 6 • Relatively easy to use with Qt projects and Qt Creator 52
  • 53. © Integrated Computer Solutions, Inc. All Rights Reserved References • https://en.wikipedia.org/wiki/CMake • https://cmake.org/ • https://cmake.org/cmake-tutorial/ • https://cmake.org/cmake/help/latest/index.html • https://cmake.org/download/ • https://cmake.org/cmake/help/latest/manual/cmake-qt.7.html#manual: cmake-qt(7) • http://doc.qt.io/qt-5/cmake-manual.html • http://doc.qt.io/qtcreator/creator-project-cmake.html • https://github.com/tranter/webinars/tree/master/CMake • https://wiki.qt.io/CMake_Port 53
  • 54. © Integrated Computer Solutions, Inc. All Rights Reserved Q&A • Questions? • Any tips to share? 54