0

I am trying to use lapack functions from C.

Here is some test code, copied from this question

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "clapack.h"
#include "cblas.h"

void invertMatrix(float *a, unsigned int height){
  int info, ipiv[height];
  info = clapack_sgetrf(CblasColMajor, height, height, a, height, ipiv);
  info = clapack_sgetri(CblasColMajor, height, a, height, ipiv);
}

void displayMatrix(float *a, unsigned int height, unsigned int width)
{
  int i, j;
  for(i = 0; i < height; i++){
    for(j = 0; j < width; j++)
      {
        printf("%1.3f ", a[height*j + i]);
      }
    printf("\n");
  }
  printf("\n");
}


int main(int argc, char *argv[])
{
  int i;
  float a[9], b[9], c[9];
  srand(time(NULL));
  for(i = 0; i < 9; i++)
    {
      a[i] = 1.0f*rand()/RAND_MAX;
      b[i] = a[i];
    }
  displayMatrix(a, 3, 3);
  return 0;
}

I compile this with gcc:

gcc -o test test.c  \
    -lblas -llapack -lf2c

n.b.: I've tried those libraries in various orders, I've also tried others libs like latlas, lcblas, lgfortran, etc.

The error message is:

/tmp//cc8JMnRT.o: In function `invertMatrix':
test.c:(.text+0x94): undefined reference to `clapack_sgetrf'
test.c:(.text+0xb4): undefined reference to `clapack_sgetri'
collect2: error: ld returned 1 exit status

clapack.h is found and included (installed as part of atlas). clapack.h includes the offending functions --- so how can they not be found?

The symbols are actually in the library libalapack (found using strings). However, adding -lalapack to the gcc command seems to require adding -lcblas (lots of undefined cblas_* references). Installing cblas automatically uninstalls atlas, which removes clapack.h.

So, this feels like some kind of dependency hell.

I am on FreeBSD 10 amd64, all the relevant libraries seem to be installed and on the right paths.

Any help much appreciated.

Thanks

Ivan

Community
  • 1
  • 1
Ivan Uemlianin
  • 953
  • 7
  • 21
  • I have read that question obvs - note that I link to it. I don't think it's a duplicate, and the other question has not been answered adequately. – Ivan Uemlianin Oct 30 '14 at 09:23
  • It doesn't look like you've tried the suggestion in the answer to the duplicate question though, i.e. add `-lcblas` to your command line ? (Oh, my bad, I just re-read your question more carefully and you mentioned libcblas after all - did that not help ?). I've retracted my close vote now anyway. – Paul R Oct 30 '14 at 09:25
  • Thanks :) For my part, I did some more work on this this morning and I've added some more details after the error message. – Ivan Uemlianin Oct 30 '14 at 09:30
  • I'm just trying this on OS X now, which should be close enough to FreeBSD to see what's going on... – Paul R Oct 30 '14 at 09:32
  • OK - on OS X I can get it to compile with `-llapack -lblas -latlas` - you seem to need `-llapack` for `clapack_sgetrf`/`clapack_sgetri`, and then you need the other two to resolve further dependencies from `liblapack`. – Paul R Oct 30 '14 at 09:47
  • Thanks. Adding -latlas didn't help me however. :( In the meantime I have managed to fix it, just posting the answer now. – Ivan Uemlianin Oct 30 '14 at 12:20

1 Answers1

0

I uninstalled everything remotely relevant --- blas, cblas, lapack, atlas, etc. --- then reinstalled atlas (from ports) alone, and then the lapack and blas packages.

This time around, /usr/local/lib contained a new lib file: libcblas.so --- previous random installations must have deleted it.

The gcc line that compiles is now:

gcc -o test test.c  \
-llapack -lblas -lalapack -lcblas

Changing the order of the -l arguments doesn't seem to make any difference.

Ivan Uemlianin
  • 953
  • 7
  • 21