Solarian Programmer

My programming ramblings

Compiling GCC 6 on OS X

Posted on May 10, 2016 by Sol

UPDATE 22 September 2016

If you are using macOS Sierra check the new tutorial.

In this tutorial, I will show you how to compile from source and install the current stable version of GCC with Graphite loop optimizations on your OS X computer. The instructions from this tutorial were tested with Xcode 7.3 and El Capitan (OS X 10.11).

Clang, the default compiler for OS X, supports only C, 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 Mac is that it provides you with an alternative C and C++ compiler. Testing your code with two different compilers is always a good idea.

►  Continue reading

How to upgrade Node.js on Mac OS X

Posted on April 29, 2016 by Sol

If you already have Node.js installed on your Mac you will need to upgrade it when a new version comes up. There are basically two ways to achieve this goal download the installer from Node.js or use the command line to upgrade Node.js.

A simple way to upgrade Node.js from the Terminal is to use the n version manager:

►  Continue reading

Compiling Boost with GCC 5 or Clang on Mac OS X

Posted on March 6, 2016 by Sol

In this article I will show you how to build the Boost libraries under Mac OS X with GCC 5 or Clang. Once the libraries are installed, we’ll test the build with a short demo of using Boost Filesystem.

The next C++ standard, C++17, will probably include a much needed Filesystem specification based on Boost Filesystem. Some compilers like Visual Studio 2015 already include the filesystem header as an experimental feature. In the meantime, if your preferred C++ compiler doesn’t include filesystem, you can use the Boost implementation and update your code later.

►  Continue reading

OpenCV video editing tutorial

Posted on June 4, 2015 by Sol

The code for this post is on GitHub:

I’ve always knew that OpenCV can be used to do some video editing, however when I’ve actually tried to use it to open a video file, a few weeks ago, I was amazed at the quantity of misleading or incomplete tutorials you find on the web. I wrote this tutorial to save some time for others like me that need to do some quick and dirty video editing with C++ and OpenCV.

Simplest thing that you could try is to read frames from your computer’s webcam. OpenCV uses the same function, VideoCapture, for opening a video file or a camera attached to your computer, the only difference is that for a camera you will feed the function a number, while for an actual video file you will use the video path.

►  Continue reading

OpenMP on OS X

Posted on May 14, 2015 by Sol

At the time of this writing Clang, the default C++ compiler on OS X, doesn’t have an OpenMP implementation. GCC however supports OpenMP 4, but you need to install it yourself if you want to use it. GCC 5 can be built from sources for OS X if you want to have the latest and greatest or, you can install it with Homebrew.

OpenMP ads threading support to a C or C++ code through pragmas, this has the advantage that you can take a serial code and parallelize it with minimal code modifications.

As a side node, starting with C++11, you can directly use the thread header if you want to explicitly parallelize your code and this approach is supported by Clang.

►  Continue reading

Detect red circles in an image using OpenCV

Posted on May 8, 2015 by Sol

The code for this post is on GitHub:

A few days ago someone asked me, in an email, if it is possible to detect all red circles in an image that contains circles and rectangles of various colors. I thought this problem could be of certain interest to the readers of this blog, hence the present article.

From the many possible approaches to the problem of red circles detection, two seem straightforward:

  • Detect all circles from the input image and keep only the ones that are filled with red.
  • Threshold the input image in order to keep only the red pixels, search for circles in the result.

I found the second approach to be slightly better than the first one (less false positives), so I am going to present it in this post.

►  Continue reading

Raspberry Pi - Building SDL 2 on Raspbian

Posted on January 22, 2015 by Sol

This is a short tutorial on how to get started with SDL 2 programming on a Raspberry Pi device that has Raspbian installed. If you want to write C++, or C, games that are easily portable to other platforms SDL 2 is the way to go on Raspbian. Unfortunately, at the time of this writing Raspbian comes with the outdated SDL 1.2 installed.

Let’s start by updating our Raspbian installation, feel free to skip this step if your system was recently updated:

1 sudo apt-get update
2 sudo apt-get upgrade

►  Continue reading

Getting started with JOGL (OpenGL bindings for Java) in Eclipse

Posted on December 8, 2014 by Sol

Recently, a reader of my OpenGL 101 series emailed me about how to get started with OpenGL in Java. More specifically, he was interested in JOGL the Java bindings for OpenGL. I thought his question was general enough to write a small post about creating a Java OpenGL getting started project.

JOGL is a good fit for a Java programmer that wants to learn OpenGL, it is particularly useful if you follow some OpenGL intro book or article. If you are more interested in writing Java games, you should check a more game friendly library like LWJGL or, even better, libGDX.

►  Continue reading

C++14 lambda tutorial

Posted on August 28, 2014 by Sol

The last iteration of C++, C++14 was approved this month. C++14 brings a few much anticipated changes to the C++11 standard, like allowing auto to be used as the return type of a function, or generic lambdas - the subject of this article.

Lambdas, in C++, were introduced by the C++11 standard. They were meant to let the coder write short, anonymous functions, that could be used instead of a function object, avoiding the need to create a separate class and a function definition. Here is a typical example of C++11 lambda usage, that returns the square of a number:

1 int result = [](int input) { return input * input; }(10);
2 cout << result << endl;

If you need to reuse the same piece of code in more than one place, you can save the function in a variable:

1 auto func = [](int input) { return input * input; };
3 // first use
4 std::cout << func(10) << std::endl;
6 // second use
7 std::cout << func(23) << std::endl;

►  Continue reading

C++14 auto tutorial

Posted on August 21, 2014 by Sol

The C++14 standard was recently approved. Without bringing big changes to the language, like the previous standard did, the C++14 standard aims to make the programmer’s job easier by improving the C++11 standard.

C++11 introduced the auto keyword (technically auto was present even in the C++03 standard, however C++11 changed the meaning of auto), auto was meant to make the code cleaner and less error prone, by letting you write for example:

1     auto i = 1;
2     auto c = return_a_complex_number();

instead of:

1     int i = 1;
2     std::complex<double> c = return_a_complex_number();

►  Continue reading

Install a 3rd party free SSL certificate on your LAMP server

Posted on August 19, 2014 by Sol

Last week, due to the recent Google announcement that they intend to favor websites that use SSL encryption, I’ve decided to install a SSL certificate on my blog. After a few hours of reading various articles about how you to install a SSL certificate on a Linux VPS, and another hour or two fiddling with my server settings, I’ve had all my traffic redirected to the https version of my blog.

Everything seemed to be in order, Chrome showed me a nice, green, https address:

https green OK

and I was ready to declare a major success in my short career as a server admin … until I’ve opened my website in Firefox and the world become gray and ugly:

https gray not OK

►  Continue reading

Remote text editing with SublimeText and TextMate

Posted on August 16, 2014 by Sol

A common problem for most server administrators and web programmers is how to edit text files quickly and efficiently on the server. For the Emacs and Vim gurus this is not really a problem, they’ll just install some plugin and do their work. What about the other 90% of the world, the mere mortals, that don’t know how to use one of the above editors ?

Some people will use a ftp client, like FileZilla, edit their files locally, with their preferred text editor, and upload the result to the server. Others, will chose to edit directly from a ftp client using the default text editor of their main OS (which on Windows, the horror …, is Notepad or on OSX is TextEdit). The savvy user will set his ftp client to use a more approachable, and usable, text editor like SublimeText, NotePad++, gEdit etc … The problem with using a ftp client to locally edit some file is that it is slow and somehow uncomfortable, at least for me.

In the next part of this post, I will assume that you have administrator rights on your web server and that this server runs on a different machine than your main computer. Another implicit assumption is that you want to use a text editor like SublimeText 3, which runs on all major operating systems, or TextMate 2 which runs only on OSX.

►  Continue reading

Increase your productivity with the Pomodoro technique

Posted on August 14, 2014 by Sol

A few years have past since I’ve started to work from home, during this time I’ve had my up and downs in productivity. In the beginning, there were more downs than ups, it was simply too tempting to lose an entire day reading various blogs, and articles and since nobody was there to ask me how I spent my day it was easy to lose track of time. Despite the popular beliefs, good and bad habits are built in similar amounts of time.

After a few weeks of working hard to achieve my Master Procrastinator diploma, I’ve realized that I was basically stilling my own time and slowly burning through my savings. It become clear to me that this path will end with me, again, working for someone else instead of working for me and on my own terms. Did I mentioned that I hate working for someone else ?

I always knew that in order to finish something you need to work on it every day, for a few hours, until it is done. The key here is to be completely focused on the task and to not exhaust yourself. I was always good at working full time, from 8 AM to 10 PM, for a few days. The problem with this approach was that after a few days of intense work I was burn out for the same number of days, or even more. When I feel dried out I tend to procrastinate by reading random stuff on forums like Hacker News or Reddit. I don’t remember exactly the context, but in one of my recovering sessions I read about the Pomodoro method as a way of slowly building discipline and improving focus by following a simple recipe:

►  Continue reading

Getting started with OpenCV on the BeagleBone Black with Ubuntu 14.04 LTS

Posted on April 21, 2014 by Sol

I’ve recently bought a BeagleBone Black for some computer vision and home automation projects I intend to do sometime in the future. BeagleBone has Ångström Linux already installed on the 2GB eMMC card. I have nothing against Ångström, but I prefer to run Ubuntu headless on this tiny computer. Also, Ångström has g++-4.7.2 as the default C++ compiler and I want to be able to use a newer compiler, like the g++-4.8.2, for C++11 development.

As a side note, the next revision of the BeagleBone Black will support Debian Wheezy out of the box, which has an even older C++ compiler than Ångström.

If you want to be able to run Ubuntu and build OpenCV on the BeagleBone Black, you will need a micro SD card of at least 8GB, I recommend buying one of class 10. Personally, I bought a SanDisk micro SD card of 16 GB.

►  Continue reading

Modular JavaScript development with Browserify and LivePage

Posted on February 5, 2014 by Sol

Working on a medium to large scale JavaScript project was always a challenge. Just picture yourself editing a few thousands lines of code in a single source file! Fortunately, the next version of the JavaScript standard, ECMAscript 6, is going to give us modules. Personally, I think that the golden standard for JavaScript modules is the Node.js model, what could be more elegant than to write:

1 require('foo');

and instantly have access to everything was exported in the required module ?

In the past, a possible solution to the above problem was to use an AJAX call to load a piece of JavaScript code in another file, e.g. jQuery.getScript(). More recently, like a few years ago, RequireJS become the state of the art approach. With RequireJS you would write:

1 require(["foo"], function(foo) {
2 	//This is called when foo.js is loaded.
3 }

this gives us a callback function that will fire once the required module is loaded. But, what if you want something even simpler, something that looks more like the Node.js syntax, or Lua’s require, or other languages ? Enter Browserify!

►  Continue reading