0

I have this trivial C program, but just can't it to link correctly.

here is the program,

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

int main()
{
   double bottom_tail = gsl_cdf_gaussian_P(-1.96, 1);
   printf("Area between [-1.96, 1.96]: %g\n", 1-2*bottom_tail);
}

at the shell, I'm doing:

gcc gsl_erf.c -o gslTest -I/usr/local/include/gsl -L/usr/local/lib

I know for sure that the gsl_cdf.h header file is in /usr/local/include/gsl, similarly I know that the .sos are in /usr/local/lib

the linker, gcc backend?, complains that I have an undefined reference to gsl_cdf_gaussian_P

I thought my order was incorrect, so I also tried:

gcc -I/usr/local/include/gsl -L/usr/local/lib gsl_erf.c -o gslTest

but this craps out as well. What am I doing wrong? :(

5
  • Your question says that the undefined reference is gel_cdf_gausssian_P. Is that a typo in the question, or does the linker message actually refer to "gel" rather than "gsl"?
    – Wyzard
    Nov 21, 2013 at 3:41
  • no, that was a typo, fixed.
    – user1971598
    Nov 21, 2013 at 3:44
  • gsl_cdf_gausssian_P?
    – Havenard
    Nov 21, 2013 at 3:47
  • Ha, I didn't even notice that part. :-)
    – Wyzard
    Nov 21, 2013 at 3:47
  • okay, i think i got all the typos now.
    – user1971598
    Nov 21, 2013 at 3:50

2 Answers 2

1

You have to tell the compiler to actually link with the library you're using: you need an -l option, probably something like -lgsl. (Take the name of the .so file, remove the .so suffix and lib prefix, and that's what to put after -l.)

The -L option tells the linker where to find libraries, but doesn't direct it to actually link with anything — just like the -I option tells the compiler where to find headers, but doesn't actually #include any code.

5
  • thanks, but now I get way more undefined references, all sorts of cblas_<something>. But I think this might be because I don't have blas installed. EDIT: Just installed blas but still have these undefined references. EDIT #2: Just looked on gnu page for gls, and found this, The library is easy to compile and does not have any dependencies on other packages. So i don't think it should matter if I don't have blas installed.
    – user1971598
    Nov 21, 2013 at 3:51
  • Looks like you also need -lgslcblas. It comes with GSL (at least it did in the Debian package I just tried). I'm not really familiar with GSL, but from the docs it sounds like they put the CBLAS stuff in a separate library so that you have the option of linking a different BLAS implementation instead.
    – Wyzard
    Nov 21, 2013 at 4:04
  • okay, so I now did gcc -I/usr/local/include/gsl -L/usr/local/lib gel_erf.c -o gslTest -lgsl -lgslcblas and this compiled, but when running the executable, I get an error about loading the shared library, libgsl.so.0 which I know exists in /usr/local/lib/
    – user1971598
    Nov 21, 2013 at 4:11
  • Sounds like /usr/local/lib isn't on the dynamic linker's search path, though I'm surprised if that's the case since most systems have it there by default. Try setting LD_LIBRARY_PATH=/usr/local/lib when running the program.
    – Wyzard
    Nov 21, 2013 at 4:20
  • Arch has a GSL package. If you install that, you'll have the files in /usr/include and /usr/lib so you won't need the -I, -L, and LD_LIBRARY_PATH. (Plus you get all the other benefits of packages, like automated upgrades.)
    – Wyzard
    Nov 21, 2013 at 4:43
1

You don't actually ask for the libraries on the compile command. You provide a search path with "-L" but you don't actually request the libraries. You need something like "-lgsl" as well (assuming the library is libgsl.so).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.