My programming ramblings

Posted on May 13, 2019 by Paul

In this article, I will show you how to build the newly released Windows Terminal from Microsoft. At the time of this writing, you need to build the application yourself, but it is expected to be released in the Windows Store by the end of this year.

Windows Terminal is a new Terminal application from Microsoft that has tabs, multiple profiles, colors, text resizing, transparency and a lot of features which makes it similar with the best Terminal applications from Linux and macOS.

There is also a video version of this tutorial:

In order to be able to use the Windows Terminal application, you will need at least Windows 1903 which is going to be released this month, in a matter of days. If you are like me and can’t wait until the official release of 1903, you can install Windows Insiders edition which will let you build and use the new Terminal application today. If you already are on Windows 1903, or up, you are good to go.

Posted on May 9, 2019 by Paul

Thanks to Bartlomiej Filipek for the suggestion to try C++17 parallel algorithms with GCC 9 and Intel TBB on Linux.

GCC 9.1 has support for C++17 parallel algorithms by using the Intel TBB library. In this article, I will show you how to build Intel TBB from sources on your machine and how to sort a vector of random numbers in parallel using C++17 std::sort.

At the time of this writing, the latest stable version of Intel TBB is version 2019 Update 6. You can find the latest release of TBB on their GitHubrepo. If a newer version was released, change the version number from the next instructions accordingly.

Posted on May 4, 2019 by Paul

In this article, I will show you how to cross compile C and C++ programs on a x86-64 machine for Raspberry Pi using Clang 8. The advantage of cross compiling on a x86-64 system for armhf is that, usually one has a beefy laptop or desktop computer that can speed up, by an order of magnitude or more, the compilation of C and C++ programs. This is more visible with large C++ programs, that can take hours to build on a Raspberry Pi 3 or days on a Raspberry Pi Zero.

We’ll start by building Clang as a cross compiler on the host x86-64 system. This will let us use the latest stable version of Clang, which at the time of this writing is 8. You will also be able to run the binaries compiled with this Clang version on all versions of Raspberry Pi.

As a side note, you can install the official Clang binary directly on your Raspberry Pi, check this article if you are interested. The problem with this approach is that the compiler will work only on Raspberry Pi 2 and up, no support for RPi 1 or Zero, and that it is really slow for large C++ projects.

I recommend that you do the build in a Debian Stretch virtual machine or a Docker container in order to not mess your system. If you decide to install Stretch in a virtual machine, make sure to use the minimal netinst system. It is really important that you start with a bare bone system, because we need to install armhf executables and libraries. By using a minimal system we avoid potential conflicts with the native x86-64 versions.

Posted on April 17, 2019 by Paul

This is a short note about measuring the execution time for a C program, or delaying the program execution for a specified amount of time. I’m only interested in using mechanisms that let us measure the execution time with at least millisecond resolution, so I won’t mention the classical C way of getting the time of day with second resolution time or using the clock function.

You can find all the code examples at the GitHub repo for this article.

Since C11, the standard way to get a good time estimate is to use the timespec_get function, which returns the system clock time:

Posted on April 8, 2019 by Paul

In this article I will show you how to use ANSI escape codes to control the colors of your Terminal, write text at arbitrary positions, erase lines of text or portions of the terminal and move the cursor. The advantage of using ANSI escape codes is that, today, these are available on most operating systems, including Windows, and you don’t need to install third party libraries. These are well suited for simple command line applications. If you need to do complex text graphics check the ncurses library.

You can find all the code examples at the GitHub repo for this article.

There is also a video version of this tutorial:

Let’s start with a simple C example that will print a green text message on your Terminal:

Posted on April 3, 2019 by Paul

In this article, I will show you how to read a text file line by line in C using the standard C function fgets and the POSIX getline function. At the end of the article, I will write a portable implementation of the getline function that can be used with any standard C compiler.

Reading a file line by line is a trivial problem in many programming languages, but not in C. The standard way of reading a line of text in C is to use the fgets function, which is fine if you know in advance how long a line of text could be.

You can find all the code examples and the input file at the GitHub repo for this article.

Let’s start with a simple example of using fgets to read chunks from a text file. :

Posted on March 27, 2019 by Paul

In this article I will show you how to pass a multi-dimensional array as a parameter to a function in C. For simplicity, we will present only the case of 2D arrays, but same considerations will apply to a general, multi-dimensional, array.

There is also a video version of this tutorial:

In C, a two-dimensional array is a one-dimensional array of one-dimensional arrays. For example, an array of two rows and 3 columns is technically a one-dimensional array of two elements, where each element is a one-dimensional 3 elements array. Here is an example of a 2D array of two rows and three columns:

Posted on March 26, 2019 by Paul

In this article I will show you how to install Docker on macOS. In order to exemplify the use of Docker, we’ll use a container to build and run a simple C or C++ application with GCC 8.

There is also a video version of this tutorial:

Start by downloading Docker Desktop for macOS. If you don’t have a Docker account, sign up for one it is free and it only takes a few minutes to create one. Once you are logged in, you should be able to get the Docker app for macOS. Open the dmg file and drag the installer to your Applications, you can safely accept all the default settings.

Posted on February 22, 2019 by Paul

In this tutorial, I will show you how to implement a singly linked list data structure in C++17 with smart pointers. This is also useful as an exercise in learning about how you can use smart pointers instead of raw pointers to manage the memory of the list elements.

A singly linked list is a collection of nodes (elements), in which every node contains a data field and a link to the next node. The first node of a singly linked list is usually named head or root. The last element of the list is usually named tail and his next field links to nothing.

An interesting property of the singly linked list is that, in order to visit a certain node, you will need to pass through all the previous list nodes until the searched one. For example, if you want to print the third element of a singly linked list, you will need to start at the list head and pass through the first and second nodes. In other words, you can’t have random access to the singly linked list elements, like you can for example with an array. If you want to read more about the linked lists theory check the books recommended at the end of the article.

For the purpose of this article, we are going to implement a few of the linked list possible operations: insertion, deletion and traversal.

Posted on January 13, 2019 by Paul

Updated 15 April 2019

In this article I will show you how to use the C++17 std::filesystem library to write a simple file watcher or file monitor. The advantage of using the C++17 std::filesystem library is that your code will be portable on all operating systems for which a C++17 compiler is available.

We are going to implement a C++17 file watcher that will monitor a given folder for file changes. For our limited purposes, we’ll monitor only the creation, modification and deletion of all files from the watched directory. The base folder will be checked for changes at regular time intervals and, in case of changes, we’ll run a user defined function.

Posted on December 18, 2018 by Paul

In this article, I will show you how to cross compile the latest version of OpenCV for Raspberry Pi and BeagleBone Black. At the time of this writing, OpenCV is at version 4.0.0 while the version available with the default Debian based OS on Raspberry Pi and BeagleBone Black is 2.4.9. Building OpenCV 4 directly on a Raspberry Pi or BeagleBone Black is doable, but it could take a few hours. Cross compiling OpenCV for armhf is a 20 - 30 minutes process, depending on the speed of your computer it could be even shorter.

I recommend that you do the build in a Debian Stretch virtual machine or a Docker container in order to not mess your system. If you decide to install Stretch in a virtual machine, make sure to use the minimal netinst system. It is really important that you start with a bare bone system, because we need to install armhf executables and libraries. By using a minimal system we avoid potential conflicts with the native x86-64 versions.

For the remaining of this article, I will mention only Raspbian and Raspberry Pi, if you are using a BeagleBone Black everything should work the same.

I’ll assume that you have a clean Raspbian install. I’ve used the latest available desktop image. In principle you should be able to follow the article using Raspbian Lite, but I did all my tests using the full desktop version of Rasbian.

Posted on December 7, 2018 by Paul

This is a short article about sharing your Internet connection through USB with a Raspberry Pi Zero. The procedure presented here will work with all versions of Raspberry Pi Zero (with or without onboard WiFi). You will need a Raspberry Pi Zero, a micro SD card and a USB to micro USB data cable.

Start by downloading the latest Raspbian image, I’ve used Raspbian Stretch Lite. Once the download is finished, extract the archive and flash it to your micro SD card. The Etcher flasher app works on macOS, Windows and Linux.

Posted on December 4, 2018 by Paul

This is a short article about sharing your Internet connection with a BeagleBone Black through USB on macOS Mojave. I assume that you’ve gone through the Getting Started setup and that you’ve installed the latest Debian image on your BBB.

Connect the BBB to your Mac through USB and start the device. Once the device is on, open a Terminal and use ssh to connect to your BBB:

Disclaimer:All data and information provided on this site is for informational purposes only. solarianprogrammer.com makes no representations as to accuracy, completeness, currentness, suitability, or validity of any information on this site and will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use. All information is provided on an as-is basis. solarianprogrammer.com does not collect any personal information about its visitors except that which they provide voluntarily when leaving comments. This information will never be disclosed to any third party for any purpose. Some of the links contained within this site have my referral id, which provides me with a small commission for each sale. Thank you for understanding.