Ask Sawal

Discussion Forum
Notification Icon1
Write Answer Icon
Add Question Icon

How to use gsl in visual studio?

5 Answer(s) Available
Answer # 1 #

I am a Physicist, not a computer scientist, and struggle with a lot of the jargon in the GNU documentation.

For the last few hours I've been thoroughly confused on how to use GSL on a Windows 7 machine. I've tried reading a lot of questions on this site as well as others, but a lot of the jargon goes over my head.

Normally, when I write a program I do the following steps:

gcc -Wall -std=c99 -o myfile myfile.c

I made gcc an Environment Variable, and installed gcc by installing CodeBlocks for Windows (however, I don't use codeblocks).


5.I called the compiler as follows:

This resulted in the following error message:

Magera Abhimanyu
Answer # 2 #

Table of Contents

The GNU Scientific library (GSL) is a powerful C/C++ numerical library.

This guide assumes you already have a working C/C++ compiler - check by entering gcc --version in Terminal. OSX's default Clang compiler is fine, otherwise this page describes how to install gcc 4.9.

Apparently GSL can be installed through Homebrew via

though installing it manually is just as simple, which we now describe.

If the above gives a "permission denied" error, instead try

You'll now be able to include GSL into your code from anywhere.

You'll now be able to include GSL into your code from anywhere.

In the ARCUS terminal, download GSL anywhere

and unzip it

then open the new folder e.g.

To install without admin permissions, we'll keep the GSL files in a folder called gsl .

Note some newer versions off gcc (e.g. 8) seem incompatible. Use an older version (e.g. gcc/5.3.0) and first call

Replace YOURUSERNAME below with your ARCUS-B username (e.g. abcd1234) and run

We'll need to remember this location when compiling

Compiling GSL with our code is easy; we simply add library flags -lm -lgsl -lgslcblas to our calls to gcc.

For example, we'd adapt the QuEST makefile to include

Exactly as above for OSX.

Before compiling on ARCUS-B, we must extend LD_LIBRARY to include our gsl folder (replace YOURUSERNAME below)

This need only be done once each time we SSH into ARCUS.

Our compilation must additionally include the -lm -lgsl -lgslcblas libraries and flags -I/data/oums-quantopo/YOURUSERNAME/gsl/include and -L/data/oums-quantopo/YOURUSERNAME/gsl/lib.

For example, we'd modify the QuEST makefile to include

and just after that, insert

and modify the rules list

and finally the build commands

See a complete example of my QuEST & gsl makefile here.

The many different GSL functions are available in different source files.

For example, to use the matrix, linear equation solver, and matrix multiplication functionalities, respectively include

View available functions and their necessary header files can be found here.

Sometimes GSL is so powerful, it's confusing to figure out how to do some simple things. So here are some examples

Matrices are dynamic objects, indexed from 0, which must be allocated before, and freed after, their use.

For a vector, replace matrix above with vector.

GSL's matrix operations come under 'Basic Linear Algebra Subprograms' (BLAS), listed here

To multiply matrices matrA and matrB and store the result in matrC (all of which must be previously allocated by gsl_matrix_alloc), call:

In the name gsl_blas_dgemm, the d refers to double-precision matrices, and the final m refers to matrix. The other arguments are explained here.

To multiply a matrix matrA and a vector vecB and store the result in vecC (previously allocated by gsl_matrix_alloc and gsl_vector_alloc), call:

Linear algebra operations are provided by

Here's how to solve A x = b (or matrA vecX = vecB) for x by LU decomposition, which involves creating an intermediate permutation structure

By default, a numerical error (e.g. attempting to invert a non-invertible matrix) will cause an error message and execution to stop. To instead detect and handle numerical errors, include

Carl cpzha
Answer # 3 #

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.

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

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, 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:

If you want to learn more about C++17 I would recommend reading C++17 in Detail by Bartlomiej Filipek:

znhqcnx Rautela
Answer # 4 #

Thank you for your update.

As far as I know, we need to manually install this package through Nuget or install then configure it under each solution or project. If the solution includes several projects, you can right click the solution name and select ‘Manage Nuget Package for Solution…’ to install this package into many projects, it is also very convenient.

You can also redirect to this GSL support forum: and click ‘Issues‘ to start a new issue to seek for a more professional support, since our forum is to discuss the VS IDE, thank you for your understanding.

BTW, if you think my previous reply is helpful for your issue, you can mark it as answer and we will close this thread. Meanwhile, it will help other community members who meet the same issue to easier search a method to use the GSL in project, thanks in advance.

Have a nice day.

Best regards,

Shinobu Flippen
Ostomy Nursing
Answer # 5 #
  • Download gsl-latest.tar.gz from the GSL ftp site and unzip it anywhere (e.g. /Downloads)
  • Open the unzipped gsl folder in Terminal (e.g. cd ~/Downloads/gsl-2.4.
  • Run sudo ./configure && make && make install.
Farren Mortimer
Chief Genealogical Officer