getting started with c++ on raspberry pi

Getting Started with C++ on Raspberry Pi (Guide & examples)

When thinking about programming on Raspberry Pi, Python, and Scratch are the most common choices. I write a lot about Python on this website because it’s the most natural for beginners, but it isn’t the only option, and not necessarily the best in every situation. So, can you use C++ on Raspberry Pi, and how? That’s what I’ll explain in this article.

Raspberry Pi OS includes all the packages needed to code in C/C++ directly after the installation. Common libraries are pre-installed to interact with the specific Raspberry Pi components (GPIO, Camera, etc.), and the default text editor (Geany) has built-in support for this language.

Even if everything is ready to use, it doesn’t mean it’s simple – even for experienced programmers. There are some things you need to know to use your Raspberry Pi at its full potential. I will start by giving you more details about C++ and Raspberry Pi, then we’ll learn how to get started, and why/when you should use it instead of Python.

If you’re looking to quickly progress on Raspberry Pi, you can check out my e-book here. It’s a 30-day challenge where you learn one new thing every day until you become a Raspberry Pi expert. The first third of the book teaches you the basics, but the following chapters include projects you can try on your own.

Want the best experience? Become a premium member for ad-free browsing, access exclusive content, and ask questions in our private forums. Your membership helps support the site!

Can you Use C/C++ on Raspberry Pi?

C/C++ is supported natively on Raspberry Pi. The C preprocessor and other mandatory packages (like make and libstdc++) are preinstalled on any Raspberry Pi OS version. The source code can be written with Nano (command line text editor) or Geany (graphical editor).

As we’ll later learn, Raspberry Pi OS also includes the most common libraries you may need for basic programs and to control the GPIO pins (with Wiringpi). Interacting with the camera is also possible, even if you need to download and install some additional libraries.

Geany is a nice editor to create your first scripts (I have a tutorial about it here). It’s pre-installed on Raspberry Pi OS with Desktop. But it isn’t the only option. Visual Studio Code can now be added in one click from the Recommended Software tool, and many others can be installed easily (check my favorite text editors here and how to install them).

How to use C/C++ on Raspberry Pi

So, the short answer is yes, C and C++ can be used on Raspberry Pi, and everything is ready to use. But whatever your current level with these languages, you might need a few tips to get started.

I haven’t used C++ since high school, so I was a little rusty. But after a few tests, it’s back, and I can share some examples.

First try: Hello world

My goal is not to give a complete lesson about C/C++ here. Instead, I will explain how to use it on Raspberry Pi. But if you are entirely new to this, let’s start with the basics. Here is the most straightforward code you can start with:

Grab my Python cheat sheet!
If like me, you always mix the languages syntax, download my cheat sheet for Python here!
Download now
#include <iostream>
using namespace std;

int main()
   cout << "Hello you" << endl;
   return 0;
  • Create a new file (with Geany, Nano or any other text editor).
  • Copy and paste this source code.
  • Save the file (hello.cpp for example).
  • As a reminder, C/C++ requires pre-compilation, you can do it with this command:
    g++ -o hello hello.cpp
  • Then you can run your program with:

If everything works as expected, it should just display “Hello you”. Alright? Your C/C++ skills are back too?

If it’s your first script in C/C++, I wouldn’t read any further without understanding the language perfectly. It will only get more difficult. You can probably find a decent course on Skillshare for C/C ++ beginners that will explain everything in a few videos. Skillshare offers a one-month free trial with access to any course, so there’s no reason to learn the hard way.

Are you a bit lost in the Linux command line? Check this article first for the most important commands to remember and a free downloadable cheat sheet so you can have the commands at your fingertips.

Interact with the Raspberry Pi Camera

Ok, I hope you are still with me here. As I explained previously, the standard libraries are available, but you can “easily” use the Raspberry Pi components like the camera in your C++ programs.

I don’t know if there are other solutions, but the one I have found is to use Raspicam, a library available on GitHub, to use the Raspberry Pi Camera. It supports OpenCV, so it’s pretty good.

Install Raspicam on your Raspberry Pi

Here is how to install Raspicam on Raspberry Pi OS:

  • Start by installing cmake if not yet available:
    sudo apt install cmake
  • Clone and build the GitHub repository:
    git clone
    cd raspicam
    mkdir build
    cd build
    cmake ..
  • It will automatically detect if the OpenCV development files are installed, and add support for it if needed (check this guide to install OpenCV on your Raspberry Pi).
  • Once done, you can compile and install it with:
    sudo make install
    sudo ldconfig

That’s it, Raspicam is ready to use in your C++ projects.

Control the Raspberry Pi camera in C++

Here is a basic example to take a picture with the Raspberry Pi camera in C++:

 include <ctime>
 include <fstream>
 include <iostream>
 include <raspicam/raspicam.h>
 include <unistd.h>

 using namespace std;

 int main ( int argc,char **argv ) {
         raspicam::RaspiCam Camera; //Camera object
         //Open camera
         cout<<"Opening Camera…"<<endl;
         if ( ! {cerr<<"Error opening camera"<<endl;return -1;}
         //wait a while until camera stabilizes
         cout<<"Sleeping for 3 secs"<<endl;
         //allocate memory
         unsigned char *data=new unsigned char[  Camera.getImageTypeSize ( raspicam::RASPICAM_FORMAT_RGB )];
         //extract the image in rgb format
         Camera.retrieve ( data,raspicam::RASPICAM_FORMAT_RGB );//get camera image
         std::ofstream outFile ( "raspicam_image.ppm",std::ios::binary );
         outFile<<"P6\n"<<Camera.getWidth() <<" "<<Camera.getHeight() <<" 255\n";
         outFile.write ( ( char* ) data, Camera.getImageTypeSize ( raspicam::RASPICAM_FORMAT_RGB ) );
         cout<<"Image saved at raspicam_image.ppm"<<endl;
         //free resrources
         delete data;
         return 0;

This example comes almost entirely from the GitHub page. I fixed a few things that didn’t work on my Raspberry Pi. You can find the original source code here:

Note: when you compile it, don’t forget to add Raspicam in your command line, for example:
g++ -o camera camera.cpp -lraspicam

It will save the picture in a file named raspicam_image.ppm.

Control the GPIO Pins

Interacting with the GPIO pins is even easier, as the library is already installed on Raspberry Pi OS. The name of this library is Wiringpi, and you can find the reference documentation here.

Here is a quick guide on how to get started with it:

Grab my Python cheat sheet!
If like me, you always mix the languages syntax, download my cheat sheet for Python here!
Download now
  • Start by enabling the GPIO pins on your Raspberry Pi if not already done.
    I explain everything here, you can come back when you see some Python code.
  • Then you need to include Wiring pi in your header:
    #include <wiringPi.h>
  • After that, you should be able to use all the functions listed in the reference linked previously.
  • I tested with this example from SunFounder, and it worked fine.

By the way, I have the DaVinci Kit from Sunfounder, which includes a bunch of components to use with the GPIO pins (LED, wires, fun sensors, motors and buttons). There is a complete tutorial on how to use each component, and in each lesson, they explain how to do it in Python and C. If you want to strengthen your skills in C and Raspberry Pi, that might be a good fit.

Text Editors available on Raspberry Pi

I told you that you can use Nano or Geany to write your C or C++ code on Raspberry Pi, but don’t forget that many other editors are available. I have a short list here of my favorite text editors, but you can also install a complete IDE if needed.

I don’t have a lot of experience with heavy IDE, as I code mainly in HTML/PHP or Python, but I’m sure you’ll find a way to install your favorite one.

Download Your Essential Linux Commands Guide!
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now

Which is better: Python or C++?

All of this is fine, it’s “possible” to code in C++ on Raspberry Pi, and it’s well-supported. But is it a good idea? Why does everyone speak about Python and not C++? I will answer your questions about this in this last section.

Is C++ or Python easier?

As a whole, C++ is a complex language with powerful features, but it’s difficult for beginners to grasp. Python is easier to learn, it’s close to the English language, and is ideal for simple scripts.

Also, Raspberry Pi is built around the Python language, so all the libraries are directly available (or easy to install). For a beginner, this is a no-brainer choice. Get started with Python, and experiment with C/C++ later once you understand the logic behind any code.

For more details, read this article: 5 Best Programming Languages for Your Raspberry Pi Projects.

By the way, if you get overwhelmed as soon as Python is required for a project, I recommend checking out my e-book “Master Python on Raspberry Pi“. It will guide you step-by-step to learn the essential concepts (and only the essential concepts) required to achieve any project in the future. Raspberry Pi without Python is like a car without an engine, you miss all the fun parts. Get 10% off by downloading it today!

Is C++ a dying language?

According to Google Trends, C++ is slowly losing interest over time but remains in the top ten most used languages among developers. When speed is an essential factor, C++ is still one of the best choices for programmers.

LanguageUsage among developers
Most used programmed languages (excluding SQL and bash/PowerShell)

If you look at the latest data from Statista, web languages and Python are way more used than C++, but they are different choices for different purposes. You won’t use the same language for a website, a mobile app or a graphic driver.

C and C++ are often popular languages at school too. I remember learning Java, C and C++ at school. They are not the easiest ones to start with, but it’s good to see them at school to learn more complex problems. I suppose it’s still the case, that explains why there are still many developers who use these languages.

GitHub’s statistics also show a similar ranking. C++ is in the top 10, with a stable (or slightly increasing) trend. So don’t worry, learning C/C++ is not a waste of time.

Which is more in demand: Python or C++?

According to Indeed USA, Python is more in-demand than C++. There are currently 44,311 jobs available for Python developers and 15,778 jobs for C++ developers. In terms of salary, C++ developers are paid better than Python developers ($113,561 on average versus $107,397).

Anyway, if you know how to code (and can prove it), you should easily find a job, whatever your best programming language. A good developer can switch quickly from one language to another. As explained previously, start with Python and then play with C++ to try something different. But I don’t think it will provide a major difference in your career in which one you use on Raspberry Pi.

Can Python do everything C++ can?

As a whole, Python can do the same thing as C++. There are some slight differences, but Python can use C libraries, and C can use Python libraries, so it’s possible to replace one language with another in most cases.

I won’t enter all the details, but remember that C works at a lower level, so it’s generally way faster. If you are doing a small script to control your lights at home (like explained here), turn on a LED with GPIO or even do basic movements with a robot kit (my favorites are here), Python will be just fine and way faster to code.

C++ can be useful on bigger projects, with many components that need to work together, but it should be limited. In short, Python is good for basic developments like scripts and small projects, while C++ is generally chosen for larger programs.

C++ vs Python on Raspberry Pi: Comparison table

Work fine on Raspberry PiPerfect for most Raspberry Pi projects
C++ is faster than PythonPython can be slower than C++
A slight decrease in trendsStable trends and more in demand than C++
Require pre-compilationDon’t need compilation
Difficult to learn for beginnersEasy for beginners
C++ vs Python: Pros & cons

By the way, if you are looking for Python examples, you can find 15 Python projects to create on Raspberry Pi here.

Download Your Essential Linux Commands Guide!
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now

🛠 This tutorial doesn't work anymore? Report the issue here, so that I can update it!

If you are looking for exclusive tutorials, I post a new course each month, available for premium members only. Join the community to get access to all of them right now!

Final Thoughts

To sum up, C/C++ is an option available on Raspberry Pi if you already have basic knowledge of this language or want to try big projects. But in most cases, Python will be easier and faster to use, especially for beginners.

I still recommend using Python on your Raspberry Pi (and learning it the right way if needed), the Raspberry Pi is built for this and I suppose your goals are not to code the next AAA game or CRM on a Raspberry Pi, so C++ is not mandatory. But it’s nice to know that it’s an option if you find a way to do something in C/C++ and don’t know how to do it in Python.

For example, you can try to play with OpenCV on your Raspberry Pi, using C++ instead of Python. This way, you’ll have something concrete to learn, where you can play with the language and the hardware (the Raspberry Pi camera).

Whenever you’re ready, here are other ways I can help you:

The RaspberryTips Community: If you want to hang out with me and other Raspberry Pi fans, you can join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.

Master your Raspberry Pi in 30 days: If you are looking for the best tips to become an expert on Raspberry Pi, this book is for you. Learn useful Linux skills and practice multiple projects with step-by-step guides.

The Raspberry Pi Bootcamp: Understand everything about the Raspberry Pi, stop searching for help all the time, and finally enjoy completing your projects.

Master Python on Raspberry Pi: Create, understand, and improve any Python script for your Raspberry Pi. Learn the essentials step-by-step without losing time understanding useless concepts.

You can also find all my recommendations for tools and hardware on this page.

Similar Posts

One Comment

  1. Magnificent website. A lot of helpful information here. I抦 sending it to a few pals ans also sharing in delicious. And of course, thank you on your effort!

Comments are closed.