Fail to link c code to lapack / blas : undefined reference

北城以北 提交于 2019-12-11 01:23:54

问题


i have been trying for hours and it drives me crazy. The last error I get is :

demo_cblas.c:(.text+0x83): undefined reference to `clapack_sgetrf'
demo_cblas.c:(.text+0xa3): undefined reference to `clapack_sgetri'

I am compiling the code using

/usr/bin/gcc -o demo_cblas demo_cblas.c -L /usr/lib64 -l :libgfortran.so.3 -L /usr/lib64 \
    -llapack -L /usr/lib64 -lblas 

I try with and without libgfortran, with different compilers gcc-33, gcc-47, gcc-48. The test code is not from me but comes from this forum ...

#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 am on Suse 12.3 64bits. In /usr/lib64 I have liblapack.a liblapack.so, ... and libblas.a libblas.so, ... and libgfortran.so.3

The same code without the function "invertMatrix" (the one using the library) compiles fine.

Any idea or suggestion ?

Thank you all for your help.

Vava


回答1:


I'm quite positive that you also need to link to libcblas, which is the c wrapper library for libblas. Note that libblas is a FORTRAN library which therefore does not contain the function clapack_* you're calling.




回答2:


I've just got this working on FreeBSD with:

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

I'd installed math/atlas (from ports) and the lapack and blas packages.

See my question here



来源:https://stackoverflow.com/questions/24427802/fail-to-link-c-code-to-lapack-blas-undefined-reference

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