Solarian Programmer

My programming ramblings

Building GCC as a cross compiler for Raspberry Pi

Posted on May 6, 2018 by Sol

In this article, I will show you how to build GCC 8 as a cross compiler for Raspberry Pi. A cross compiler is a compiler that runs on an operating system and produces executables for another. This is really useful when you want to use your beefy computer to build a library or other large piece of code for Raspberry Pi. As a practical example, at the end of the article, I will show you how to use the cross compiler to build GCC itself as a native Raspberry Pi application.

Part of this article is a compilation of what I’ve learned reading other people posts. Here is a list of the sources I’ve used:

From the above list, the first article is the one that is the most complete and, if you follow it, you end up with a cross compiler that partially works. To be fair, the article wasn’t written for Raspberry Pi. I recommend that you read it if you want to see a more in depth explanation of certain steps of the process.

►  Continue reading


Install NumPy, SciPy, Matplotlib and OpenCV for Python 3 on Ubuntu 18.04

Posted on April 25, 2018 by Sol

This is a short article about installing NumPy, SciPy, Matplotlib and OpenCV on the latest Ubuntu LTS, which at the time of this writing is 18.04. Ubuntu 18.04 comes with Python 3.6.5.

Let’s start by making sure we have an updated system:

1 sudo apt update
2 sudo apt upgrade

►  Continue reading


Raspberry Pi - Install Clang 6 and compile C++17 programs

Posted on April 22, 2018 by Sol

In this article I will show you how to install Clang 6 on your Raspberry Pi system and how to compile C++17 programs. At the time of this writing Raspbian is based on Debian Stretch, which comes with the stable but slightly outdated GCC 6.3 as the default C and C++ compiler. If you prefer to use GCC 7 I wrote an article about installing GCC 7 on Raspberry Pi.

Let’s start the installation process. Open a Terminal and download the official binary of Clang 6 for Raspberry Pi:

1 cd ~
2 wget http://releases.llvm.org/6.0.0/clang+llvm-6.0.0-armv7a-linux-gnueabihf.tar.xz

►  Continue reading


Python OpenCV - show a video in a Tkinter window

Posted on April 21, 2018 by Sol

In my last tutorial I’ve shown you how to create a minimal Tkinter application: load an image with OpenCV, plot the image on a Tkinter window and apply a blur filter when the user presses a button. Another interesting application is to show a camera feed or an exiting video on a Tkinter window.

Simple Tkinter window

►  Continue reading


Python OpenCV - show an image in a Tkinter window

Posted on April 20, 2018 by Sol

This is a short tutorial about using Tkinter, the default Python GUI library, with OpenCV. On Windows, Tkinter is bundled with the official Python installer. On Linux, you can install Tkinter using your distribution package manager. The situation is a bit more complex on macOS, that comes with Python 2.7 and an old version of Tkinter, at the time of this writing, the easiest path is to install Miniconda Python 3 that comes with the latest Tkinter.

OpenCV includes some rudimentary GUI capabilities, useful if you need to show a video or an image, get the mouse or the keyboard input. But, if you need something more complicated like buttons, drop down lists, menus, labels, text boxes and so on, you need to use a dedicated GUI library like Qt or Tkinter.

In the remaining of this article, I’ll assume that you have Python 3.6, Tkinter 8.6 and OpenCV 3.3 or newer installed on your machine. If you need help to install the above on Windows, macOS or Linux check my previous articles.

►  Continue reading


Writing a minimal x86-64 JIT compiler in C++ - Part 2

Posted on January 12, 2018 by Sol

In my last article, I’ve shown you how to generate the machine code for a function at runtime, copy this code in a part of the memory, marked as executable, and call it from C++. Now, we’ll go the other way around, we’ll call a C++ function from a function generated at runtime. Like before, I assume that you try the code on Linux or macOS.

If you remember from part 1, we’ve started by adding machine code instructions in an std::vector and copying this code to an executable memory page. While this was a fine approach from a didactic point of view, in practice, you will probably want to write the code directly to the executable memory. Here is an example of how I propose to do it:

1     MemoryPages mp;
2     mp.push(0x48); mp.push(0xb8);

The object mp, from the above piece of code, will ask the OS for memory, release this memory when it is not needed and will have some helper member functions that will let us push pieces of machine code to the executable memory. We can also add safety features, e.g. a mechanism to check if we can push more data on the executable memory or if we’ve reached the bounds of the allocated memory pages.

►  Continue reading


Writing a minimal x86-64 JIT compiler in C++ - Part 1

Posted on January 10, 2018 by Sol

In this article, I will show you how to write a minimal, bare-bones, x86-64 JIT compiler in C++ that runs on macOS, Linux and could potentially run on Windows through WSL.

For our purposes, JIT compilation is a technique through which a program generates machine code at runtime, based on the user input. A C++ program is AOT (ahead of time) compiled, which typically means that once the original code was compiled for a particular machine it can’t be changed at runtime (and from a security point of view this is a desirable feature). A simple, useful application, of a C++ JIT compiler is on the fly compilation of a new function that is based on other functions already defined in the original code.

Let’s start with an even simpler example. Write a C++ program that asks the user for his name and generates, at runtime, a function that simply prints a greeting. While not a very practical program (you really don’t need to compile this to a separate function), this example will exemplify how to create and execute code at runtime.

►  Continue reading


C++17 constexpr everything (or as much as the compiler can)

Posted on December 27, 2017 by Sol

During the holidays I did some catch up with CppCon 2017. One of the titles that I had on my to watch list for a few months now was constexpr ALL the Things! by Bean Deane and Jason Turner. Please note that I wrote most of this article before actually watching the presentation.

The title of the presentation made me curious if I can optimize an old piece of code that used a huge 2D array of coefficients as the initial condition for a long calculation. In order to avoid recalculating the big array of coefficients, I used to keep them in a file and simply load the data in memory every time the code was executed. The promise of using a constexpr was that I could avoid keeping two executables (the code that generated the coefficients and the code that did the actual work) and a data file. Replacing everything with a single binary was interesting and could potentially be faster.

In order to test the above, I devised a simpler model - fill an array with data generated at compile time.

►  Continue reading


Clang 6 in a Docker container for C++17 development

Posted on December 14, 2017 by Sol

Updated 24 April 2018

If you want to try the new C++17, using Clang in a Docker container, you are in the right place. Running Clang in a container has the advantage that it is light on resources and won’t mess with your underlying OS. The last point is especially important if your host operating system is macOS, on which it is a really bad idea to directly install a binary Clang other than the one that comes with Xcode. I’ve tested the approach presented in this article on Windows 10, macOS High Sierra and Ubuntu Linux.

I assume that you have Docker installed on your machine, if not go to the Docker website and install it. After the installation, open a Terminal or, if you are on Windows, a PowerShell window and check if Docker was properly installed with:

1 docker version

►  Continue reading


Linux and WSL - Install Clang with libc++ and compile C++17 programs

Posted on December 13, 2017 by Sol

Updated 24 April 2018

In this article, I will show you how to install Clang with libc++ on Ubuntu Linux and Windows System for Linux. Same procedure should work on other Debian based Linux distributions. Latest version of Clang has partial support for the newest C++ standard, C++17.

If you want to compile Clang from sources check my previous post. In this article, we are going to use the official Clang 6.0.0 binary from http://releases.llvm.org/download.html.

Open a Terminal (on Windows 10, you can open a Command Prompt or a PowerShell window and write bash to start WSL) and make sure your system is updated:

►  Continue reading


Raspberry Pi - Install GCC 8 and compile C++17 programs

Posted on December 8, 2017 by Sol

Updated 5 May 2018

In this article I will show you how to install GCC 8 on your Raspberry Pi system and how to compile C++17 programs. At the time of this writing Raspbian is based on Debian Stretch, which comes with the stable but slightly outdated GCC 6.3 as the default C and C++ compiler.

If also you want to install Clang 6 on your Raspberry Pi, check my article.

If you want to compile GCC 8 from sources check my previous article.

►  Continue reading


Raspberry Pi Raspbian - Compiling GCC 8.1

Posted on December 7, 2017 by Sol

Updated 5 May 2018

This is a short article about compiling, building, GCC 8 from sources and how to get started with C++14 and C++17 on Raspberry Pi with Raspbian. At this time Raspbian comes with the stable but slightly outdated GCC 6.3 as the default C and C++ compiler.

I’ve tested the next steps on a Raspberry Pi 3, but it should work on all current models. Fair warning, compiling GCC from source is a fairly long and intensive process and Raspberry Pi 3 tends to overheat, make sure that you have heat sinks installed or proceed at your own risk. Alternatively, you can use the binary I’ve made, you can can find it on Bitbucket.

►  Continue reading


Install Code::Blocks and GCC 7 on Windows

Posted on November 22, 2017 by Sol

In this article, I will show you how to install a minimal C, C++ compiler toolchain and an IDE on Windows. More to the point, you will install GCC 7 and Code::Blocks. The advantage of this setup is that you will be able to compile any standard C99, C11, C++11, C++14 and C++17 program on your Windows machine. Please note, that Code::Blocks is available in two versions: as a standalone IDE, as an IDE and an outdated version of GCC (4.9.2). I will show you how to use the latest version of GCC with the Code::Blocks IDE.

►  Continue reading


The eight queens puzzle in Python

Posted on November 20, 2017 by Sol

The eight queens puzzle, or the eight queens problem, asks how to place eight queens on a chessboard without attacking each other. If you never played chess before, a queen can move in any direction (horizontally, vertically and diagonally) any number of places. In the next figure, you can see two queens with their attack patterns:

Two queens attack pattern on a chessboard

At the end of the article we present a Python 3 solution to the eight queens puzzle.

We can generate a solution to the problem by scanning each row of the board and placing one queen per column, while checking at every step, that no two queens are in the line of attack of the other. A brute force approach to the problem will be to generate all possible combinations of the eight queens on the chessboard and reject the invalid states. How many combinations of 8 queens on a 64 cells chessboard are possible ?

The combinations formula is

which, for our particular case is:

►  Continue reading


Barnsley Fern in Python 3

Posted on November 2, 2017 by Sol

In this article, I will show you how to render the Barnsley Fern in Python 3. The Barnsley Fern is a fractal that can be generated using four simple affine transformations of the form:

where the coefficients of the transform are:

     w           a           b           c           d           e           f           p     
1 0 0 0 0.16 0 0 0.01
2 0.85 0.04 -0.04 0.85 0 1.6 0.85
3 0.2 -0.26 0.23 0.22 0 1.6 0.07
4 -0.15 0.28 0.26 0.24 0 0.44 0.07

In the above table p represents the probability factor for a transform. For example, the second transform will be used 85% of times, third transform 7% and so on.

From a practical point of view, the Barnsley Fern is generated starting with an initial point , , and iteratively calculating the next point using one of the above transforms.

At each step, we generate a random number r from the interval [0, 1) and, interpreting this number as a probability, we pick the corresponding transform. For example, if r is in the interval we pick , if r is in the interval we pick and so on. Note, we define the intervals using the cumulative sum of the probability factors.

This is the result, after 1000000 iterations:

Barnsley Fern

►  Continue reading