SlideShare a Scribd company logo
Introduction to CMake
Dimitris Platis
dimitris@platis.solutions
[grcpp]
👍facebook.com/grcpp
👍meetup.com/grcpp-athens
● Knowledge spreading
● Discussions
● Development culture
● Networking
About me Dimitrios Platis
● Grew up in Rodos, Greece
● Software Engineer @ Zenseact,
Gothenburg
● Course responsible @ DIT112 & DAT265
Gothenburg University / Chalmers
● Interests:
○ Embedded systems
○ Software Architecture
○ API Design
○ Open source software & hardware
○ Robots, Portable gadgets, IoT
○ 3D printing
○ Autonomous Driving
● Website: https://platis.solutions
How about you?
What is CMake?
CMake is a software that manages the
way C/C++ projects are built. It has the
ability to simplify the build process of
projects with large or complex code
layouts and is compiler-agnostic.
It is considered the de facto standard for
building C/C++ projects.
When is a build
system
necessary?
● Avoid hard-coding paths
● Build a package on more than one
computer
● Support multiple operating systems and
compilers
● Describe how your program is
structured logically, not flags and
commands
Source: An Introduction to Modern CMake
Why do you
need to learn
CMake?
● 55 to 80% of C++ developers use it
● The vast majority professional C++
projects use it
● Cross-platform
● Well-supported by IDEs
● Relatively easy to get started with
● Makefiles and building with the command
line simply do not scale
Create binaries
Create binary with command line
// src/main.cpp
#include <iostream>
int main()
{
std::cout << "Hello World" << std::endl;
return 0;
}
$ g++ src/main.cpp
$ ./a.out
Create binary with CMake
cmake_minimum_required(VERSION 3.12)
project(IntroToCmake)
add_executable(hello_world src/main.cpp)
$ mkdir build
$ cd build
$ cmake ..
$ make
$ ./hello_world
Generated build/Makefile
# Default target executed when no arguments are given to make.
default_target: all
# The main all target
all: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start /home/me/projects/intro-to-cmake/build/CMakeFiles
/home/me/projects/intro-to-cmake/build//CMakeFiles/progress.marks
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 all
$(CMAKE_COMMAND) -E cmake_progress_start /home/me/projects/intro-to-cmake/build/CMakeFiles 0
# Build rule for target.
hello_world: cmake_check_build_system
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 hello_world
Let's spice things up
// src/greeter.h
#ifndef GREETER_H
#define GREETER_H
void greetClass();
#endif
// src/greeter.cpp
#include "greeter.h"
#include <iostream>
void greetClass()
{
std::cout << "Hello GRCPP" << std::endl;
}
$ g++ src/main.cpp src/greeter.cpp
$ ./a.out
CMake allows for structured & version controlled scaling
add_executable(hello_world src/main.cpp
src/greeter.cpp)
$ make
$ ./hello_world
Set include path
#include <foo/Foo.h>
mv src/greeter.h include/greeter/greeter.h
We want to start including "greeter.h" as
"greeter/greeter.h"
#include "greeter.h" ➡ #include "greeter/greeter.h"
$ g++ src/main.cpp src/greeter.cpp -I include/
mv src/greeter.h include/greeter/greeter.h
target_include_directories(hello_world PUBLIC include) $ make
Create libraries
libgreeter.a (static library)
add_library(greeter src/greeter.cpp)
target_include_directories(greeter PUBLIC include)
add_executable(hello_world src/main.cpp)
target_link_libraries(hello_world PUBLIC greeter)
$ make greeter
$ ls -l libgreeter.a
> -rw-r--r-- 1 me me 3088 libgreeter.a
$ make hello_world
$ ldd
libgreeter.so (dynamic library)
add_library(greeter SHARED src/greeter.cpp)
target_include_directories(greeter PUBLIC include)
add_executable(hello_world src/main.cpp)
target_link_libraries(hello_world PUBLIC greeter)
$ make greeter
$ ls -l libgreeter.so
-rw-r--r-- 1 me me 16856 libgreeter.so
$ make hello_world
$ ldd hello_world
libgreeter.so =>
/home/me/intro-to-cmake/build/libgreeter.so
Building libraries with CLI
Static library
$ g++ -c src/greeter.cpp -I include/
$ ar rvs greeter.a greeter.o
$ g++ src/main.cpp greeter.a -I include/
Dynamic library
$ g++ -c src/greeter.cpp -I include/ -fPIC
$ g++ -L . src/main.cpp -l greeter -I include/
Set compilation flags
🚩🚩🚩
Add universal VS target-specific flags
add_compile_options(
-Wall
-Wextra
-Wpedantic
-Werror
)
$ g++ src/main.cpp -Wall -Wextra -Wpedantic -Werror
Disable flag(s) for a specific target
target_compile_options (greeter PRIVATE -Wno-pedantic)
Configure the build with options
Conditional builds
option(BUILD_ALTERNATIVE_GREETER
"Build the alternative libgreeter" OFF)
if(BUILD_ALTERNATIVE_GREETER)
add_library(greeter SHARED
src/alternative_greeter.cpp)
else()
add_library(greeter SHARED src/greeter.cpp)
endif(BUILD_ALTERNATIVE_GREETER)
target_include_directories(greeter PUBLIC
include)
add_executable(hello_world src/main.cpp)
target_link_libraries(hello_world PUBLIC
greeter)
$ cmake .. -DBUILD_ALTERNATIVE_GREETER=ON
CMake functions
⚙
Customize your build steps
function(configure_test testExecutable)
# Link against gtest library
target_link_libraries(${testExecutable}
gtest gtest_main gmock_main)
# Disable variadic macro warnings
target_compile_options(${testExecutable}
PRIVATE -Wno-gnu-zero-variadic-macro-arguments)
# Create test name as the capitalized form
string(TOUPPER ${testExecutable} testName)
# Add executable to test suite
add_test(${testName} ${testExecutable}
${GTEST_RUN_FLAGS})
endfunction(configure_test)
add_executable(dummy_test DummyTest.cpp)
configure_test(dummy_test)
Takeaways
● We covered only the surface
● Easy things are simple with CMake
● Integration with custom build systems or other
libraries is where the trickery begins
● Start using CMake (or equivalent, e.g. Bazel)
even for personal projects
Let's keep in touch!
https://www.linkedin.com/in/platisd/
dimitris@platis.solutions
@PlatisSolutions
JetBrains lottery
1 year license for any Jetbrains IDE!
1. Go to: http://plat.is/jetbrains
2. If you won, please stick around until I
contact you
3. If you did not win, better luck next time!
Did you know that as a university student you
can get a free JetBrains license anyway?

More Related Content

What's hot

Introduction To Makefile
Introduction To MakefileIntroduction To Makefile
Introduction To Makefile
Waqqas Jabbar
 
Meet cute-between-ebpf-and-tracing
Meet cute-between-ebpf-and-tracingMeet cute-between-ebpf-and-tracing
Meet cute-between-ebpf-and-tracing
Viller Hsiao
 
Valgrind tutorial
Valgrind tutorialValgrind tutorial
Valgrind tutorial
Satabdi Das
 
Git cheatsheet
Git cheatsheetGit cheatsheet
Git cheatsheet
Weghlis Azzariou
 
Cmake kitware
Cmake kitwareCmake kitware
Cmake kitware
achintyalte
 
OpenWRT guide and memo
OpenWRT guide and memoOpenWRT guide and memo
OpenWRT guide and memo
家榮 吳
 
eBPF Trace from Kernel to Userspace
eBPF Trace from Kernel to UserspaceeBPF Trace from Kernel to Userspace
eBPF Trace from Kernel to Userspace
SUSE Labs Taipei
 
Q2.12: Debugging with GDB
Q2.12: Debugging with GDBQ2.12: Debugging with GDB
Q2.12: Debugging with GDB
Linaro
 
Version Control & Git
Version Control & GitVersion Control & Git
Version Control & Git
Jason Byrne
 
DMA Survival Guide
DMA Survival GuideDMA Survival Guide
DMA Survival Guide
Kernel TLV
 
Vim Rocks!
Vim Rocks!Vim Rocks!
Vim Rocks!
Kent Chen
 
Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021
Jian-Hong Pan
 
Android audio system(audioflinger)
Android audio system(audioflinger)Android audio system(audioflinger)
Android audio system(audioflinger)fefe7270
 
Git in 10 minutes
Git in 10 minutesGit in 10 minutes
Git in 10 minutes
Safique Ahmed Faruque
 
Interrupt Affinityについて
Interrupt AffinityについてInterrupt Affinityについて
Interrupt AffinityについてTakuya ASADA
 
Ninja Build: Simple Guide for Beginners
Ninja Build: Simple Guide for BeginnersNinja Build: Simple Guide for Beginners
Ninja Build: Simple Guide for Beginners
Chang W. Doh
 
LLVM Instruction Selection
LLVM Instruction SelectionLLVM Instruction Selection
LLVM Instruction Selection
Shiva Chen
 
Introduction to Makefile
Introduction to MakefileIntroduction to Makefile
Introduction to Makefile
Tusharadri Sarkar
 
Linux Kernel - Virtual File System
Linux Kernel - Virtual File SystemLinux Kernel - Virtual File System
Linux Kernel - Virtual File System
Adrian Huang
 
Git l'essentiel
Git l'essentielGit l'essentiel
Git l'essentiel
Riadh MNASRI
 

What's hot (20)

Introduction To Makefile
Introduction To MakefileIntroduction To Makefile
Introduction To Makefile
 
Meet cute-between-ebpf-and-tracing
Meet cute-between-ebpf-and-tracingMeet cute-between-ebpf-and-tracing
Meet cute-between-ebpf-and-tracing
 
Valgrind tutorial
Valgrind tutorialValgrind tutorial
Valgrind tutorial
 
Git cheatsheet
Git cheatsheetGit cheatsheet
Git cheatsheet
 
Cmake kitware
Cmake kitwareCmake kitware
Cmake kitware
 
OpenWRT guide and memo
OpenWRT guide and memoOpenWRT guide and memo
OpenWRT guide and memo
 
eBPF Trace from Kernel to Userspace
eBPF Trace from Kernel to UserspaceeBPF Trace from Kernel to Userspace
eBPF Trace from Kernel to Userspace
 
Q2.12: Debugging with GDB
Q2.12: Debugging with GDBQ2.12: Debugging with GDB
Q2.12: Debugging with GDB
 
Version Control & Git
Version Control & GitVersion Control & Git
Version Control & Git
 
DMA Survival Guide
DMA Survival GuideDMA Survival Guide
DMA Survival Guide
 
Vim Rocks!
Vim Rocks!Vim Rocks!
Vim Rocks!
 
Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021
 
Android audio system(audioflinger)
Android audio system(audioflinger)Android audio system(audioflinger)
Android audio system(audioflinger)
 
Git in 10 minutes
Git in 10 minutesGit in 10 minutes
Git in 10 minutes
 
Interrupt Affinityについて
Interrupt AffinityについてInterrupt Affinityについて
Interrupt Affinityについて
 
Ninja Build: Simple Guide for Beginners
Ninja Build: Simple Guide for BeginnersNinja Build: Simple Guide for Beginners
Ninja Build: Simple Guide for Beginners
 
LLVM Instruction Selection
LLVM Instruction SelectionLLVM Instruction Selection
LLVM Instruction Selection
 
Introduction to Makefile
Introduction to MakefileIntroduction to Makefile
Introduction to Makefile
 
Linux Kernel - Virtual File System
Linux Kernel - Virtual File SystemLinux Kernel - Virtual File System
Linux Kernel - Virtual File System
 
Git l'essentiel
Git l'essentielGit l'essentiel
Git l'essentiel
 

Similar to Introduction to CMake

[EXTENDED] Ceph, Docker, Heroku Slugs, CoreOS and Deis Overview
[EXTENDED] Ceph, Docker, Heroku Slugs, CoreOS and Deis Overview[EXTENDED] Ceph, Docker, Heroku Slugs, CoreOS and Deis Overview
[EXTENDED] Ceph, Docker, Heroku Slugs, CoreOS and Deis Overview
Leo Lorieri
 
Acquia BLT for the Win, or How to speed up the project setup, development an...
Acquia BLT for the Win, or  How to speed up the project setup, development an...Acquia BLT for the Win, or  How to speed up the project setup, development an...
Acquia BLT for the Win, or How to speed up the project setup, development an...
DrupalCamp Kyiv
 
Lightweight Developer Provisioning with Gradle
Lightweight Developer Provisioning with GradleLightweight Developer Provisioning with Gradle
Lightweight Developer Provisioning with Gradle
QAware GmbH
 
Lightweight Developer Provisioning with Gradle and SEU-as-code
Lightweight Developer Provisioning with Gradle and SEU-as-codeLightweight Developer Provisioning with Gradle and SEU-as-code
Lightweight Developer Provisioning with Gradle and SEU-as-code
Mario-Leander Reimer
 
Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014
biicode
 
XPDDS18: A dive into kbuild - Cao jin, Fujitsu
XPDDS18: A dive into kbuild - Cao jin, FujitsuXPDDS18: A dive into kbuild - Cao jin, Fujitsu
XPDDS18: A dive into kbuild - Cao jin, Fujitsu
The Linux Foundation
 
Golang 101 for IT-Pros - Cisco Live Orlando 2018 - DEVNET-1808
Golang 101 for IT-Pros - Cisco Live Orlando 2018 - DEVNET-1808Golang 101 for IT-Pros - Cisco Live Orlando 2018 - DEVNET-1808
Golang 101 for IT-Pros - Cisco Live Orlando 2018 - DEVNET-1808
Cisco DevNet
 
Golang Project Layout and Practice
Golang Project Layout and PracticeGolang Project Layout and Practice
Golang Project Layout and Practice
Bo-Yi Wu
 
Basicsof c make and git for a hello qt application
Basicsof c make and git for a hello qt applicationBasicsof c make and git for a hello qt application
Basicsof c make and git for a hello qt application
Dinesh Manajipet
 
What makes me "Grunt"?
What makes me "Grunt"? What makes me "Grunt"?
What makes me "Grunt"?
Fabien Doiron
 
Recipe to build open splice dds 6.3.xxx Hello World example over Qt 5.2
 Recipe to build open splice dds 6.3.xxx Hello World example over Qt 5.2   Recipe to build open splice dds 6.3.xxx Hello World example over Qt 5.2
Recipe to build open splice dds 6.3.xxx Hello World example over Qt 5.2 Adil Khan
 
Webinar: Building Embedded Applications from QtCreator with Docker
Webinar: Building Embedded Applications from QtCreator with DockerWebinar: Building Embedded Applications from QtCreator with Docker
Webinar: Building Embedded Applications from QtCreator with Docker
Burkhard Stubert
 
Build and run embedded apps faster from qt creator with docker
Build and run embedded apps faster from qt creator with dockerBuild and run embedded apps faster from qt creator with docker
Build and run embedded apps faster from qt creator with docker
Qt
 
Perl on-embedded-devices
Perl on-embedded-devicesPerl on-embedded-devices
Perl on-embedded-devices
Jens Rehsack
 
DevFest 2022 - Cloud Workstation Introduction TaiChung
DevFest 2022 - Cloud Workstation Introduction TaiChungDevFest 2022 - Cloud Workstation Introduction TaiChung
DevFest 2022 - Cloud Workstation Introduction TaiChung
KAI CHU CHUNG
 
Continuous Delivery com Docker, OpenShift e Jenkins
Continuous Delivery com Docker, OpenShift e JenkinsContinuous Delivery com Docker, OpenShift e Jenkins
Continuous Delivery com Docker, OpenShift e Jenkins
Bruno Padilha
 
Microservices DevOps on Google Cloud Platform
Microservices DevOps on Google Cloud PlatformMicroservices DevOps on Google Cloud Platform
Microservices DevOps on Google Cloud Platform
Sunnyvale
 
The Fairy Tale of the One Command Build Script
The Fairy Tale of the One Command Build ScriptThe Fairy Tale of the One Command Build Script
The Fairy Tale of the One Command Build Script
Docker, Inc.
 
Life of a Chromium Developer
Life of a Chromium DeveloperLife of a Chromium Developer
Life of a Chromium Developermpaproductions
 
Gitlab ci e kubernetes, build test and deploy your projects like a pro
Gitlab ci e kubernetes, build test and deploy your projects like a proGitlab ci e kubernetes, build test and deploy your projects like a pro
Gitlab ci e kubernetes, build test and deploy your projects like a pro
sparkfabrik
 

Similar to Introduction to CMake (20)

[EXTENDED] Ceph, Docker, Heroku Slugs, CoreOS and Deis Overview
[EXTENDED] Ceph, Docker, Heroku Slugs, CoreOS and Deis Overview[EXTENDED] Ceph, Docker, Heroku Slugs, CoreOS and Deis Overview
[EXTENDED] Ceph, Docker, Heroku Slugs, CoreOS and Deis Overview
 
Acquia BLT for the Win, or How to speed up the project setup, development an...
Acquia BLT for the Win, or  How to speed up the project setup, development an...Acquia BLT for the Win, or  How to speed up the project setup, development an...
Acquia BLT for the Win, or How to speed up the project setup, development an...
 
Lightweight Developer Provisioning with Gradle
Lightweight Developer Provisioning with GradleLightweight Developer Provisioning with Gradle
Lightweight Developer Provisioning with Gradle
 
Lightweight Developer Provisioning with Gradle and SEU-as-code
Lightweight Developer Provisioning with Gradle and SEU-as-codeLightweight Developer Provisioning with Gradle and SEU-as-code
Lightweight Developer Provisioning with Gradle and SEU-as-code
 
Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014
 
XPDDS18: A dive into kbuild - Cao jin, Fujitsu
XPDDS18: A dive into kbuild - Cao jin, FujitsuXPDDS18: A dive into kbuild - Cao jin, Fujitsu
XPDDS18: A dive into kbuild - Cao jin, Fujitsu
 
Golang 101 for IT-Pros - Cisco Live Orlando 2018 - DEVNET-1808
Golang 101 for IT-Pros - Cisco Live Orlando 2018 - DEVNET-1808Golang 101 for IT-Pros - Cisco Live Orlando 2018 - DEVNET-1808
Golang 101 for IT-Pros - Cisco Live Orlando 2018 - DEVNET-1808
 
Golang Project Layout and Practice
Golang Project Layout and PracticeGolang Project Layout and Practice
Golang Project Layout and Practice
 
Basicsof c make and git for a hello qt application
Basicsof c make and git for a hello qt applicationBasicsof c make and git for a hello qt application
Basicsof c make and git for a hello qt application
 
What makes me "Grunt"?
What makes me "Grunt"? What makes me "Grunt"?
What makes me "Grunt"?
 
Recipe to build open splice dds 6.3.xxx Hello World example over Qt 5.2
 Recipe to build open splice dds 6.3.xxx Hello World example over Qt 5.2   Recipe to build open splice dds 6.3.xxx Hello World example over Qt 5.2
Recipe to build open splice dds 6.3.xxx Hello World example over Qt 5.2
 
Webinar: Building Embedded Applications from QtCreator with Docker
Webinar: Building Embedded Applications from QtCreator with DockerWebinar: Building Embedded Applications from QtCreator with Docker
Webinar: Building Embedded Applications from QtCreator with Docker
 
Build and run embedded apps faster from qt creator with docker
Build and run embedded apps faster from qt creator with dockerBuild and run embedded apps faster from qt creator with docker
Build and run embedded apps faster from qt creator with docker
 
Perl on-embedded-devices
Perl on-embedded-devicesPerl on-embedded-devices
Perl on-embedded-devices
 
DevFest 2022 - Cloud Workstation Introduction TaiChung
DevFest 2022 - Cloud Workstation Introduction TaiChungDevFest 2022 - Cloud Workstation Introduction TaiChung
DevFest 2022 - Cloud Workstation Introduction TaiChung
 
Continuous Delivery com Docker, OpenShift e Jenkins
Continuous Delivery com Docker, OpenShift e JenkinsContinuous Delivery com Docker, OpenShift e Jenkins
Continuous Delivery com Docker, OpenShift e Jenkins
 
Microservices DevOps on Google Cloud Platform
Microservices DevOps on Google Cloud PlatformMicroservices DevOps on Google Cloud Platform
Microservices DevOps on Google Cloud Platform
 
The Fairy Tale of the One Command Build Script
The Fairy Tale of the One Command Build ScriptThe Fairy Tale of the One Command Build Script
The Fairy Tale of the One Command Build Script
 
Life of a Chromium Developer
Life of a Chromium DeveloperLife of a Chromium Developer
Life of a Chromium Developer
 
Gitlab ci e kubernetes, build test and deploy your projects like a pro
Gitlab ci e kubernetes, build test and deploy your projects like a proGitlab ci e kubernetes, build test and deploy your projects like a pro
Gitlab ci e kubernetes, build test and deploy your projects like a pro
 

More from Dimitrios Platis

[GRCPP] Introduction to concepts (C++20)
[GRCPP] Introduction to concepts (C++20)[GRCPP] Introduction to concepts (C++20)
[GRCPP] Introduction to concepts (C++20)
Dimitrios Platis
 
OpenAI API crash course
OpenAI API crash courseOpenAI API crash course
OpenAI API crash course
Dimitrios Platis
 
Builder pattern in C++.pdf
Builder pattern in C++.pdfBuilder pattern in C++.pdf
Builder pattern in C++.pdf
Dimitrios Platis
 
Interprocess communication with C++.pdf
Interprocess communication with C++.pdfInterprocess communication with C++.pdf
Interprocess communication with C++.pdf
Dimitrios Platis
 
Lambda expressions in C++
Lambda expressions in C++Lambda expressions in C++
Lambda expressions in C++
Dimitrios Platis
 
Writing SOLID C++ [gbgcpp meetup @ Zenseact]
Writing SOLID C++ [gbgcpp meetup @ Zenseact]Writing SOLID C++ [gbgcpp meetup @ Zenseact]
Writing SOLID C++ [gbgcpp meetup @ Zenseact]
Dimitrios Platis
 
Pointer to implementation idiom
Pointer to implementation idiomPointer to implementation idiom
Pointer to implementation idiom
Dimitrios Platis
 
Afry software safety ISO26262 (Embedded @ Gothenburg Meetup)
Afry software safety ISO26262 (Embedded @ Gothenburg Meetup)Afry software safety ISO26262 (Embedded @ Gothenburg Meetup)
Afry software safety ISO26262 (Embedded @ Gothenburg Meetup)
Dimitrios Platis
 
How to create your own Linux distribution (embedded-gothenburg)
How to create your own Linux distribution (embedded-gothenburg)How to create your own Linux distribution (embedded-gothenburg)
How to create your own Linux distribution (embedded-gothenburg)
Dimitrios Platis
 
[grcpp] Refactoring for testability c++
[grcpp] Refactoring for testability c++[grcpp] Refactoring for testability c++
[grcpp] Refactoring for testability c++
Dimitrios Platis
 
Refactoring for testability c++
Refactoring for testability c++Refactoring for testability c++
Refactoring for testability c++
Dimitrios Platis
 

More from Dimitrios Platis (11)

[GRCPP] Introduction to concepts (C++20)
[GRCPP] Introduction to concepts (C++20)[GRCPP] Introduction to concepts (C++20)
[GRCPP] Introduction to concepts (C++20)
 
OpenAI API crash course
OpenAI API crash courseOpenAI API crash course
OpenAI API crash course
 
Builder pattern in C++.pdf
Builder pattern in C++.pdfBuilder pattern in C++.pdf
Builder pattern in C++.pdf
 
Interprocess communication with C++.pdf
Interprocess communication with C++.pdfInterprocess communication with C++.pdf
Interprocess communication with C++.pdf
 
Lambda expressions in C++
Lambda expressions in C++Lambda expressions in C++
Lambda expressions in C++
 
Writing SOLID C++ [gbgcpp meetup @ Zenseact]
Writing SOLID C++ [gbgcpp meetup @ Zenseact]Writing SOLID C++ [gbgcpp meetup @ Zenseact]
Writing SOLID C++ [gbgcpp meetup @ Zenseact]
 
Pointer to implementation idiom
Pointer to implementation idiomPointer to implementation idiom
Pointer to implementation idiom
 
Afry software safety ISO26262 (Embedded @ Gothenburg Meetup)
Afry software safety ISO26262 (Embedded @ Gothenburg Meetup)Afry software safety ISO26262 (Embedded @ Gothenburg Meetup)
Afry software safety ISO26262 (Embedded @ Gothenburg Meetup)
 
How to create your own Linux distribution (embedded-gothenburg)
How to create your own Linux distribution (embedded-gothenburg)How to create your own Linux distribution (embedded-gothenburg)
How to create your own Linux distribution (embedded-gothenburg)
 
[grcpp] Refactoring for testability c++
[grcpp] Refactoring for testability c++[grcpp] Refactoring for testability c++
[grcpp] Refactoring for testability c++
 
Refactoring for testability c++
Refactoring for testability c++Refactoring for testability c++
Refactoring for testability c++
 

Recently uploaded

Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
RISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent EnterpriseRISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent Enterprise
Srikant77
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 

Recently uploaded (20)

Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
RISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent EnterpriseRISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent Enterprise
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 

Introduction to CMake

  • 1. Introduction to CMake Dimitris Platis dimitris@platis.solutions
  • 3. About me Dimitrios Platis ● Grew up in Rodos, Greece ● Software Engineer @ Zenseact, Gothenburg ● Course responsible @ DIT112 & DAT265 Gothenburg University / Chalmers ● Interests: ○ Embedded systems ○ Software Architecture ○ API Design ○ Open source software & hardware ○ Robots, Portable gadgets, IoT ○ 3D printing ○ Autonomous Driving ● Website: https://platis.solutions
  • 5. What is CMake? CMake is a software that manages the way C/C++ projects are built. It has the ability to simplify the build process of projects with large or complex code layouts and is compiler-agnostic. It is considered the de facto standard for building C/C++ projects.
  • 6. When is a build system necessary? ● Avoid hard-coding paths ● Build a package on more than one computer ● Support multiple operating systems and compilers ● Describe how your program is structured logically, not flags and commands Source: An Introduction to Modern CMake
  • 7. Why do you need to learn CMake? ● 55 to 80% of C++ developers use it ● The vast majority professional C++ projects use it ● Cross-platform ● Well-supported by IDEs ● Relatively easy to get started with ● Makefiles and building with the command line simply do not scale
  • 9. Create binary with command line // src/main.cpp #include <iostream> int main() { std::cout << "Hello World" << std::endl; return 0; } $ g++ src/main.cpp $ ./a.out
  • 10. Create binary with CMake cmake_minimum_required(VERSION 3.12) project(IntroToCmake) add_executable(hello_world src/main.cpp) $ mkdir build $ cd build $ cmake .. $ make $ ./hello_world
  • 11. Generated build/Makefile # Default target executed when no arguments are given to make. default_target: all # The main all target all: cmake_check_build_system $(CMAKE_COMMAND) -E cmake_progress_start /home/me/projects/intro-to-cmake/build/CMakeFiles /home/me/projects/intro-to-cmake/build//CMakeFiles/progress.marks $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 all $(CMAKE_COMMAND) -E cmake_progress_start /home/me/projects/intro-to-cmake/build/CMakeFiles 0 # Build rule for target. hello_world: cmake_check_build_system $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 hello_world
  • 12. Let's spice things up // src/greeter.h #ifndef GREETER_H #define GREETER_H void greetClass(); #endif // src/greeter.cpp #include "greeter.h" #include <iostream> void greetClass() { std::cout << "Hello GRCPP" << std::endl; } $ g++ src/main.cpp src/greeter.cpp $ ./a.out
  • 13. CMake allows for structured & version controlled scaling add_executable(hello_world src/main.cpp src/greeter.cpp) $ make $ ./hello_world
  • 15. mv src/greeter.h include/greeter/greeter.h We want to start including "greeter.h" as "greeter/greeter.h" #include "greeter.h" ➡ #include "greeter/greeter.h" $ g++ src/main.cpp src/greeter.cpp -I include/
  • 18. libgreeter.a (static library) add_library(greeter src/greeter.cpp) target_include_directories(greeter PUBLIC include) add_executable(hello_world src/main.cpp) target_link_libraries(hello_world PUBLIC greeter) $ make greeter $ ls -l libgreeter.a > -rw-r--r-- 1 me me 3088 libgreeter.a $ make hello_world $ ldd
  • 19. libgreeter.so (dynamic library) add_library(greeter SHARED src/greeter.cpp) target_include_directories(greeter PUBLIC include) add_executable(hello_world src/main.cpp) target_link_libraries(hello_world PUBLIC greeter) $ make greeter $ ls -l libgreeter.so -rw-r--r-- 1 me me 16856 libgreeter.so $ make hello_world $ ldd hello_world libgreeter.so => /home/me/intro-to-cmake/build/libgreeter.so
  • 20. Building libraries with CLI Static library $ g++ -c src/greeter.cpp -I include/ $ ar rvs greeter.a greeter.o $ g++ src/main.cpp greeter.a -I include/ Dynamic library $ g++ -c src/greeter.cpp -I include/ -fPIC $ g++ -L . src/main.cpp -l greeter -I include/
  • 22. Add universal VS target-specific flags add_compile_options( -Wall -Wextra -Wpedantic -Werror ) $ g++ src/main.cpp -Wall -Wextra -Wpedantic -Werror
  • 23. Disable flag(s) for a specific target target_compile_options (greeter PRIVATE -Wno-pedantic)
  • 24. Configure the build with options
  • 25. Conditional builds option(BUILD_ALTERNATIVE_GREETER "Build the alternative libgreeter" OFF) if(BUILD_ALTERNATIVE_GREETER) add_library(greeter SHARED src/alternative_greeter.cpp) else() add_library(greeter SHARED src/greeter.cpp) endif(BUILD_ALTERNATIVE_GREETER) target_include_directories(greeter PUBLIC include) add_executable(hello_world src/main.cpp) target_link_libraries(hello_world PUBLIC greeter) $ cmake .. -DBUILD_ALTERNATIVE_GREETER=ON
  • 27. Customize your build steps function(configure_test testExecutable) # Link against gtest library target_link_libraries(${testExecutable} gtest gtest_main gmock_main) # Disable variadic macro warnings target_compile_options(${testExecutable} PRIVATE -Wno-gnu-zero-variadic-macro-arguments) # Create test name as the capitalized form string(TOUPPER ${testExecutable} testName) # Add executable to test suite add_test(${testName} ${testExecutable} ${GTEST_RUN_FLAGS}) endfunction(configure_test) add_executable(dummy_test DummyTest.cpp) configure_test(dummy_test)
  • 28. Takeaways ● We covered only the surface ● Easy things are simple with CMake ● Integration with custom build systems or other libraries is where the trickery begins ● Start using CMake (or equivalent, e.g. Bazel) even for personal projects
  • 29.
  • 30. Let's keep in touch! https://www.linkedin.com/in/platisd/ dimitris@platis.solutions @PlatisSolutions
  • 31. JetBrains lottery 1 year license for any Jetbrains IDE! 1. Go to: http://plat.is/jetbrains 2. If you won, please stick around until I contact you 3. If you did not win, better luck next time! Did you know that as a university student you can get a free JetBrains license anyway?