• Stars
    star
    217
  • Rank 181,392 (Top 4 %)
  • Language
    C++
  • License
    MIT License
  • Created over 6 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Brief Beginnerโ€™s Guide to CMake or How to quickly get up and running with CMake

CMake Tutorial

This tutorial cover the following:

  1. Build the project using simple c++(1) and make(1).
  2. Build the project using cmake(1).
  3. Build the project using cmake(1) with third party library.

In this tutorial we will use the following project structure:

cmake-tutorial/
โ”œโ”€โ”€ CMakeLists.txt
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ src
โ”‚ย ย  โ”œโ”€โ”€ main.cc
โ”‚ย ย  โ”œโ”€โ”€ math.cc
โ”‚ย ย  โ””โ”€โ”€ math.h
โ””โ”€โ”€ test
    โ””โ”€โ”€ math_test.cc

Directory structure:

  • src : Directory for source code.
  • test : Directory for test.

src/main.cc is our main executable and src/math.{cc,h} is an internal library that used by src/main.cc.

We will start from the basic on how to build the project using c++(1) only and a simple Makefile. Then we define the build in CMakeLists.txt and using cmake(1) to generate complex Makefile for us.

Install CMake

First of all, you need to install cmake.

On Ubuntu:

sudo apt-get install cmake

On macOS:

brew install cmake

Make sure the cmake is installed correctly:

% cmake --version
cmake version 3.10.2

CMake suite maintained and supported by Kitware (kitware.com/cmake).

Compiling & Linking

We can build this project using the following command:

c++ src/main.cc src/math.cc -o cmake-tutorial

Or we can do the compile and linking on the separate steps

c++ -c src/math.cc -o math.o 
c++ src/main.cc math.o -o cmake-tutorial

Using Makefile

We can automate the step to compile and link above using Makefile. First we need to create new Makefile in the root directory with the following content:

# Add definition to generate math.o object file
math.o: src/math.cc src/math.h
    c++ -c src/math.cc -o math.o

# Add definition to generate cmake-tutorial binary
cmake-tutorial: math.o
    c++ src/main.cc math.o -o cmake-tutorial

Now we can run:

make cmake-tutorial

to build cmake-tutorial binary. If there are no changes in src/{main,math}.cc and src/math.h, the subsequent command will do nothing:

% make cmake-tutorial
make: Nothing to be done for `cmake-tutorial'.

this is useful when working on larger project, we only compile the object that changes.

Using CMake

Now we know how to perform compiling and linking using the C++ and make command. Now we can use cmake to do all of this for us.

Create new CMakeLists.txt with the following content:

cmake_minimum_required (VERSION 3.10)

# Define the project
project(cmake-tutorial)

# Add definition for math library
add_library(math src/math.cc)

# Add definition for the cmake-tutorial binary
add_executable(cmake-tutorial src/main.cc)
target_link_libraries(cmake-tutorial math)

We can generate the Makefile based on the definition above using the following command:

cmake .

Or create a build directory to store the generated files by CMake:

mkdir build
cd build/
cmake ..

Now we can run make cmake-tutorial to build the binary.

% make cmake-tutorial
Scanning dependencies of target math
[ 25%] Building CXX object CMakeFiles/math.dir/src/math.cc.o
[ 50%] Linking CXX static library libmath.a
[ 50%] Built target math
Scanning dependencies of target cmake-tutorial
[ 75%] Building CXX object CMakeFiles/cmake-tutorial.dir/src/main.cc.o
[100%] Linking CXX executable cmake-tutorial
[100%] Built target cmake-tutorial

Or we can use the CMake directly via:

cmake --build . --target cmake-tutorial

Using CMake with 3rd-party library

Suppose that we want to write a unit test for math::add(a, b). We will use a googletest library to create and run the unit test.

Add the following definition to CMakeLists.txt:

# Third-party library
include(ExternalProject)
ExternalProject_Add(googletest
    PREFIX "${CMAKE_BINARY_DIR}/lib"
    GIT_REPOSITORY "https://github.com/google/googletest.git"
    GIT_TAG "main"
    CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/lib/installed
)
# Prevent build on all targets build
set_target_properties(googletest PROPERTIES EXCLUDE_FROM_ALL TRUE)

# Define ${CMAKE_INSTALL_...} variables
include(GNUInstallDirs)

# Specify where third-party libraries are located
link_directories(${CMAKE_BINARY_DIR}/lib/installed/${CMAKE_INSTALL_LIBDIR})
include_directories(${CMAKE_BINARY_DIR}/lib/installed/${CMAKE_INSTALL_INCLUDEDIR})

# This is required for googletest
find_package(Threads REQUIRED)

# Test
add_executable(math_test test/math_test.cc)
target_link_libraries(math_test math gtest Threads::Threads)
# Make sure third-party is built before executable
add_dependencies(math_test googletest)
set_target_properties(math_test PROPERTIES EXCLUDE_FROM_ALL TRUE)

Re-generate the build files using the following command:

cd build/
cmake ..

Build the unit test:

cmake --build . --target math_test

Run the test:

% ./math_test 
[==========] Running 6 tests from 3 test cases.
[----------] Global test environment set-up.
[----------] 2 tests from MathAddTest
[ RUN      ] MathAddTest.PositiveNum
[       OK ] MathAddTest.PositiveNum (0 ms)
[ RUN      ] MathAddTest.ZeroB
[       OK ] MathAddTest.ZeroB (0 ms)
[----------] 2 tests from MathAddTest (0 ms total)

[----------] 2 tests from MathSubTest
[ RUN      ] MathSubTest.PositiveNum
[       OK ] MathSubTest.PositiveNum (0 ms)
[ RUN      ] MathSubTest.ZeroB
[       OK ] MathSubTest.ZeroB (0 ms)
[----------] 2 tests from MathSubTest (0 ms total)

[----------] 2 tests from MathMulTest
[ RUN      ] MathMulTest.PositiveNum
[       OK ] MathMulTest.PositiveNum (0 ms)
[ RUN      ] MathMulTest.ZeroB
[       OK ] MathMulTest.ZeroB (0 ms)
[----------] 2 tests from MathMulTest (0 ms total)

[----------] Global test environment tear-down
[==========] 6 tests from 3 test cases ran. (0 ms total)
[  PASSED  ] 6 tests.

Done.

IDE Support

If you are using CLion, the google test will automatically detected.

CLion

Visual studio also support cmake

More Repositories

1

hsr

Hand signals recognition using Convolutional Neural Network implemented in TensorFlow
Python
109
star
2

rojak

Project Rojak, Yuk gabung diskusi di Slack nya Rojak: http://team.rojak.id/
Python
98
star
3

covid19-resources

Curated list of Coronavirus data & apps
35
star
4

byten

Bit the size of file and turn it into human readable format. Go Package.
Go
23
star
5

Crabsformer

Work in progress: An easy-to-use fundamental library for scientific computing with Rust, highly inspired by NumPy.
Rust
13
star
6

k8s-flask-tutorial

A Guide to Deploy Flask App on Google Kubernetes Engine
Python
11
star
7

pemilu

A beautiful Ruby interface of Pemilu APIs
Ruby
9
star
8

sayoeti-core

Sayoeti is the PR's assistant of Komisi Pemberantasan Korupsi (KPK) that powered by SVM. He helps KPK by watching all mass media in Indonesia and provide sentiment analysis of the mass media.
C
7
star
9

automata

REST API server for curhat app project
Go
7
star
10

talks

6
star
11

multicall3.py

Python 3 interface for Multicall3
Python
6
star
12

rcnn

Recurrent Convolutional Neural Networks for Text Classification
C++
5
star
13

pycon2018

Slides and source code for my talks at PyCon Indonesia 2018
Jupyter Notebook
5
star
14

panduan.datascience.id

Panduan data science
HTML
4
star
15

ziggurat

C library which generates random variates from the uniform, normal or exponential distributions
C
4
star
16

singleton

Simple and reusable singleton smart contract that supports ERC3156
Solidity
3
star
17

sanic-instrumentation-guide

A guide on how to collect metrics from your Sanic application
Python
3
star
18

cpm-cc

Critical Path Method implementation in C++
C++
3
star
19

worter

[WIP] An easy-way to build REST API in Cloudflare Workers
TypeScript
3
star
20

ads

Facebook & Instagram ads landing page
HTML
2
star
21

neural-networks-cc

C++ version of hacker's guide to Neural Networks
C++
2
star
22

simple-nn

Simple Neural Network written in C programming language
C
2
star
23

too-old

Simple apps to measure how old are you now with precisly. built using AngularJS aand Bootstrap
CSS
2
star
24

sublime-terra

A grammar for Terra programming language
2
star
25

toool

Ideal Toolkit for static site development.
JavaScript
2
star
26

rust-perceptron

A single-layer neural networks (perceptron) in Rust
Rust
2
star
27

liputan6-scraper

Go
1
star
28

salestock-backend-ta

Salestock Backend Technical Assesment
JavaScript
1
star
29

nvim-backup

pyk's code editor
Lua
1
star
30

rust-reader

A stream reader in Rust
Rust
1
star
31

belajar-data-engineering

Panduan untuk menjadi Data Engineer
1
star
32

prometheus-grafana-ansible

Ansible playbook that I use to run prometheus & grafana in DigitalOcean
1
star
33

lory

An easy-to-use Rust library for accessing the Twitter API
Rust
1
star
34

go-smtp-server

Go
1
star
35

kga

Solving Knapsack problem with Genetic Algorithm
C
1
star
36

building-srf

On my way building Speedy React Framework
TypeScript
1
star
37

postgresql-ansible

Ansible playbook that I use to provision new PostgreSQL database in Ubuntu 18.04
1
star
38

calegkita.org

Web apps for looking details information about all Candidates in Indonesian Elections 2014. - work in progress
CSS
1
star
39

vim

๐Ÿง™ pyk's ~/.vim
Vim Script
1
star
40

nextjs-12

Experimenting with next.js 12
TypeScript
1
star
41

like-likers

My personal tool for instagram marketing
JavaScript
1
star
42

peg-generator

Rust
1
star
43

inverse-exploit

Reproduce Inverse Finance exploit using Foundry
Solidity
1
star
44

labs

My home labs running Ethereum Archive nodes, Prometheus and other useful services
1
star
45

LlamaLocker

Lock your LLAMAs to claim share of yields
Solidity
1
star
46

zkevm-dai

Polygon zkEVM's DAI Bridge
Solidity
1
star
47

milapy

Simple Python 3 interface designed for Ethereum and various EVM-based chains
Python
1
star
48

impala-go

Impala SQL Driver for Go Programming Language
Go
1
star
49

snarkOS.js

WIP: snarkOS Javascript API
TypeScript
1
star
50

node-playground

JavaScript
1
star
51

yulang

yu - A programming language that focus on the developer experience (DX).
C
1
star
52

binary-tree-c

Binary tree in C
C
1
star
53

deep-parser

fast and efficient constituency parser
C
1
star
54

ai-mentoring-surabaya

A.I. Indonesia Academy, Batch #1
HTML
1
star
55

syndication

A simple RSS & Atom feed reader written in Go language
Go
1
star
56

zkevm-wsteth

Polygon zkEVM wstETH
Solidity
1
star
57

indodax-techical-analysis

Trying to find a patternfor best time & best price to trade Crypto
Jupyter Notebook
1
star