Solarian Programmer

My programming ramblings

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

Posted on December 8, 2017 by Sol

In this article I will show you how to install GCC 7 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 want to compile GCC 7 from sources check my previous article.

►  Continue reading

Raspberry Pi Raspbian - Compiling GCC 7.2

Posted on December 7, 2017 by Sol

This is a short article about compiling, building, GCC 7 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

PPM image from scratch in Python 3

Posted on October 25, 2017 by Sol

Someone asked me if it is possible to write image files in pure Python, without any libraries. The answer is, of course, you can implement any image format in pure Python, as long as you have a clear description of the image format and you know how to write binary files.

In this article, I will show you how to write binary PPM images in pure Python. Linux and recent versions of macOS have native support for PPM files, so you can open a PPM image using the default OS image viewer. For Windows, you can use a program like IrfanView. The PPM file format can store uncompressed RGB images in binary or human readable, ASCII format. As a side note, the PPM format is not an efficient format to store images, however it is relatively easy to implement and use, from a programming point of view.

►  Continue reading

Building GCC 7 on Chrome OS

Posted on October 5, 2017 by Sol

This is a short introduction on how to build GCC 7 from sources on your Chromebook or Chrome OS device. I’ve tested the procedure on an Intel based Chromebook with 4GB of RAM. In principle, same approach should work on an ARM device. I assume that you’ve enabled the Developer Mode on your Chrome OS device.

In order to be able to build GCC 7, you will need a C++ compiler. You can install an older compiler toolchain from Chromebrew, for more details read my previous article or, if you prefer the short version, open a shell tab and write the next commands:

1 shell
2 wget -q -O - | bash
3 crew install buildessential
4 crew install autconf
5 crew install automake

At this point, you should have GCC 4.9 installed on your machine. We will use the older 4.9 to compile GCC 7.

►  Continue reading

Chrome OS native development

Posted on September 13, 2017 by Sol

Today is the third day of my Two weeks programming on a Chromebook challenge. For the first two days I’ve played with enabling the Developer Mode on my Chromebook and installing Crouton and GCC 7 for C++17 development. Running Chrome OS and Crouton side by side is the easiest path for a complete Unix like development experience.

I think it could be interesting and more challenging for me personally, to investigate if I could do native Chrome OS development. By native I mean using only applications that run directly on Chrome OS or can be compiled to run on Chrome OS. Using a separate OS is not an option!

►  Continue reading

C++ local development on a Chromebook or Chrome OS device

Posted on September 11, 2017 by Sol

In this article, from my Two weeks programming on a Chromebook challenge, I will show you how to install locally a modern C++ development environment on your Chromebook.

Please make sure that you have Developer Mode enabled. If you want to be able to use a modern C++, like the one included with GCC 7, I suggest you to install Crouton using a CLI only install or using the LXDE desktop environment. My suggestion is to use the LXDE approach, unless you are Linux veteran and prefer to do your development from command line only.

►  Continue reading

My two weeks programming on a Chromebook challenge

Posted on September 11, 2017 by Sol


A few days ago, I bought a Chromebook for testing purposes. I was curious to see if it is possible to use it as a general development machine. My challenge is to use only the Chromebook and my iPhone for the next two weeks. I’m allowed to use local or external servers as long as I access these from my Chromebook.

In its default configuration, a Chromebook is not a developer machine. Chromebook was designed to be used as a cloud connected device, that can be used occasionally offline. The machine has a strong emphasis on security and ease of use for Internet related tasks: browsing, writing documents, email and so on.

►  Continue reading

Building Python 3.6 from source on Ubuntu and Debian Linux

Posted on June 30, 2017 by Sol

This is a short article about building Python 3.6 from source on Ubuntu 16.04 or Debian 9 Linux. At the time of this writing both Ubuntu LTS and Debian stable come with Python 3.5. Python 3.6 comes with a production ready asyncio module, new formatted string literals and many more improvements vs the old 3.5 version. You can read more about what’s new in Python 3.6 here. The procedure described in this tutorial also works with Windows Subsystem for Linux, WSL, aka Bash on Ubuntu on Windows.

As a side note, you should be able to use the same procedure on other Debian based systems, like Raspbian on Raspberry Pi, just replace apt with apt-get and you are good to go.

►  Continue reading

Compiling GCC 7 on macOS

Posted on May 21, 2017 by Sol

In this tutorial, I will show you how to compile from source and install the current stable version of GCC with Graphite loop optimizations on your macOS computer. The instructions from this tutorial were tested with Xcode 8 and Sierra (macOS 10.12).

Clang, the default compiler for macOS, supports only C, C++, Objective-C and Objective-C++. If you are interested in a modern Fortran compiler, e.g. you will need gfortran that comes with GCC. Another reason to have the latest stable version of GCC on your macOS is that it provides you with an alternative C and C++ compiler. Testing your code with two different compilers is always a good idea.

►  Continue reading

Emacs 25 on Windows Subsystem for Linux

Posted on May 18, 2017 by Sol

Recently, a reader of this blog asked me about using Emacs under Windows Subsystem for Linux. According to the WSL documentation, Emacs, Vim and other Linux applications that run from console should work just fine. Turns out that the situation is a bit more complex. At the time of this writing, Emacs works fine for normal text editing. But, when you try to install a new package directly from Emacs, the program is stopped and sent to background and doesn’t seem to be able to cleanly recover when you use fg. From a practical point of view, this makes Emacs, under WSL, useless if you need to install any package that is not built in.

Fortunately, there is a workaround for the above bug and I will show you in this post how to avoid the problem. As far as I know, Emacs under WSL seems to work without a problem only if you install a desktop environment like Xfce 4. The idea is to use the GUI version of Emacs when you need to install a package. For normal editing, you can use Emacs from the WSL console.

►  Continue reading

Building GCC 7 on Windows Subsystem for Linux

Posted on May 4, 2017 by Sol

In this article, I will show you how to compile from sources GCC 7.1 on WSL, Windows Subsystem for Linux. The default version of GCC, at the time of this writing, is 5.4 which is pretty old. GCC 7.1 has complete support for C++11, C++14 and experimental support for the current C++17 draft.

GCC 7.1 has *C++14 support enabled by default. If you want to try the new C++17 support use the -std=c++1z flag, example:

1 g++-7.1 -std=c++1z test.cpp -o test

►  Continue reading

Swift Alamofire tutorial uploading and downloading images

Posted on May 2, 2017 by Sol

This is a short tutorial about uploading and downloading images with Alamofire from an iOS, Swift 3, application.

I assume that you have the latest Xcode installed on your Mac, and that you know how to code small iOS applications in Swift 3.

I also assume that you have access to a server, ideally you have your own server with a domain name and a SSL certificate installed. If this is not the case, you can use a local Apache or NGINX web server. Personally, I use the free version of MAMPP which allows me to easily switch between Apache and NGINX if necessary. A good alternative to MAMPP is XAMPP. As a side note, macOS comes with an old version of Apache and PHP 5.3 already installed.

►  Continue reading