Solarian Programmer

My programming ramblings

Getting started with Swift 3 on Windows Subsystem for Linux

Posted on April 19, 2017 by Sol

Recently, I’ve played a bit with Swift 3 on WSL (Windows Subsystem for Linux). In this article, I will show you how to install the latest stable version of Swift under WSL. Combining the open source Swift compiler with a text editor like Visual Studio Code is quite a pleasant coding experience, as you can see in the next screenshot:

Editing and running Swift code in Visual Studio Code

►  Continue reading

Using the Windows Subsystem for Linux with Xfce 4

Posted on April 16, 2017 by Sol

In my previous article, I’ve shown you how to install WSL, the Windows System for Linux, on a fresh Windows 10 Creators Update. Officially, at the time of this writing, WSL supports only command-line applications. However, you can install graphical Linux applications and run these under WSL if you have an X server like VcXsrv or Xming, on your Windows machine.

Assuming that you have a working WSL installation, start Bash on Ubuntu on Windows, or open a Command Prompt and type bash in it. First step is to install a window manager. From my experience, Xfce works reasonably well with WSL:

1 sudo apt install xfce4

►  Continue reading

Install WSL - Windows Subsystem for Linux

Posted on April 15, 2017 by Sol

With the recent release of Windows 10 Creators Update, I’ve decided that I want to test the Windows Subsystem for Linux, WSL, aka Bash on Ubuntu on Windows. WSL is a way to run unmodified Ubuntu binaries on Windows natively, using the Windows kernel instead of the Linux kernel. The advantage of WSL is that you can run Linux command-line applications without the extra weight of a virtual machine. At this time, WSL on Windows 10 Creators Update uses binaries from Ubuntu 16.04.2.

If you want to run WSL on your Windows 10 installation, make sure that you have a 64 bits machine with a 64 bits Windows. If this is the case, check if your system is updated. Ideally, you will have at least the Creators Update which is version 1703. You can check the above by going in Settings → System → About:

►  Continue reading

Transferring data between Matlab and Armadillo

Posted on April 12, 2017 by Sol

In this article, I will show you how to move data between Matlab and Armadillo, specifically how to move matrices between the two. If you need an introduction in how to install Armadillo on your system, check my previous article.

Easiest approach is to transfer data between Matlab and Armadillo using text files. For example, the next Matlab code will generate some data and save it in a text file that can be loaded from Armadillo:

1 A = rand(4,5);
3 % Save the matrix A to "matlab_data.txt" the content of the file is overwritten
4 dlmwrite('matlab_data.txt', A, 'delimiter', '\t', 'precision', 16);

Reading the matrix from matlab_data.txt in Armadillo can be done with:

1 arma::Mat<double> A;
2 A.load("matlab_data.txt");

►  Continue reading

Getting started with Armadillo a C++ Linear Algebra Library on Windows, Mac and Linux

Posted on March 24, 2017 by Sol

In this article, I will give you a quick introduction in how to get started with Armadillo, a C++ Matlab like Linear Algebra Library on Windows, Mac and Linux. Armadillo is particularly interesting for Matlab users that want to port their code to C++ for speed. Please note that Armadillo is not a drop in replacement for all Matlab functionality. If your code uses some specialized Matlab toolbox, you will need to find another library to complement Armadillo or implement the missing functionality from scratch.

Armadillo uses BLAS and LAPACK for matrix operations. It is recommended that you install an optimized version of the above libraries on your system and not the reference implementations from Netlib. I will guide you on how to install accelerated versions of BLAS and LAPACK for your operating system, in the install sections of this article.

►  Continue reading

Install NumPy, SciPy, Matplotlib with Python 3 on Windows

Posted on February 25, 2017 by Sol

This is a short tutorial about installing Python 3 with NumPy, SciPy and Matplotlib on Windows.

We’ll start by installing the latest stable version of Python 3, which at the time of this writing is 3.6. Head over to and download the installer. The default Python Windows installer is 32 bits and this is what I will use in this article. If you need the 64 bits version of Python, check the Looking for a specific release? section from the above page.

►  Continue reading

iOS Remote Push Notifications with a Node.js backend

Posted on February 14, 2017 by Sol

In this tutorial I will show you how to get started with Swift 3 and iOS 10 push notifications, using your own custom Node.js server. Please note that there are plenty of alternatives to the custom server solution I will present here, like Firebase and similar services, these will start charging you a monthly fee after you reach a certain number of users. Using your own server is advantageous if you want complete control over what software you run on it and how you store and use your data.

If you want to follow this tutorial you will need to be a paying member of the Apple Developer Program and have an iOS device. It is also recommended, but not required, that you have a working web server with root access. Personally I tend to use DigitalOcean, but you can use any VPS provider. For the purpose of this article, I will show you how to use a local web server.

►  Continue reading

Arduino buzzer player with adjustable volume - playing a part of Für Elise by Ludwing van Beethoven

Posted on January 31, 2017 by Sol

The code for this post is on GitHub:

In this article, I will show you how to play music with the Arduino, using a passive piezo buzzer. You can see the final result in the next video:

►  Continue reading

Beginning C Game Development with SDL 2 - video series

Posted on January 22, 2017 by Sol

This is an ongoing video series about beginning C game development with SDL 2 by our partner AtoZ Programming Tutorials. While the videos where recorded on Windows 10, everything should work the same on macOS and Linux as long as you use SDL 2 and a C99 compiler like GCC, Clang or Visual Studio 2015/2017.

Currently, the series presents how to setup SDL 2 on Windows using Visual Studio 2015 and Code::Blocks. Let us know in the comments section if you would like to have SDL 2 setup videos for macOS and Linux.

Setup SDL 2 with Visual Studio 2015

►  Continue reading

Arduino - pulse an LED with a cosine wave function

Posted on January 21, 2017 by Sol

In this short article, I will show you how to pulse an LED with Arduino using a cosine function. You can see the final result in the next short video:

►  Continue reading

C99/C11 dynamic array that mimics C++'s std::vector - API improvements

Posted on January 8, 2017 by Sol

In my last article I wrote a portable C dynamic array, auto-resizable and theoretically capable to store any C representable data type. While the original implementation works, it is a bit cumbersome to use in practice. A better approach is to create a dynamic array that looks like a normal C array, but it is capable to automatically adjust its size as needed. I took from Sean Barrett’s stb libraries the idea that you can store the size information of an array at the beginning of the array, while publicly exposing a pointer to where the array data resides in memory. The advantage of this approach is that from a user point of view a dynamic array can be used like a C array, except for the creation and destruction phases.

It goes without saying that this article is written for C programmers or people that use a C compiler. If you are using C++, you usually don’t need to implement a vector type, you already have one in the STL, use it.

It will be easier to understand how the general case works if we start with an explicit implementation for a dynamic array of integers. Conceptually, our code has three phases

 1 #include <stdio.h>
 2 #include <stdlib.h>
 4 int main() {
 5     // 1. Create and initialize the array
 6     // ...
 8     // 2. Fill the array with data and do something with this data
 9     // ...
11     // 3. Destroy the array, freeing the memory
12     // ...
14     return 0;
15 }

►  Continue reading

C99/C11 dynamic array that mimics C++'s std::vector

Posted on January 6, 2017 by Sol

Recently, I’ve worked on a large C library used to store items from a database. The idea was to get all records that matched a particular criteria and store these in a C data structure for further processing. The original author of the library used a linked list to store the records which probably was a sensible idea 20 years ago. However, today’s computers perform better if you use a cache friendly data structure like an array. Basically, I wanted a dynamic array that automatically increases his size if needed, something like C++'s std::vector.

UPDATE 8 January 2017
Part 2 of this article presents a better, more flexible, implementation for a C dynamic array. Feel free to skip the remaining of this article and read the second part.

►  Continue reading

Install OpenCV 3 with Python 3 on macOS Sierra

Posted on November 29, 2016 by Sol

In this article, I will show you how to install OpenCV 3 with Python 3 on macOS Sierra. Most tutorials I found online, including the OpenCV documentation, seem concerned only with Python 2.7.

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 latest stable Python 3 release, Python 3.5.2.

►  Continue reading

C++ - Passing a C-style array with size information to a function

Posted on November 28, 2016 by Sol

When you pass a C-style array to a function it will decay to a pointer to the first element of the array, basically losing the size information.

As a side note, in a pure C++ code, one will prefer to use std::vector or std::array instead of C-style arrays. However, there are cases like in embedded systems in which using the Standard Template Library is forbidden by your company guide rules or simply not available on the platform. This is why I think the approach presented in this article could be of some interest to C++ programmers.

In C, you will need to pass the size of the array as an extra parameter to the function or use a global variable. A potential problem with this approach is that it is easy to screw up by forgetting the array to pointer decay rule or by passing the wrong size information.

Here is an example of a classical bug when one forgets about the array to pointer decay rule:

 1 void func(int *my_array) {
 2 	// Wrong, my_array_size won't be equal with the actual number of the elements from the original array
 3 	size_t my_array_size = sizeof(my_array)/sizeof(my_array[0]);
 5 	// ...
 7 }
 9 int main() {
10 	// C-style array
11 	int my_array[5] = {1, 2, 3, 4, 5};
13 	// Good, here my_array_size will be 5!
14 	size_t my_array_size = sizeof(my_array)/sizeof(my_array[0]);
15 	return 0;
16 }

►  Continue reading

Swift 3 and OpenGL on Linux and macOS with GLFW

Posted on November 19, 2016 by Sol

This is a short article about how to get started with Swift 3 and OpenGL on Linux and macOS. In order to keep the code portable on both operating systems, we’ll use GLFW to create a window with an OpenGL context. Keep in mind that, at time of this writing, Swift can call directly any C library. However, if you need to use a C++ library you will need to write a C wrapper around it, because Swift 3 is not interoperable with C++.

As a practical example, we’ll use OpenGL to draw a yellow rectangle on a red background. In order to keep the code short and as simple as possible, we’ll use the fixed OpenGL functionality. If you want to use modern OpenGL, please check my other OpenGL articles. Replacing the fixed OpenGL functionality from the article example is left as an exercise for the reader. Basically, once you know how to call OpenGL functions from Swift and compile the code, it is just a question of writing supporting code for dealing with shaders, buffers and so on.

►  Continue reading