15

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

HOW TO INSTALL 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?

feetwet
  • 3,248
  • 7
  • 46
  • 84
Rohan
  • 507
  • 1
  • 4
  • 15
  • You are missing some `-L/directory` and `-lgsl` like flags to `gcc`. And order of arguments to `gcc` is important. BTW, always pass `-Wall -Wextra -g` to `gcc`; and all this could be slightly easier on a Linux machine. – Basile Starynkevitch Nov 03 '14 at 22:07
  • Use also some version control system like [git](http://git-scm.com/) – Basile Starynkevitch Nov 03 '14 at 22:09
  • As @BasileStarynkevitch suggests, put the `test.c` before all the other stuff on your command line. The libraries should be specified after the things that use the symbols in them. – Crowman Nov 04 '14 at 02:21
  • @PaulGriffiths Brilliant. Thanks so much - works fine now. I didn't quite understand what Basile was alluding to. Can I ask why you left this as a comment rather than an answer? – Rohan Nov 04 '14 at 15:40
  • I also needed to have the `libgsl.dll` and `libgslcblas.dll` in the location I was compiling, in this case the Desktop. I find this strange since in PATH in my Environment Variables the bin folder (the location of the dll's is already recorded). – Rohan Nov 04 '14 at 15:54
  • @Rohan: I put it as an answer for you. Your `PATH` variable is used to search for executables, but not for libraries, so setting your `PATH` will have no effect on the locations searched for libraries. There are other environment variables, such as `LD_LIBRARY_PATH`, which can serve this purpose, though. – Crowman Nov 04 '14 at 18:43

1 Answers1

7

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)
Crowman
  • 25,242
  • 5
  • 48
  • 56