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:
#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:
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
The 2 Comments Found
Hi,
I just bought a new Pi 5 and want to install cmake but i get this:
sudo apt install -y cmake
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
You might want to run 'apt --fix-broken install' to correct these.
The following packages have unmet dependencies:
cmake : Depends: libjsoncpp25 (>= 1.9.5) but it is not going to be installed
Depends: librhash0 (>= 1.2.6) but it is not going to be installed
Depends: libuv1 (>= 1.38.0) but it is not going to be installed
Depends: cmake-data (= 3.25.1-1) but it is not going to be installed
sdrpp : Depends: libfftw3-dev but it is not going to be installed
Depends: libglfw3-dev but it is not going to be installed
Depends: libvolk2-dev but it is not going to be installed
Depends: librtaudio-dev but it is not going to be installed
E: Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution).
How to fix this?
Thanks'
Andre
Hi,
I successfully installed CMake on my Raspberry Pi 5 without any issues. I'm using the latest version of Raspberry Pi OS, which is currently Bookworm.
The error you mentioned typically occurs when you install a package from a third-party Debian repository that offers higher version packages than those available in the official Debian repository. I recommend identifying and uninstalling the problematic packages. If you can't pinpoint the issue, reinstalling a fresh version of Raspberry Pi OS might be the best solution.
Leave a Comment
Cancel reply