SlideShare ist ein Scribd-Unternehmen logo
1 von 31
Downloaden Sie, um offline zu lesen
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?

Weitere ähnliche Inhalte

Was ist angesagt?

Profiling your Applications using the Linux Perf Tools
Profiling your Applications using the Linux Perf ToolsProfiling your Applications using the Linux Perf Tools
Profiling your Applications using the Linux Perf Tools
emBO_Conference
 

Was ist angesagt? (20)

Présentation de git
Présentation de gitPrésentation de git
Présentation de git
 
Git Started With Git
Git Started With GitGit Started With Git
Git Started With Git
 
Advanced Git Tutorial
Advanced Git TutorialAdvanced Git Tutorial
Advanced Git Tutorial
 
Conan a C/C++ Package Manager
Conan a C/C++ Package ManagerConan a C/C++ Package Manager
Conan a C/C++ Package Manager
 
Git utilisation quotidienne
Git   utilisation quotidienneGit   utilisation quotidienne
Git utilisation quotidienne
 
Profiling your Applications using the Linux Perf Tools
Profiling your Applications using the Linux Perf ToolsProfiling your Applications using the Linux Perf Tools
Profiling your Applications using the Linux Perf Tools
 
Git Introduction Tutorial
Git Introduction TutorialGit Introduction Tutorial
Git Introduction Tutorial
 
Git - An Introduction
Git - An IntroductionGit - An Introduction
Git - An Introduction
 
Git, CMake, Conan - How to ship and reuse our C++ projects?
Git, CMake, Conan - How to ship and reuse our C++ projects?Git, CMake, Conan - How to ship and reuse our C++ projects?
Git, CMake, Conan - How to ship and reuse our C++ projects?
 
Automated Testing with CMake, CTest and CDash
Automated Testing with CMake, CTest and CDashAutomated Testing with CMake, CTest and CDash
Automated Testing with CMake, CTest and CDash
 
Basic Cmake for Qt Users
Basic Cmake for Qt UsersBasic Cmake for Qt Users
Basic Cmake for Qt Users
 
Introduction to Makefile
Introduction to MakefileIntroduction to Makefile
Introduction to Makefile
 
C++20 the small things - Timur Doumler
C++20 the small things - Timur DoumlerC++20 the small things - Timur Doumler
C++20 the small things - Timur Doumler
 
Slab Allocator in Linux Kernel
Slab Allocator in Linux KernelSlab Allocator in Linux Kernel
Slab Allocator in Linux Kernel
 
MariaDB Performance Tuning Crash Course
MariaDB Performance Tuning Crash CourseMariaDB Performance Tuning Crash Course
MariaDB Performance Tuning Crash Course
 
Linux kernel tracing
Linux kernel tracingLinux kernel tracing
Linux kernel tracing
 
Git l'essentiel
Git l'essentielGit l'essentiel
Git l'essentiel
 
Introduction to git
Introduction to gitIntroduction to git
Introduction to git
 
[KubeConEU] Building images efficiently and securely on Kubernetes with BuildKit
[KubeConEU] Building images efficiently and securely on Kubernetes with BuildKit[KubeConEU] Building images efficiently and securely on Kubernetes with BuildKit
[KubeConEU] Building images efficiently and securely on Kubernetes with BuildKit
 
Physical Memory Management.pdf
Physical Memory Management.pdfPhysical Memory Management.pdf
Physical Memory Management.pdf
 

Ähnlich wie Introduction to CMake

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
 
Life of a Chromium Developer
Life of a Chromium DeveloperLife of a Chromium Developer
Life of a Chromium Developer
mpaproductions
 

Ähnlich wie 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
 
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
 
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
 
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
 

Mehr von 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
 

Mehr von Dimitrios Platis (10)

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++
 

Kürzlich hochgeladen

%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 

Kürzlich hochgeladen (20)

%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 

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?