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:
- Connecting to the Raspberry Pi via SSH
- Using VS Code with Raspberry Pi (Mac) or Using VS Code with Raspberry Pi (Windows)
- Using Synergy on Raspberry Pi (If you are using a GUI on your Pi)
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:
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