# Getting started with GSL - GNU Scientific Library on Windows, macOS and Linux

## Posted on January 26, 2020 by Paul

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.

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.

Install GSL on Windows:

For Windows, I recommend that you first install Visual Studio 2019. Make sure that you install the proper Visual Studio and not Visual Studio Code which is an entirely different product. Once the Visual Studio installer starts, check the Desktop development with C++ workload:

Next, install git for Windows from https://git-scm.com/. If you are not familiar with git, just accept the defaults suggested by the installer.

Finally, we are going to install vcpkg which is a C++ library manager that will let us install GSL. I will install vcpkg in a folder on my C drive, feel free to install it in a different location, just avoid paths with empty spaces. Open a PowerShell window (PowerShell is already installed on Windows, you can find it in the Start menu):

Next, we are going to use git to get the latest vcpkg version:

After the above command finishes, go to the vcpkg folder, bootstrap and integrate vcpkg:

At this point, you can use the vcpkg command to install any of the available C++ libraries. You can find the complete list of available libraries in the ports folder from the vcpkg installation. Once you use vcpkg to install a library, it will be available in your Visual Studio C++ projects.

Let’s install GSL with vcpkg:

The above command will build and install GSL for 32 and 64 bits Visual Studio projects.

Next time when you want to install a C++ library that is available in the vcpkg ports, remember to open a PowerShell window and navigate to the where you’ve installed vcpkg. vcpkg is not added to the system path, so you need to be in the vcpkg folder in order to be able to use it.

As a side note, a library installed with vcpkg is directly available in your msbuild projects (the ones created by default by Visual Studio). If you want to create a CMake project, you will need to add the include folders and libraries to the CMake project.

Now, that you have GSL installed, start Visual Studio and create a new C++ console project. You can remove the hello world C++ code that is generated by the compiler and write your C++ GSL program, see the GSL usage part of the article for an example.

Install GSL on macOS:

On macOS, I recommend that you install the Command Line Tools which will give you the Clang C++ compiler and also installs a bunch of useful utilities like make, git, vim and so on. Open a Terminal and write:

just follow the installer suggestions to get the Command Line Tools.

Once the Command Line Tools are installed, we need to install the Homebrew package manager which will let us install GSL:

At this point, you should be able to use the brew command to install GSL and CMake:

Now, that you have GSL installed, open your preferred C++ editor (if you don’t have one, a good suggestion is Visual Studio Code) and write a C++ GSL program, see the GSL usage part of the article for an example.

Install GSL on Linux:

For Linux, I will exemplify the installation part on a Debian based system.

Let’s start by installing a C++ toolchain. Open a Terminal and write:

Next, install GSL with:

By default GSL will use a GSL built version of BLAS which is a library for basic vector and matrix operations. If you have access to a a highly tuned BLAS version, like the one provided with MKL, I suggest to use it. Alternatively, you can install libatlas, which is not as fast a MKL, but it is usually faster than the BLAS version that is include with GSL. You can install libatlas with:

Now, that you have GSL installed, open your preferred C++ editor (if don’t have one, a good suggestion is Visual Studio Code) and write a C++ GSL program, see the GSL usage part of the article for an example.

GSL usage example - solving a linear system of equations:

In this part of the article, I will show you how to use GSL to solve a linear system of equation:

$A \cdot x = b$

Let’s start by including the necessary header files and filling the above A matrix and b vector with some values. For simplicity, we are going to use some random hard coded values. I will leave it as an exercise for the reader to discover how to use GSL to fill A and b with random numbers:

Next, we are going to use GSL views to access the data from the above two arrays:

You can, optionally, print the vector and matrix values using gsl_vector_fprintf and gsl_matrix_fprintf, e.g.:

Please note, that a matrix is printed in the order that is stored in memory, row by row.

In order to solve the above system with GSL we first need to decompose the matrix A into the LU form and pass the decomposed matrix to the GSL linear solver:

Please note that we allocate memory for the solution vector x and a permutation of the rows of the matrix A during the LU decomposition. Since GSL is a C library, we need to release the allocated memory manually when it is not needed. Another observation is that the original values of the matrix A are destroyed during the decomposition and replaced with the values of the decomposition.

Here is entire program:

This is how you can compile the above program on a Linux machine:

and on a macOS machine:

Here is an example of what I see on a Linux machine when I build and run the above program:

For Windows, create a new C++ console application and replace the C++ code generated by Visual Studio with the above C++ program. Since we’ve installed GSL with vcpkg, Visual Studio knows where to find the include files and with what library to link the project, all you have to do is build and run the program.

Here is an example of what I see on a Windows machine:

Create a C++ GSL CMake based project:

In the last part of the article, I will show you how to create a portable C++ and GSL CMake based project that can be used on Windows, macOS and Linux. This is useful if you are interested in running the same project on multiple operating systems.

I will assume that you have a folder named gsl_tests with a C++ program named gsl_test.cpp that contains the code presented in the previous section. In the same folder create a new file named CMakeLists.txt with the next content:

For Linux and macOS, open a Terminal and navigate to the gsl_tests folder:

You should end up with an executable named gsl_demo in the build folder.

For Windows, go to the Start menu, find the Visual Studio 2019 menu and from it start x64 Native Tools Command Prompt for VS 2019. From the above Command Prompt navigate to where you’ve placed the gsl_tests folder. Here, I will assume for simplicity, that you have the folder gsl_tests in C:\DEV and that your vcpkg installation is also in C:\DEV:

At this point, you can build the project from the same Command Prompt with:

and you should end up with an executable named gsl_demo.exe inside the Release folder.

Alternatively, open C:\DEV\gsl_tests\build in Windows Explorer, right click on gsl_demo.sln and chose to open it with Visual Studio 2019. You should see a list of projects, right click on the one named gsl_demo and make it the default one than build it. At this point, you can run the code as usual with CTRL + F5, F5 or from the Debug menu.

As a side note, latest Visual Studio installer should install cmake for you. If you get an error when trying to use cmake with the above instructions, first check that you are using a proper Visual Studio command prompt. If you still can’t find cmake, you can install it from https://cmake.org/, just be sure to add it to the Windows path.

If you need to learn about Linear Algebra, I would recommend reading Linear Algebra and Learning from Data by Gilbert Strang: