Using GSL on Windows (compiling, linking, ect). A Step by Step Guide

Rohan picture Rohan · Nov 3, 2014 · Viewed 9.9k times · Source

I need to write some C code using GSL (GNU Scientific Library), and have never used libraries before.

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:

1)Open notepad++, include any of my own header files and write my code.

2)Compile my program by opening the Windows Command prompt and typing:

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).

From Research on the internet this is what I've managed to do to use GSL:

1)Downloaded GSL for Windows from: http://gnuwin32.sourceforge.net/packages/gsl.htm

2)Installed it on the Desktop, so that my Path will have no space characters (I've read spaces in the Path can be a problem).

3)Set my Path Environment Variable to the location of the .dll's: C:\Users\Rohan\Desktop\GnuWin32\bin;

4) Attempted to compile the GSL example program:

#include <stdio.h>
#include <gsl/gsl_sf_bessel.h>

int main (void)
{
  double x = 5.0;
  double y = gsl_sf_bessel_J0 (x);
  printf ("J0(%g) = %.18e\n", x, y);
  return 0;
}

5) I called the compiler as follows:

C:\Users\Rohan\Desktop>gcc -Wall -I"C:\Users\Rohan\Desktop\GnuWin32\include" -L"C:\Users\Rohan\Desktop\GnuWin32\lib" -lgslcblas -lgsl -lm -o test test.c

This resulted in the following error message:

C:\Users\Rohan\AppData\Local\Temp\ccW8cO7I.o:test.c:(.text+0x30): undefined reference to `gsl_sf_bessel_J0'
collect2: ld returned 1 exit status

Most of this was done with little understanding, due to not having a background in CS.

Am I even on the right track? Is there an easy step by step guide, in non-technical language I can follow to get this working?

Any help would be greatly appreciated, Thanks!

Answer

Crowman picture Crowman · Nov 4, 2014

You need to put the test.c before the libraries. Libraries should be specified on the command line after the things that use the symbols in them, so change to:

gcc test.c -Wall -I"C:\Users\Rohan\Desktop\GnuWin32\include" -L"C:\Users\Rohan\Desktop\GnuWin32\lib" -lgslcblas -lgsl -lm -o test

    ^^^^^^  (put this first)