Your First Build¶
This tutorial walks you through building a sample C project with rninja, demonstrating caching and common workflows.
Prerequisites¶
- rninja installed (Installation Guide)
- A C compiler (gcc or clang)
- Basic familiarity with build systems
Create a Sample Project¶
Let's create a simple C project to demonstrate rninja's features.
Step 1: Create Project Structure¶
Step 2: Create Source Files¶
Create main.c:
Create greet.h:
Create greet.c:
#include <stdio.h>
#include "greet.h"
void greet(const char *name) {
printf("Hello, %s!\n", name);
}
Step 3: Create the Build File¶
Create build.ninja:
# Ninja build file for demo project
# Variables
cc = gcc
cflags = -Wall -O2
# Rules
rule cc
command = $cc $cflags -c $in -o $out
description = CC $out
rule link
command = $cc $in -o $out
description = LINK $out
# Build edges
build main.o: cc main.c | greet.h
build greet.o: cc greet.c | greet.h
build hello: link main.o greet.o
# Default target
default hello
Build with rninja¶
First Build (Cold)¶
Run your first build:
Expected output:
Run the program:
Output:
Second Build (No-op)¶
Run rninja again without changes:
Expected output:
Instant Detection
rninja detected nothing changed in under 10 milliseconds.
Cached Rebuild¶
Now clean and rebuild to see caching in action:
Notice how the rebuild completes almost instantly? rninja restored the cached artifacts instead of recompiling.
Explore Build Information¶
View Dependencies¶
Output:
Query a Target¶
Output:
Show All Targets¶
Generate Compilation Database¶
For IDE integration (clangd, ccls, etc.):
Modify and Rebuild¶
Change a Source File¶
Edit greet.c to change the message:
#include <stdio.h>
#include "greet.h"
void greet(const char *name) {
printf("Greetings, %s!\n", name); // Changed message
}
Incremental Rebuild¶
Only the changed file and dependent targets rebuild:
Incremental Builds
rninja tracks dependencies and only rebuilds what's necessary.
View Cache Statistics¶
Example output:
Cache Statistics:
Enabled: true
Mode: local
Directory: /home/user/.cache/rninja
Local Cache:
Total entries: 6
Total size: 24.3 KB
Hit rate: 66.7%
Debug Build Issues¶
Explain Rebuilds¶
If you're unsure why something is rebuilding:
Output shows why each target needs rebuilding:
Verbose Output¶
See all commands being executed:
Dry Run¶
See what would be built without building:
Build with Different Options¶
Parallel Jobs¶
Control parallelism:
Continue on Errors¶
Keep building other targets if one fails:
Clean Up¶
Clean Build Outputs¶
Clean Stale Outputs¶
Remove outputs that are no longer in the build:
Clear Cache (if needed)¶
Using CMake Instead¶
If you prefer CMake, here's the equivalent workflow:
Create CMakeLists.txt¶
cmake_minimum_required(VERSION 3.10)
project(hello C)
add_executable(hello main.c greet.c)
Generate and Build¶
Next Steps¶
-
Learn how to switch your team from Ninja to rninja
-
Customize rninja's behavior
-
Share cache across machines
-
Explore available tools