Failing to link c code to lapack: undefined reference

天涯浪子 提交于 2019-12-13 06:56:36

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/26636582/failing-to-link-c-code-to-lapack-undefined-reference

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!