Raspberry Pi

Getting Started with C++ in Raspberry Pi

C++ is a versatile and fast-speed language that has a wide range of applications such as Object-Oriented Programming (OOP), embedded systems, creating compilers, and others. C++ is very closely related to C due to which it has wide online support available. Also, it provides great portability which means you can run the same code of C++ on all types of operating systems like Windows, macOS, and Linux. So, the good news for Raspberry Pi OS users is that they can also use C++ on Raspberry Pi. In this tutorial, we will discuss how to use C++ on Raspberry Pi OS.

Let’s begin!

Getting Started with C++ in Raspberry Pi OS

There are two methods to start coding in C++ on Raspberry Pi:

Method 1: Code in C++ Using Geany IDE on Raspberry Pi

By default, the Raspberry Pi does have a Geany text-editor/IDE that can be used to code in different programming languages including C++. You can open this editor through the terminal using the following command:

$ geany editor

To open this editor from the desktop, go to the “Application Menu” > “Programming” > “Geany Programmer’s Editor” options.

The Geany Editor window will appear on the screen:

You can type any C++ code within this opened window. I have shared mine and if you are new to C++, you can copy the same code below for printing a message “Hello linux-hint” using C++.

#include <iostream>

using namespace std;

int main()

{

  cout<<"Hello linux-hint" << endl;

 

   return 0;

}

Code to print “Hello linux-hint” on Geany editor:

After adding the C++ code, go to the “Document” option from the menu bar, select the “Set Filetype” and choose the “C++ source file” option from the “Programming Languages” section:

Once the C++ source file is selected, the code window will look like this as shown below:

Once you are done with it, it is time to save the file of C++ code. To save the file, click on the “File” option and then select “Save As” from the drop-down list:

A “Save File” window will appear on the screen.

You can name your file according to your choice, here I have named it as “hello-Linux.cpp”:

Finally, your C++ code file is saved:

Compile the C++ code using the Compile button from the menu bar, which is highlighted in the below image:

The result of your compilation will be displayed on the output window which is at the bottom of the interface:

Now finally you can run the code by using the below-highlighted icon:

The output will be displayed on a separate window.

Method 2: Code in C++ Using Terminal on Raspberry Pi

If you want to code C++ through the terminal just like any other Linux-based application, you can follow the below-mentioned steps:

Step 1: To run C++ code through the terminal, first you have to create a C++ file and for that, you can use the following command:

$ sudo nano <source file name>.cpp

Note: I have used linux-hint2 name for my source file, users can choose any name according to their choice.

$ sudo nano linux-hint2.cpp

By using the above command, an empty window will appear on the screen:

You can type your C++ code here, I am using the same above code again:

#include <iostream>

using namespace std;

int main()

{

cout<<"Hello Linux Hint!"<<endl;

 

return 0;

}

Once you are done typing the code, then press “Ctrl + X” and then “Y” to save the file.

Now let’s check if the source file is saved or not for that, use ls command to display the list of files:

$ ls

To run C++ on Raspberry Pi requires a pre-compilation to generate an executable file, so for that follow the below-mentioned command:

$ g++ -o <filename> <souce_file>.cpp

Note: Users can use any name in place of <file name>. Here, I have used linux-hint.

Let’s check if our executable file is generated or not and for that, we will use the below-mentioned command:

$ ls

Now, finally our file is ready to run, and to run the file use the below-mentioned command:

$ ./<file name>

Note: Remember here you have to use the name of the executable file that you have just created above.

That’s it for the process you can also try some more C++ codes by following the same instructions.

Final Words

To use C++ in Raspberry Pi, there is already an editor known as Geany Programmer’s Editor, which can be run both from the desktop and terminal. Users just have to create a C++ source file first and then build the code to generate an executable file. Later, the executable file can be run to display the output, You can also use the terminal to create and run your C++ code. The step-by-step details are already provided in the above guidelines.

About the author

Zahra Zamir

An Electronics graduate who loves to learn and share the knowledge, my passion for my field has helped me grasp complex electronics concepts and now I am here to share them with others.