Page 1 of 1

How to link Lapack?

PostPosted: Thu May 19, 2011 6:36 am
by mdelmar
Hi!

I have just installed Lapack and Blas from Synaptic on Ubuntu 10.04. I'm trying to compile this simple programm:

Code: Select all
#include <stdio.h>
#include <clapack.h>
#include <cblas.h>


void sgesv( int* n, int* nrhs, float* a, int* lda, int* ipiv, float* b, int* ldb, int* info );

int main()
{
               
int uno = 1;
int tres = 3;

float A []={
2, 1, 1,
3, -1, 1,
0, 2, 3};

float x[]= {0, 0, 0};

float b [] = {1, 0, 3};

int ipiv[3];
int info;
int i;

sgesv (&tres, &uno, A, &tres, ipiv, b, &tres, &info);

for (i=0; i<3; ++i)  printf("%f\n", x[i]);

return 0;

}


But I get the following error: "undefinded reference to 'sgesv' " when I type "gcc lap.c -o lap -llapack -lblas"
I wrote another program that just uses BLAS and compiles fine with "gcc prueba.c -o prueba -lblas". However, this program using the lapack routine returns an error. Have I missed anything? What am I doing wrong?

Thanks in advance

Re: How to link Lapack?

PostPosted: Thu May 19, 2011 11:25 am
by admin
Hi,
You got a Fortran Mangling Problem.
Adding an underscore to the name of the routine should do the trick. (it can be different depending on your system)

Here is your code that works.

Code: Select all
#include <stdio.h>
void sgesv_( int* n, int* nrhs, float* a, int* lda, int* ipiv, float* b, int* ldb, int* info );

int main()
{
int uno = 1;
int tres = 3;

float A []={
2, 1, 1,
3, -1, 1,
0, 2, 3};

float x[]= {0, 0, 0};

float b [] = {1, 0, 3};

int ipiv[3];
int info;
int i;

sgesv_ (&tres, &uno, A, &tres, ipiv, b, &tres, &info);
printf("INFO SGESV=%d\n", info);
for (i=0; i<3; ++i)  printf("%f\n", b[i]);

return 0;
}


and here is the way to compile and link:
Code: Select all
gcc -c lap.c
gfortran lap.o -o lap -llapack -lblas


Best would be to use the Standard C language APIs for LAPACK.
It is available here: http://www.netlib.org/lapack/#_standard ... for_lapack
Your program will look like this:
Code: Select all
#include <stdio.h>
#include "lapacke.h"

int main()
{
float A []={
2, 1, 1,
3, -1, 1,
0, 2, 3};

float x[]= {0, 0, 0};

float b [] = {1, 0, 3};

int ipiv[3];
int info;
int i;

info = LAPACKE_sgesv (LAPACK_COL_MAJOR, 3, 1, A, 3, ipiv, b, 3);
printf("INFO SGESV=%d\n", info);
for (i=0; i<3; ++i)  printf("%f\n", b[i]);

return 0;
}


and to compile and link
Code: Select all
gcc -c newlap.c
gfortran newlap.o -o newlap -llapacke -llapack -lblas


Julie

Re: How to link Lapack?

PostPosted: Thu May 19, 2011 4:39 pm
by mdelmar
Wow, Julie, thank you for yor help. I totally forgot the underscore because I'm using a guide for Fortran.

Re: How to link Lapack?

PostPosted: Tue Jan 31, 2012 12:15 am
by venkatesh
Hi,
I am linux newbie....

I have installed Lapack 3.4 version and I tried to use the example program...compiled and linked using

gcc -c newlap.c
gfortran newlap.o -o newlap -llapacke -llapack -lblas

It gives me an error

"/usr/bin/ld: cannot find -llapacke"

Please help me......

Re: How to link Lapack?

PostPosted: Tue Jan 31, 2012 12:20 am
by venkatesh
Hi ,

I am new to linux,

I just blindly followed the steps for instaling lapack 3.4 and also tried to test it using your example program it gave me the following error

"cannot find llapacke"

I used the following commands
gcc -c newlap.c
gfortran newlap.o -o newlap -llapacke -llapack -lblas

Please help me.....

Re: How to link Lapack?

PostPosted: Sun Dec 29, 2013 3:19 am
by selvvy
solved my problem, thanks