1

I am trying to use the cblas library for matrix multiplication, but get "undefined reference to `cblas_dgemm'" when I try to compile, even though the library should be linked properly.

Code in test.c looks like this:

...
#include <cblas.h>

void reference_dgemm (int N, double ALPHA, double* A, double* B, double* C)
{
  int ORDER = 101; // row major
  char TRANSA = 111; // no transpose
  char TRANSB = 111;
  int M = N;
  int K = N;
  double BETA = 1.;
  int LDA = N;
  int LDB = N;
  int LDC = N;
  cblas_dgemm(ORDER, TRANSA, TRANSB, M, N, K, ALPHA, A, LDA, B, LDB, BETA, C, LDC);
}

is compiled like this:

gcc -lblas test.c

giving this error:

/tmp/cc9fVU41.o: In function `reference_dgemm':
test.c:(.text+0xc7): undefined reference to `cblas_dgemm'
collect2: error: ld returned 1 exit status

I have tried using #include <gsl/gsl_cblas.h> with -lgslcblas instead, as in this example, same problem. Format and name of the referenced function I got from /usr/include/cblas.h

Swend
  • 11
  • 1
  • 2
    Did you install development packet of BLAS? I mean: `sudo apt-get install libblas-dev` – LPs Oct 12 '15 at 11:38
  • 4
    Libraries goes at the end of the command line. Do `gcc test.c -lblas` – nos Oct 12 '15 at 11:39
  • @nos AFAIK not mandatory. – LPs Oct 12 '15 at 11:43
  • @LPs Yes it is, if you're using a modern toolchain. – nos Oct 12 '15 at 11:43
  • And now it works. How did I not know this. Do you know _why_ they need to be at the end? man gcc doesn't seem to mention it. – Swend Oct 12 '15 at 11:45
  • @nos a `gcc 4.9.2` does not care about the order. Eg. simple main with pow can be compiled with `gcc -o test test.c -lm` or `gcc -lm -o test text.c` – LPs Oct 12 '15 at 11:46
  • @LPs It's the version of the linker that matters in this regard, the `--no-copy-dt-needed-entries` and `--as-needed` was made default at some point (at least on most distros)Make sure your tests doesn't constant fold your call to pow() though, in which case -lm isn't needed at all. – nos Oct 12 '15 at 11:48
  • @user3047819 See this question: http://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc – nos Oct 12 '15 at 11:51
  • @nos My simple test does not use `pow` as `pow(2,3)` that does not require to link math lib. BTW it work both ways. Thx for your explanation. I learned something new today ;) – LPs Oct 12 '15 at 12:01
  • why you didn't know - this feature is new, the man page is provided as a courtesy, the official GCC documentation is in texinfo, not man, – Jasen Oct 12 '15 at 12:02

0 Answers0