Lapack linking in c++ (Ubuntu)

Open discussion regarding features, bugs, issues, vendors, etc.

Lapack linking in c++ (Ubuntu)

Postby Winy » Thu Apr 13, 2006 5:42 pm

Hi all,

I have used lapack in c++ on red hat previously and i had no problem to link the libraries with:

g++ test.C -L/usr/lib/ -llapack -lblas -lm -lg2c

Now I am working on my laptop which has Ubuntu with the lapack provided by Synaptic and the previous command didn't work anymore. It gave me:

/usr/bin/ld: cannot find -llapack
collect2: ld returned 1 exit status

So after a look to the usr/lib folder, i changed the command by:
g++ test.C -L/usr/lib/ -lblas -lm -lg2c

But now I have another error:
/tmp/ccfwmBwU.o: In function `main':
test.c.text+0x151): undefined reference to `dgesv_'
collect2: ld returned 1 exit status

It seems that I don't export the function correctly. Does anyone have any idea about this?



ps: here is the code

#include <iostream>

#define MAX 10

extern "C" {
extern void dgesv_(int *,int *,double *,int *,int*,double *,int*,int*);

int main(){
// Values needed for dgesv
int n;
int nrhs = 1;
double a[MAX][MAX];
double b[1][MAX];
int lda = MAX;
int ldb = MAX;
int ipiv[MAX];
int info;
// Other values
int i,j;

// Read the values of the matrix
std::cout << "Enter n \n";
std::cin >> n;
std::cout << "On each line type a row of the matrix A followed by one element of b:\n";
for(i = 0; i < n; i++){
std::cout << "row " << i << " ";
for(j = 0; j < n; j++)std::cin >> a[j][i];
std::cin >> b[0][i];

// Solve the linear system
dgesv_(&n, &nrhs, &a[0][0], &lda, ipiv, &b[0][0], &ldb, &info);

// Check for success
if(info == 0)
// Write the answer
std::cout << "The answer is\n";
for(i = 0; i < n; i++)
std::cout << "b[" << i << "]\t" << b[0][i] << "\n";
// Write an error message
std::cerr << "dgesv returned error " << info << "\n";
return info;
Posts: 1
Joined: Thu Apr 13, 2006 5:13 pm

Postby Julie » Tue May 02, 2006 2:18 pm

undefined reference to `dgesv_'
means that the linker does not manage to find the routine dgesv in the libraries.
To check your LAPACK library, you can use the 'nm' command. Try
nm /usr/lib/ | grep -i dgesv

you should get something like:
00000000 T dgesv_

dgesv might with with 2 underscore or in upper case, it depends on your compiler/machine. You have to use that name to call the dgesv routine.
If you don't have it, it means your library is incomplete.
What you can do is dowload the Lapack library from
after just run:
Code: Select all
tar xfvz lapack.tgz

Please refer to for more information on the installation process.

Hope it helps
Posts: 299
Joined: Wed Feb 23, 2005 12:32 am
Location: ICL, Denver. Colorado

Return to User Discussion

Who is online

Users browsing this forum: No registered users and 2 guests