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 an experimented programmer (like me). There are some specificities you need to know in order 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 finally see why/when you should use it instead of Python.

If you are looking for quick 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.

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

If you are lost in all these new words and abbreviations, request my free Raspberry Pi glossary here (PDF format)!

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 you can use 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 absolutely 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 the 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 simplest code you can start with:

#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.

Raspberry Pi Bootcamp
Sale: 10% off today.
Take it to the next level.
I'm here to help you get started on Raspberry Pi.
Learn all the skills you need in the correct order.

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.
  • 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:

  • 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 you 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 and wires obviously but also many others fun sensors, motors and buttons). There is a complete tutorial on how to use each component, and in each lesson (there’s approximately 60 lessons or so), 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 shortlist 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.

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 about 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.

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 them) to achieve any project in the future. Raspberry Pi without Python is like a car without an engine, you miss all the fun part. Get 10% off by downloading it today!

Master your Raspberry Pi in 30 days
Sale: 10% off today.
Download the eBook.
Uncover the secrets of the Raspberry Pi in a 30 days challenge.
Learn useful Linux skills and practice multiples projects.

Is C++ a dying language?

According to Google Trends, C++ is slowly losing interest overtime, but remains in the top ten most used languages among developers. When speed is an essential factor, C++ is still one of the best choice 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 purpose. 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. 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 top10, 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 a bit with C++ to try something different. But I don’t think it will provide a major difference in your career which one you use on Raspberry Pi.

Can Python do everything C++ can?

As a whole, Python can do the same thing that 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 in 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. I think 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++
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.

Final Thoughts

Grab your free PDF file with all the commands you need to know on Raspberry Pi!

To sum up, C/C++ is an option available on Raspberry Pi if you already have basic knowledge with 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 learn 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.

If you have any comment, suggestion or just want to chat with us about this tutorial,
you can post your messages in the community on Patreon. See you there!

Raspberry Pi Resources

Not sure where to start?
Understand everything about the Raspberry Pi, stop searching for help all the time, and finally enjoy completing your projects.
Watch the Raspberry Pi Bootcamp course now

Master your Raspberry Pi in 30 days
Don’t want the basic stuff only? 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 multiples projects with step-by-step guides.
Download the e-book

VIP Community
If you just want to hang out with me and show your support, you can also join the Patreon community. I share behind-the-scenes content there and give you early access to my content. You’ll also get a shoutout when you join.
More details here

Need help to build something with Python?
Create, understand and improve any Python script for your Raspberry Pi.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now

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

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

Patrick Fromaget

I'm the lead author and owner of My goal is to help you with your Raspberry Pi problems using detailed guides and tutorials. In real life, I'm a Linux system administrator with a web developer experience.

Recent Posts

Master your Raspberry Pi - 10% off