In this article, I will show you how to install and get started with the C++ interface of MathGL on Linux and Windows. MathGL is a high quality scientific graphics library.

In the first two parts of the article I will describe how to install the library and how to build a C++ MathGL example. In the third part of the article we’ll use MathGL to create a 2D function graphic and save it as an image file. As a second example, we’ll use the FLTK GUI library to show the same 2D function:

This article is split in a three parts:

I recommend that you read the install part for your OS and the part that shows how to use MathGL to create some graphics.

► Continue reading

In this article, I will show you how to install GSL - the GNU Scientific Library on Windows, macOS and Linux, and how to compile and run a simple GSL program. GSL is a C library for numerical computations. You can use GSL for example to solve a linear system of equations, to fit a curve to a set of points, for numerical integration, statistical calculations and so on. You can find a detailed description of GSL capabilities in the GSL reference manual.

This article is split in a few parts:

I recommend that you read the install part for your OS and the part that shows how to use GSL to solve a linear system of equations.

► Continue reading

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

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:

► Continue reading

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

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 << '

' ; 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

Happy Halloween, from C++:

1 // halloween.cpp 2 #include <iostream> 3 4 const auto dummy1 = [] { 5 std :: cout << "Hold my beer!

" ; 6 return 1 ; 7 }(); 8 9 const auto dummy2 = [] { 10 std :: cout << "Hold my second beer!

" ; 11 return 2 ; 12 }(); 13 14 15 int main () { 16 std :: cout << "C++: Happy Halloween, your program starts here!

" ; 17 }

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

► Continue reading

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

Updated 11 February 2020

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

Start by installing the Command Line Tools for macOS. Please note, that you will need the Command Line Tools even if you’ve already installed Xcode. Open a Terminal and write:

1 xcode-select --install

Once the Command Line Tools are installed, we can install Python.

► Continue reading

Updated 8 May 2020

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 10 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

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

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

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

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

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