Solarian Programmer

My programming ramblings

Building SBCL - Steel Bank Common Lisp on Windows

Posted on August 20, 2019 by Paul

In this article, I will show you how to build the latest stable version of SBCL - Steel Bank Common Lisp on Windows. The officially provided binary of SBCL for Windows is typically older than the latest stable version, so I wrote this tutorial in the hope that it will be useful for people that want to use the latest version of SBCL. The article will also be useful for people that want to customize the installation of SBCL, for example you could build SBCL with more or less memory than the default 1GB that is allocated for the 64 bits version.

In order to build SBCL we’ll need another Common Lisp compiler, one will typically use an older version of SBCL and a C compiler with the GNU binutils. For this article I will use the latest provided SBCL Windows 64 bits binary which is at version 1.4.14 at the time of this writing. For the C compiler and GNU binutils I will use the MSYS2 distribution. The advantage of this approach is that you will be able to move the resulting SBCL binary to any Windows machine.

The article consists of three parts:

►  Continue reading


Cross compiling OpenCV 4 for Raspberry Pi Zero

Posted on August 7, 2019 by Paul

In this article I will show you how to cross compile the latest version of OpenCV for Raspberry Pi Zero. If you want to build OpenCV for Raspberry Pi 2, 3 or 4 please check this article for a much faster procedure.

Unfortunately the crossbuild-essential-armhf from Debian will not generate working binaries for Raspberry Pi Zero, so we can’t use the cross compilation procedure I’ve presented in my previous article about cross compiling OpenCV for Raspberry 2 and up.

In order to cross compile OpenCV, we are going to use a slightly more involved procedure, in which we’ll basically emulate an ARM processor on a more powerful x86-64 Debian. The approach presented in this article will work with all versions of Raspberry Pi, but it is much slower than the approach presented in my previous article because of the emulation part. Please note that, even with an emulated ARM architecture, cross compiling OpenCV is much faster than building directly on a Raspberry Pi Zero where it could take days, assuming that the build doesn’t fail because of the limited amount of RAM on the Pi Zero.

►  Continue reading


Python - using C and C++ libraries with ctypes

Posted on July 18, 2019 by Paul

In this article, I will show you how to use C or C++ dynamic libraries from Python, by using the ctypes module from the Python standard library. ctypes is a foreign function library for Python that provides C compatible data types. Although it is mostly used to consume C and C++ libraries, you can use ctypes with libraries written in any language that can export a C compatible API, e.g. Fortran, Rust.

The advantage of using ctypes is that it is already included with your Python installation and that, in theory, you can call any C or C++ shared or dynamic libraries. Another advantage of using ctypes is that you don’t need to recompile the library in order to be able to use it from Python.

A disadvantage of ctypes is that you need to manually wrap the data and functions from the foreign library in Python code. Also for C++ libraries you will need to have the exported functions wrapped in an extern C block. If you need to use a heavy OOP C++ library from Python, I recommend that you look into pybind11.

You can find the source files and image examples used here on the GitHub repo for this article.

The article consists of four parts:

►  Continue reading


Install GNU Octave on macOS and getting started with the image processing package

Posted on July 11, 2019 by Paul

In this article, I will show you how to install and get started with GNU Octave on macOS. In order to test the installation and to exemplify the usage, I will show you how to use Octave for a simple image processing application.

At the time of this writing, the latest stable version of Octave is 5.1.0. Unfortunately the graphical interface of Octave 5.1.0 is buggy on macOS and tends to freeze randomly. If you are OK with using the CLI interface of Octave, you can use version 5.1.0. See the second part of this article for a quick how to install and setup.

Since most Octave users will prefer the GUI interface, I will first show you how to install Octave 4.4.1, which seems to be more stable than 5.1.0 on macOS when used in GUI mode.

I assume that you have the Command Line Tools installed on your machine. If this is not the case, write the next command in a Terminal and accept the defaults:

1 xcode-select install

The article consists of three parts:

►  Continue reading


Batch convert images to PDF with Python by using Pillow or img2pdf

Posted on June 12, 2019 by Paul

In this article I will show you how to batch convert a folder with images to a PDF file. This is a problem that I encountered recently when I had to process a bunch of scanned images and save the processed files as a single PDF.

While there are many libraries that could be used for the above, in this post I will consider only two libraries Pillow, formerly named PIL and img2pdf.

If you need to install the two libraries, you could use pip, e.g.:

1 python -m pip install Pillow
2 python -m pip install img2pdf

►  Continue reading


C Programming - Reading and writing images with the stb_image libraries

Posted on June 10, 2019 by Paul

In this article I will show you how to read and write images with the stb_image libraries. In order to exemplify the usage of the library I’ll demo how to convert an image to gray and how to apply a sepia filter to the image.

As a side note, the C code from this article is compatible with any modern C compilers like GCC, Clang and MSVC.

You can find the source files and image examples used here on the GitHub repo for this article.

The article consists of two parts:

stb_image basic usage

Let’s start by getting the libraries from GitHub:

1 git clone https://github.com/nothings/stb.git

if you don’t have git installed on your computer you can use the Download ZIP option.

►  Continue reading


Building the Windows Terminal application with Visual Studio 2019

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.

►  Continue reading


C++17 STL Parallel Algorithms - with GCC 9.1 and Intel TBB on Linux and macOS

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 8. You can find the latest release of TBB on their GitHub repo. If a newer version was released, change the version number from the next instructions accordingly.

►  Continue reading


Using Clang as a cross compiler for Raspberry Pi

Posted on May 4, 2019 by Paul

Updated 25 September 2019

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 9. 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.

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 9. You will also be able to run the binaries compiled with this Clang version on Raspberry Pi 2 and up. You can also use the same procedure if you plan to build the trunk version of Clang in order to test the latest additions to the compiler.

As a side note, you can install the official Clang binary directly on your Raspberry Pi, check this article if you don’t want to build from sources.

I recommend that you do the build in a Debian Buster virtual machine or a Docker container in order to not mess your system. If you decide to install Buster 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. Also, Clang uses a lot of memory when built from sources, make sure you have at least 8GB of RAM on your system.

►  Continue reading


C17 Programming - measuring execution time, delaying program execution

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:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 int main(void) {
 6     struct timespec t0, t1;
 7 
 8     if(timespec_get(&t0, TIME_UTC) != TIME_UTC) {
 9         printf("Error in calling timespec_get\n");
10         exit(EXIT_FAILURE);
11     }
12 
13     // Do some work ...
14 
15     if(timespec_get(&t1, TIME_UTC) != TIME_UTC) {
16         printf("Error in calling timespec_get\n");
17         exit(EXIT_FAILURE);
18     }
19 
20     // Calculate the elapsed time
21     double diff = (double)(t1.tv_sec - t0.tv_sec) + ((double)(t1.tv_nsec - t0.tv_nsec)/1000000000L);
22     printf("Elapsed time: %lf seconds\n", diff);
23 }

Where timspec will store the time split in seconds and nanoseconds.

►  Continue reading


C Programming - using ANSI escape codes on Windows, macOS and Linux terminals

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:

1 #include <stdio.h>
2 
3 int main(void) {
4     printf("\x1b[32mHello, World\n");
5 }

►  Continue reading


C Programming - read a file line by line with fgets and getline, implement a portable getline version

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. :

►  Continue reading


C Programming - Passing a multi-dimensional array to a function

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:

1 int arr[][3] = {
2     {1, 2, 3},
3     {4, 5, 6}	
4 };

►  Continue reading


Docker on macOS - Installation and Getting Started with GCC 8 in a Docker container

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.

►  Continue reading


C++17 - Implementing a singly linked list with smart pointers

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.

►  Continue reading