In this tutorial series, we are going to learn modern c++ using the raspberry pi. So, what do I mean when I say modern? In August of 2011, a new version of the c++ standard was released that included some exciting new features. So, in this tutorial series, we will be taking advantage of these enhancements.
This tutorial series will start with the basics and then quickly move on to more advanced topics. Occasionally, we will take a step back and use what we have learned so far to do a project, so that you can exercise the skills that you have learned and build something exciting. By the end, you should have a great foundation in c++ that you can use to build your own projects.
For this tutorial series, we are going to be using the Raspbian OS. It’s very easy to install and set up and it seems to be the most popular OS for the raspberry pi. Raspbian comes with all of the tools that we need to start programming, so let’s dive in.
Here’s the code to print out a message to the screen:
using namespace std;
cout << "Hello Raspberry Pi!" << endl;
The first line includes a library. Libraries are prewritten code that you can leverage to do things faster and easier. Many common functions, such as writing a message to the screen, have already been written, so you can just reuse them and focus on aspects of your program that are unique to the problem that you are trying to solve.
The next line makes it so that you don’t have to prefix the functions in the library with the name of the namespace that it is in. This is to help fix name collision problems. Imagine if I wrote a function called foo and someone else wrote a function with the exact same name. If I tried to call that function the computer wouldn’t know which one I wanted, so to help with this namespaces were created. This line tells the computer that if it is looking for a function, that it should start by looking in the std namespace.
The next line declares a function. A function is a group of code that takes a set of inputs and produces an output. A simple example would be a sine function. The function would take in an angle, do some computations on that angle and then return the sine of the given angle. This function is called main and it doesn’t take any inputs, so there are just empty parenthesis after the name of the function. The function returns an error status. This is used to let other programs know if the program succeeded or failed. The main function is a very special function. It lets the computer know where your program starts, so every program must have a main function.
The curly braces signify the start and end of the function. Inside the function we only have two lines. The first one writes the phrase given to the screen, and the second one tells the computer that the program succeeded. If you change the text in between the two double quotes, you can make the computer write anything that you want to the screen.
Compiling and Running
So, take the above code and paste it into a file called 01_HelloRPi.cpp. Now open up a command prompt and navigate to the directory that contains the file. In the command prompt type:
g++ -std=c++0x 01_HelloRPi.cpp –o01_HelloRPi
This will take the code that you have written and create a program that can be run. The name of the program to create is specified with the –o flag, in this case we called it 01_HelloRPi. The –std=c++0x flag tells the compiler to use the latest version of the c++ standard. Then to run the program type in:
And that will write your message to the screen.
In this first tutorial we set up our environment to get ready to code and wrote a small program that prints a message to the screen.
In the next tutorial, we discuss how to read in data from the user to make an interactive program.
If you have any questions or comments about what was covered here, post them to the comments. I watch them closely and will respond and try to help you out.
01_HelloRPi.cpp.zip 251 bytes