Solarian Programmer

My programming ramblings

Install Code::Blocks and GCC 9 on Windows - Build C, C++ and Fortran programs

Posted on November 16, 2019 by Paul

In this article I will show you how to install the Code::Blocks IDE on Windows and how to configure it to use GCC 9 for building C, C++ and Fortran programs. The advantage of this setup is that you will be able to compile any standard C99, C11, C++11, C++14, C++17 and Fortran 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 (5.1.0). I will show you how to use the latest version of GCC, which is 9.2 at the time of this writing, with the Code::Blocks IDE.

I recommend that you start by installing the latest version of GCC, by following my previous article, in which I’ve shown how to install GCC 9.2 with the MSYS2 software distribution. Once you have GCC installed, you can proceed with installing Code::Blocks.

There is also a video version of this tutorial:

The article consists of five parts:

►  Continue reading


C++ Implementing a Chaos Game simulator

Posted on November 13, 2019 by Paul

In this article, I will show how to implement in C++ the classical Chaos Game. We start with a regular polygon and a random initial point inside the polygon. Next, we randomly select one of the polygon vertices and add a new point at a fraction of the distance between the initial point and the polygon vertex. We take as initial point the newly generated point and iterate for a large number of steps. Following the above process can, in some cases, produce a fractal shape.

In the next image you can see various fractal shapes generated with the program we are going to write in this post:

Various images generated with the simulator

►  Continue reading


Install GCC 9 on Windows - Build C, C++ and Fortran programs

Posted on November 5, 2019 by Paul

In this article, I will show you how the install the GCC compilers on your Windows system using the MSYS2 software distribution.

The advantage of this setup is that you will be able to build C, C++ and Fortran programs on your Windows machine and generate native Windows executables. Also, MSYS2 tends to offer the latest stable versions of GCC, which at the time of this writing is 9.2.

There is also a video version of this tutorial:

Another advantage of using the MSYS2 software distribution is that you will be able to install, if necessary, other C and C++ libraries like SDL2 or OpenCV using the pacman package manager which is included with MSYS2.

Please note that all graphical libraries provided by MSYS2 use the native Win32 API. This means that when you build for example an OpenGL application it will use a native Windows Win32 window.

►  Continue reading


C++20 span tutorial

Posted on November 3, 2019 by Paul

According to the latest C++20 draft, a span is a non-owning view over a contiguous sequence of objects. In other words, a std::span is, in essence, a pointer, length pair that gives the user a view into a contiguous sequence of elements. The elements of a span can be, for example, stored in one of the standard library sequential containers (like std::array, std::vector), in a built-in C-style array or in a memory buffer.

Here is a simple example of using std::span as a general interface for a print function that receives as argument a contiguous sequence of integers:

 1 // span_0.cpp
 2 #include <iostream>
 3 #include <vector>
 4 #include <array>
 5 #include <span>
 6 
 7 void print_content(std::span<int> container) {
 8     for(const auto &e : container) {
 9         std::cout << e << ' ';
10     }
11     std::cout << '\n';
12 }
13 
14 int main() {
15     int a[]{23, 45, 67, 89};
16     print_content(a);
17 
18     std::vector v{1, 2, 3, 4, 5};
19     print_content(v);
20 
21     std::array a2{-14, 55, 24, 67};
22     print_content(a2);
23 }

►  Continue reading


A Happy Halloween from C++ and an IIFE lambda

Posted on October 31, 2019 by Paul

Happy Halloween, from C++:

 1 // halloween.cpp
 2 #include <iostream>
 3 
 4 const auto dummy1 = [] {
 5     std::cout << "Hold my beer!\n";
 6     return 1;
 7 }();
 8 
 9 const auto dummy2 = [] {
10     std::cout << "Hold my second beer!\n";
11     return 2;
12 }();
13 
14 
15 int main() {
16     std::cout << "C++: Happy Halloween, your program starts here!\n";
17 }

Using an IIFE, or an “Immediately invoked function expression”, lambda to initialize a global variable can have surprising side effects:

►  Continue reading


C++17 - find the greatest common divisor, gcd, of two or more integers

Posted on October 25, 2019 by Paul

In this article, I will show you how to find the gcd - greatest common divisor of two or more integers with C++, by using two implementations of the classical Euclid algorithm. As a side note, the C++ standard library has a std::gcd function that can calculate the gcd of two numbers and I will also show you how to use the STL version. Implementing an algorithm from scratch is the best way to understand it, even though in practice one should use as much as possible the solution already provided by the language standard library.

There is also a video version of this tutorial:

By definition, the gcd of two or more integers, that are not all zero, is the largest positive integer that divides all these numbers. For example:

gcd(9, 15) = 3
gcd(9, 15, 21) = 3
gcd(0, 10) = 10

It is also useful to define:

gcd(0, 0) = 0.

►  Continue reading


Install OpenCV 4 with Python 3 on macOS Catalina

Posted on October 21, 2019 by Paul

In this article, I will show you how to install OpenCV 4 with Python 3 on macOS Catalina.

There is also a video version of this tutorial:

MacOS comes by default with Python 2.7 which, at this point, receives only bug fixes and will be EOL by 2020. Python 3.x is the future and it is supported by all major Python libraries. In this tutorial, we’ll use the Python 3.7.

While Python 3.8.0 was released a few days ago, not all libraries where ported or compiled for the latest Python release. I will update the article when the situation will improve. For now, make sure that you are using the 64 bits installer of Python 3.7 from https://www.python.org/.

►  Continue reading


Compiling GCC 9 on macOS Catalina

Posted on October 12, 2019 by Paul

In this tutorial, I will show you how to compile from source and install the current stable version of GCC on your macOS computer. The instructions from this tutorial were tested on Catalina (macOS 10.15).

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.

Building GCC 9 from sources could take some time, in my case it took about two hours on a MacBook Air with a 16GB of RAM.

►  Continue reading


Install OpenCV 4 on Raspberry Pi for C++ and Python development

Posted on September 17, 2019 by Paul

In this article, I will show you how to install OpenCV 4 with Python and C++ support on Raspberry Pi. I assume that you have the latest Raspbian installed on your Raspberry Pi, which at the time of this writing is based on Debian 10 Buster.

Unfortunately, there is no official binary of OpenCV 4 for Raspberry Pi, so I had to built OpenCV with Python 2 and 3 support for Raspberry Pi Zero and up from sources. If you want to do the build yourself check my previous articles:

In this article I’ll will show you how to install the already built binaries.

There is also a video version of this tutorial:

►  Continue reading


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