How to link Lapack?

Post here if you have a question about linking your program with LAPACK or ScaLAPACK library

How to link Lapack?

Postby mdelmar » Thu May 19, 2011 6:36 am

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
mdelmar
 
Posts: 2
Joined: Thu May 19, 2011 6:28 am

Re: How to link Lapack?

Postby admin » Thu May 19, 2011 11:25 am

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
admin
Site Admin
 
Posts: 474
Joined: Wed Dec 08, 2004 7:07 pm

Re: How to link Lapack?

Postby mdelmar » Thu May 19, 2011 4:39 pm

Wow, Julie, thank you for yor help. I totally forgot the underscore because I'm using a guide for Fortran.
mdelmar
 
Posts: 2
Joined: Thu May 19, 2011 6:28 am

Re: How to link Lapack?

Postby venkatesh » Tue Jan 31, 2012 12:15 am

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......
venkatesh
 
Posts: 2
Joined: Mon Jan 30, 2012 10:20 pm

Re: How to link Lapack?

Postby venkatesh » Tue Jan 31, 2012 12:20 am

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.....
venkatesh
 
Posts: 2
Joined: Mon Jan 30, 2012 10:20 pm

Re: How to link Lapack?

Postby selvvy » Sun Dec 29, 2013 3:19 am

solved my problem, thanks
selvvy
 
Posts: 1
Joined: Sun Dec 29, 2013 3:04 am


Return to Linking Problem

Who is online

Users browsing this forum: No registered users and 1 guest