1. Programming Basics for Raspberry Pi¶
This guide introduces the essentials of C++ programming on the Raspberry Pi, from setting up your development environment to compiling and debugging your first programs. By the end of this guide, you'll have a solid foundation for the GPIO programming tutorials that follow.
Why C++ on Raspberry Pi?¶
C++ is an excellent choice for Raspberry Pi projects that require:
- High performance: C++ runs 10-100× faster than Python for compute-intensive tasks
- Direct hardware access: Control GPIO pins, SPI, I2C, and UART at the register level
- Memory efficiency: Critical when working with models that have limited RAM (Pi Zero: 512MB)
- Real-time processing: Minimal latency for robotics, audio, and sensor applications
- Industry relevance: C++ skills transfer directly to embedded systems and professional development
The Raspberry Pi supports modern C++ standards (C++17, C++20) through the GNU C++ compiler (g++).
Setting Up Your Development Environment¶
Installing the Compiler Toolchain¶
Update your system and install essential development tools:
This installs:
g++— The GNU C++ compilergcc— The GNU C compiler (needed for some libraries)make— The build automation toolgdb— The GNU debuggercmake— A cross-platform build system- Standard C/C++ headers and libraries
Verifying Your Installation¶
Choosing a Code Editor¶
You have several options for writing C++ on Raspberry Pi:
| Editor | Best For | Installation |
|---|---|---|
| nano | Quick edits, beginners | Pre-installed |
| VS Code (Remote SSH) | Full IDE experience from your PC | Install on your PC, connect via SSH |
| Vim/Neovim | Power users, terminal workflows | sudo apt install vim |
| Geany | Lightweight GUI IDE on Pi Desktop | sudo apt install geany |
| Code::Blocks | Traditional C++ IDE | sudo apt install codeblocks |
Recommended: VS Code Remote SSH
The best development experience is to install Visual Studio Code on your desktop/laptop and connect to your Raspberry Pi via the Remote - SSH extension. You get full IntelliSense, debugging, and a modern editor while the code runs natively on the Pi.
Then in VS Code on your PC: Ctrl+Shift+P → "Remote-SSH: Connect to Host" → pi@raspberrypi.local
Creating Your First C++ Program¶
Create a new file called hello.cpp:
Compiling and Running¶
Basic Compilation¶
You should see the output Hello, Raspberry Pi! displayed in your terminal.
Understanding the Compilation Process¶
When you run g++ -o hello hello.cpp, the compiler performs four stages:
You can see each stage individually:
Important Compiler Flags¶
| Flag | Purpose | Example |
|---|---|---|
-o name |
Name the output file | g++ -o myapp main.cpp |
-std=c++17 |
Specify C++ standard version | g++ -std=c++17 main.cpp |
-Wall |
Enable all common warnings | g++ -Wall main.cpp |
-Wextra |
Enable extra warnings | g++ -Wextra main.cpp |
-Werror |
Treat warnings as errors | g++ -Werror main.cpp |
-g |
Include debug information (for gdb) | g++ -g main.cpp |
-O2 |
Optimize for speed | g++ -O2 main.cpp |
-Os |
Optimize for size | g++ -Os main.cpp |
-pthread |
Enable POSIX threads | g++ -pthread main.cpp |
-lwiringPi |
Link against WiringPi library | g++ main.cpp -lwiringPi |
Recommended flags for development:
Recommended flags for release:
Multi-File Projects¶
Real projects consist of multiple source files. Here's a typical structure:
Header File (sensor.h)¶
Implementation File (sensor.cpp)¶
Main File (main.cpp)¶
Compiling Multi-File Projects¶
Build Automation with Makefile¶
Typing compile commands manually gets tedious. A Makefile automates the process:
Makefile indentation
Makefiles require tabs, not spaces for indentation before commands. If you get *** missing separator. Stop., check that you're using actual tab characters.
Now you can build with a single command:
Using CMake (Recommended for Larger Projects)¶
For more complex projects, CMake is the standard:
Build with CMake:
Debugging with GDB¶
When your program crashes or produces wrong results, gdb helps you find the problem:
Essential GDB commands:
| Command | Shortcut | Action |
|---|---|---|
run |
r |
Start the program |
break main |
b main |
Set breakpoint at main() |
break sensor.cpp:15 |
b sensor.cpp:15 |
Set breakpoint at line 15 |
next |
n |
Execute next line (step over) |
step |
s |
Step into function |
continue |
c |
Continue to next breakpoint |
print variable |
p variable |
Print variable value |
backtrace |
bt |
Show call stack |
quit |
q |
Exit GDB |
Example debugging session:
GPIO Libraries for Raspberry Pi¶
When you're ready to control real hardware, you'll need a GPIO library. Here's a comparison:
| Library | Language | Status | Best For |
|---|---|---|---|
| lgpio | C/C++ | ✅ Active (recommended) | Pi 5 and all modern models |
| pigpio | C/C++ | ✅ Active | Precise timing, PWM, servo control |
| WiringPi | C/C++ | ⚠️ Discontinued (fork available) | Legacy projects |
| gpiod | C/C++ | ✅ Active | Kernel-level GPIO via libgpiod |
| RPi.GPIO | Python | ⚠️ No Pi 5 support | Simple Python scripts |
| gpiozero | Python | ✅ Active | Beginner-friendly Python |
Installing lgpio (Recommended)¶
Installing pigpio¶
Common Pitfalls for Beginners¶
Watch out for these common mistakes
- Forgetting to save before compiling — Your changes won't be included in the build
- Missing semicolons — Every statement in C++ must end with
; - Using
=instead of==—=assigns,==compares - Not checking return values — Always check if file operations and hardware access succeed
- Running GPIO programs without sudo — Most GPIO libraries need root access:
- Compiling with the wrong standard — Use
-std=c++17for modern features
Next Steps¶
You now have a solid C++ development environment on your Raspberry Pi. Continue to the hands-on tutorials:
➡️ Controlling an LED with GPIO — Your first hardware project: blinking an LED with C++ code.
Related Guides¶
- Essential Linux Commands — Master the terminal
- Modern C++ Masterclass — Deep dive into C++ language features
- ARM64 Assembly — Go even lower level with assembly programming