Install Ninja Build System on Raspberry Pi

Ninja is a build system that enables to build binaries from source code. Ninja might be used as alternative to the Make. Ninja is focused on speed. To achieve high performance, Ninja doesn’t have all features that Make build system provides, such as string manipulation.

This tutorial explains how to install Ninja on Raspberry Pi.

Install Ninja

Use SSH to connect to Raspberry pi. Run the following commands to update the package lists and install Ninja:

sudo apt update
sudo apt install -y ninja-build

Once installed, we can check Ninja version:

ninja --version

Testing Ninja

We will build executable from C source code. So make sure you have installed GNU C compiler:

sudo apt install -y gcc

Create a new directory for project files and go to this directory:

mkdir helloworld && cd helloworld

Create a main.c file:

nano main.c

Add the following code:

helloworld/main.c

#include <stdio.h>

int main() {
    printf("Hello world\n");

    return 0;
}

By default, configuration file of the Ninja is called build.ninja. So create this file:

nano build.ninja

Once the file is opened, add the following code:

helloworld/build.ninja

rule compile
  command = gcc -Wall -c $in -o $out

rule link
  command = gcc $in -o $out

build hello.o: compile main.c
build hello: link hello.o

Run the ninja command to build program:

ninja

Execute a program:

./hello

Uninstall Ninja

If Ninja is no longer needed, you can remove it with command:

sudo apt purge --autoremove -y ninja-build

Leave a Comment

Your email address will not be published. Required fields are marked *