4

I'm trying to link gsl in a small c program.

#include "stdlib.h"
#include "stdio.h"
#include "gsl/gsl_block_float.h"
#include "gsl/gsl_matrix_float.h"

int main(void)
{
  gsl_matrix_float* m = gsl_matrix_float_alloc(2, 2);
  gsl_matrix_float_fprintf(stdout, m, "%f");
}

I'm compiling with gcc -lgsl -lgslcblas -lm program.c. I've tried gcc $(pkg-config --cflags gsl) $(pkg-config --libs gsl) program.c as well, along with gsl-config. In every case, gcc returns

/tmp/cc1wKgXm.o: In function `main':
program.c:(.text+0x13): undefined reference to `gsl_matrix_float_alloc'
program.c:(.text+0x32): undefined reference to `gsl_matrix_float_fprintf'
collect2: error: ld returned 1 exit status

objdump --syms /usr/lib/libgsl.so | grep gsl_matrix_float returns the proper symbols, as does grepping my headers. Everything is in /usr/lib or /usr/include What am I doing wrong?

  • Try changing the order of your linking. `gcc -lgslcblas -lm -lgsl program.c` – Chol Nhial Jul 24 '15 at 01:03
  • 2
    @CholNhial: if anything, `program.c` has to come *first*, since it doesn't define any symbols the libraries need. – EOF Jul 24 '15 at 01:05
  • Thanks @EOF - I'm stupid. –  Jul 24 '15 at 01:06
  • @SM8: It's a really easy mistake to make, and infuriatingly difficult to spot. Also, it's a bit esoteric *why* the order even matters at all. Don't beat yourself up over it. – EOF Jul 24 '15 at 01:13
  • Found an answer that actually explains the problem: http://stackoverflow.com/a/409470/3185968 – EOF Jul 24 '15 at 01:18

1 Answers1

10

I got this from the ubuntu forums. The order of the arguments togcc might be the issue

gcc -o program program.c `gsl-config --cflags --libs`
Chol Nhial
  • 1,327
  • 1
  • 10
  • 25