Install CMake on Raspberry Pi

Install CMake on Raspberry Pi

CMake is a tool that uses configuration file called CMakeLists.txt to generate standard build files such as makefiles on Unix systems, Visual Studio project files on Windows, and so on. CMake is not a compiler or build system but rather it generates build files that can be used to compile source code.

This tutorial shows how to install CMake on Raspberry Pi.

Install CMake

Connect to Raspberry Pi via SSH. Run the following commands to update the package lists and install CMake:

sudo apt update
sudo apt install -y cmake

We can check version of CMake:

cmake --version

Testing CMake

Create a new directory to store project files and navigate to this directory:

mkdir helloworld && cd helloworld

Create a main.c file:

nano main.c

Once the file is opened, add the following code:

helloworld/main.c

#include <stdio.h>
 
int main() {
    printf("Hello world\n");
 
    return 0;
}

Create CMake configuration file called CMakeLists.txt:

nano CMakeLists.txt

Add the following content:

helloworld/CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
project(hello C)

add_executable(hello main.c)

Recommended to create separate directory to store files that will be generated by CMake.

mkdir build && cd build

A project structure looks as follows:

helloworld/
    build/
    CMakeLists.txt
    main.c

In a build directory run the cmake command to generate build files using CMakeLists.txt file that located in parent directory. By default, CMake will generate build files for native build system. In our case it will be makefiles.

cmake ..

Once complete, we can use ls command to list files in a directory.

CMakeCache.txt  CMakeFiles  cmake_install.cmake  Makefile

As we can see, Makefile file has been generated. Now run the make command to build program:

make

Execute a program:

./hello

Uninstall CMake

If you wish to completely remove CMake and related dependencies, then execute the following command:

sudo apt purge --autoremove -y cmake gcc make

Leave a Comment

Cancel reply

Your email address will not be published.