1

I'm not able to link my program with GSL. The Compilation command is: gcc -lgsl -lgslcblas -lm -o exe t.o. The reported error is undefined reference to 'gsl_ran_pareto'

But when I check the symbols of libgsl, gsl_ran_pareto does appear:

$nm -D /usr/lib/x86_64-linux-gnu/libgsl.so | grep gsl_ran_pareto
000000000011e980 T gsl_ran_pareto
000000000011e9d0 T gsl_ran_pareto_pdf

I tried to link with the static GSL library (adding -static) and it has the same problem: the linker reports undefined reference but nm /usr/lib/x86_64-linux-gnu/libgsl.a finds this symbol. I also tried copying the library to the local folder and link directly - still the same problem.

Some more information (the code of the test program t.c, the symbol list of t.o, libgsl-dev package is installed correctly).

$cat t.c
#include <gsl/gsl_math.h>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_rng.h>
int main(){
    gsl_ran_pareto(NULL, 1, 1); 
    return 0; 
}

$ nm t.o 
                 U gsl_ran_pareto
0000000000000000 T main

$ dpkg -s libgsl-dev
Package: libgsl-dev
Status: install ok installed
...

OS: Ubuntu 16.04

Any idea how to investigate this further? The linker correctly finds the library (otherwise it would complain), and the library contains the missing symbol. So what can go wrong? Thanks!

Nachshon Cohen
  • 113
  • 2
  • 10

1 Answers1

0

Apparently, I arranged the linker flags in the wrong order. Dynamic library flags must appear after the object files. The following linking command works: gcc -o exe t.c -lgsl -lgslcblas -lm

More information: Why does the order in which libraries are linked sometimes cause errors in GCC?

Nachshon Cohen
  • 113
  • 2
  • 10