## Installing and using the GNU Scientific Library (GSL) on the Windows Subsystem for Linux

January 23rd, 2019 | Categories: C/C++, Linux, programming | Tags:

I have been an advocate of the Windows Subsytem for Linux ever since it was released (See Bash on Windows: The scripting game just changed) since it allows me to use the best of Linux from my windows laptop.  I no longer dual boot on my personal machines and rarely need to use Linux VMs on them either thanks to this technology.  I still use full-blown Linux a lot of course but these days it tends to be only on servers and HPC systems.

I recently needed to compile and play with some code that was based on the GNU Scientific Library. Using the Ubuntu 18.04 version of the WSL this is very easy. Install the GSL with

sudo apt-get install libgsl-dev


A simple code that evaluates Dawson’s integral over a range of x values is shown below. Call this dawson.cpp

#include<iostream>
#include<vector>
#include<gsl/gsl_sf.h>

int main(){

double range = 6; // max/min values
int N = 100000; // Number of evaluations
double step = 2 * range / N;
std::vector<double> x(N);
std::vector<double> result(N);

for (int i=0;i<=N;i++){
x[i] = -range + i*step;
result[i] = gsl_sf_dawson(x[i]);
}

for (int i=0;i<=N;i++){
std::cout << x[i] << "," << result[i] << std::endl;
}

return 0;
}


Compile with

g++ -std=c++11 dawson.cpp -o ./dawson -lgsl -lgslcblas -lm


Run it and generate some results

./dawson > results.txt


If we plot results.txt we get

This code is also available on GitHub: https://github.com/mikecroucher/GSL_example

1. Hi Mike,
do you, like me, have trouble convincing one’s Linux loving colleagues to take WSL seriously?
Like you say in the article, it won’t replace all native Linux uses, but to save dual booting or running in a full VM it seems a great solution to me.
-Ian

2. Yes, I do. However, I find that people who mostly work on Windows embrace it fully once they’ve been shown the benefits.

3. The code posted does not compile. Missing plus signs. Note that I use macOS, a UNIX OS. The following modified works:

/*
Evaluate Dawson’s integral.
http://www.walkingrandomly.com/

Usage:

g++ -std=c++11 dawson.cpp -o ./dawson -lgsl -lgslcblas -lm -L/opt/local/lib
./dawson > results.txt
Terminal type is now ‘aqua’
gnuplot> plot “results.txt”
*/

#include
#include
#include

int main(){

double range = 6.0; // max/min values
int N = 100000; // Number of evaluations
double step = 2 * range / N;
std::vector x(N);
std::vector result(N);

for (int i=0;i<=N;i++){
x[i] = -range+i*step;
result[i] = gsl_sf_dawson(x[i]);
}

for (int i=0;i<=N;i++){
std::cout << x[i] << " " << result[i] << std::endl;
}

return 0;
}

4. Thanks John. I had some copy and paste problems in getting the code into WordPress. It ruined your example too by stripping out the includes! The tool I used to work around the include problem stripped out the + signs (who knows why!) and I didn’t notice so thanks for the heads-up.

I’ve fixed the code in the post and also posted a link to the much more reliable version on GitHub. https://github.com/mikecroucher/GSL_example

Cheers,
Mike

5. I haven’t used C++ in a long time, but shouldn’t your loop terminations be i < N
(or, as you know I prefer, i != N)?
cheers
-Ian

6. yes, you’re right