Raspberry Pi C++ Intro thumbnails

[ Raspberry Pi C ++] The Beginnings, A Very Brief Overview

Next Post: Setting up the compiler


C++ Can be a tough language to pick up, especially if you have no prior programming experience. The usual goto language for beginners with the Raspberry Pi is Python due to its simplicity and enforced structure. Whereas, C++ is the language of choice when speed and efficiency is required but with it, you need to know its quirks.

This post aims to give a brief overview of C++ so that the following tutorials will make sense if you are coming from an inexperienced background with regards to the language and it’s concepts.

If you plan on following the Raspberry Pi C ++ tutorials on this blog, I recommend reading over these posts first:

What is C++?

C++ is a language where the programmer gets full control whereas other languages (such as Python) may abstract certain features. One example is garbage collection, with C++ you have to allocate and deallocate memory for your objects and structures manually. This is obviously an advanced topic to dive into so I will avoid this, but just know that C++ is powerful.

The Compiler

C++ is a compiled language (as opposed to scripting languages such as Python, LUA and C# etc.). What this means is that the code we write has to be passed through a compiler before it can be executed. The compiler will translate the human readable code into instructions that the computer will understand – this is targeted at the CPU architecture. For example, the Pi has an ARM CPU, this means that the C++ compiler has to translate the human readable code into ARM instructions.

There are quite a lot of compilers out there, but our focus will be using the G++ compiler (the C++ subset of the GCC compilation set) for use with the Raspberry Pi.

The Structure

With C++, it is vital to break your code up into multiple files. The two main file types you will come across is a .h file and a .cpp file.

The .h file (AKA a “Header File”) is used to declare your classes / function “prototypes”. Whereas a .cpp (AKA a “Source File”) is where you define these functions / classes. The difference being that a declaration is like saying “Hey, there is a function in my code called MyFunction” and a definition is “Hey, this is what MyFunction is going to do when called”.

To put this into an example, here are two snippets of code:

// declaration of function "HelloWorld"
void HelloWorld();
hello.h
#include "Hello.h"

// definition of function "HelloWorld"
void HelloWorld()
{
    printf("Hello, World!");
}
hello.cpp

You may note the line “#include “Hello.h””. This is called an “include statement”, and is how a source file knows where to look for function declarations. When using a third party library, this is how you include their function declarations into your code.

Compilation Types

Now that we know of a compiler and what file types are being used, we finally need to know what to compile our code into. There are 3 main compilation types:

  • Application executable (.exe on Windows, Linux doesn’t really have a set file extension for these)
  • Static Library (.lib on Windows, .a on Linux)
  • Shared Library (.dll on Windows, .so on Linux)

An application executable is as it sounds, an application that you can execute either from double clicking it’s file icon or from the terminal. Code compiled into an executable cannot be used directly in other projects.

The two library types (static and shared) are used for code that could potentially be shared across multiple projects. There is a difference between the two types but it is out of scope for this brief overview. Just know that any third party libraries you come across will more than likely be compiled into one of these forms.

The term for using a library in your C++ application is called ‘linking’. To link a library into your code, you must pass the library name along with the location of where the compiler will find its .a or .so file.

Further Reading

I obviously cannot cover how to program in C++, this is way overkill for these tutorial posts but is covered extensively online / in books. I just want to give an overview on how a compiler can take C++ code and compile it into an application / library files. Maybe one day I will make a post going into detail on each step which would be fun!


Next Post: Setting up the compiler

Published by

Nick

Nick Cullen is a software developer living in South Wales, UK. He is primarily focused around coding in C++ and C# and loves tinkering with new programming languages and technologies. A key technological interest of his is Raspberry Pi development, which he has helped pioneer a unique product commercially using a Pi and programming the software in C++.

6 thoughts on “[ Raspberry Pi C ++] The Beginnings, A Very Brief Overview”

    1. Hi Juri,

      Thanks for the comment, and that tool does look quite cool! Reminds me of the QT interface.

      These few posts are for people who want to learn about using the compiler with Raspberry Pi. But I agree, that it will be a great tool to get started with, with regards to wanting to learn C++.

      Nick

    1. Hi Ali,

      Thanks for sharing that! I would say these posts are aimed at people who want to mess around with the build system. I feel, with C++ especially, knowledge in this area is key to understanding the architecture of a C++ project.

      That being said, what you linked is always nice for learning how to learn the C++ syntax and experiment quickly.

      1. Can you please do a guide on using the raspberry pi’s gpio pins with C++?!

        That would make my dreams come true, Thanks!

        1. Hey! Thanks for the comment and looks like you’re in luck! 🙂

          It’s looking to be post 4 & 5 of these tutorials which will show GPIO access with C++. Will be published in just under 2 weeks from now

Leave a Reply

Your email address will not be published. Required fields are marked *