Introduction to cmake

raphaelmeyer
2,514 views
undefined

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

CMake Project File

The entry point for a CMake project is a file called CMakeLists.txt.

Exercise 1

Let's set up the top level CMakeLists.txt in the project root folder.

[project]/
+--- CMakeLists.txt
+--- ...

The top level CMakeLists.txt must start with command cmake_minimum_required. For this exercise we will be using CMake version 3.10.

Next is the project command. Our time machine project should be named TimeMachine, should have version 1.0.2 and its language should be set to C++.

Run CMake
# [project]/CMakeLists.txt
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Building a CMake project

In order to build a CMake project we need a working directory for the build. In our example we use a folder build in the project folder.

For project initialization you must change into the build folder. But you only need to initialize a build folder once.

# create the build folder
mkdir -p /project/build

# for initialization we must change into the build folder
cd /project/build
cmake -G Ninja /project

# build the project
cmake --build /project/build

The option -G of the cmake command line tool sets the generator. There are generators for different build systems like make or ninja. Also, there are generators to create projects for IDEs like eclipse or visual studio.

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content