LAPACK Archives

[Lapack] LAPACK and C++

There is no restriction on using LAPACK or CLAPACK from C++. Both are just 
regular libraries. Below an example code in C++ that works with LAPACK 
without any problem. (Ok, so there is nothing C++ except the extern "C" 
and the fact that I am using g++, your job is to do what you want with the 
C++ part.)

The main problem is that people forgot to declare the prototype and that's 
true that we are not providing any .h so far to do it for you.

Works the same with CLAPACK.

compile it with something like:
g++ -Wall -O3 \
        -L/usr/local/lib/ -llapack -lf77blas -lcblas -latlas\
        -lg2c -lm
should work.

Here I am using ATLAS BLAS.


#include <stddef.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

extern "C" double dnrm2_(int *n,double *x, int* incx);
extern "C" void   dgesv_(int *n, int *nrhs, double *a, int *lda, int *ipiv, 
double *b, int *ldb, int *info );
extern "C" void   dcopy_(int *n, double *x, int *incx, double *y, int *incy);
extern "C" void   dgemm_(char *transa, char *transb,
                         int *m, int *n, int *k, double *alpha, double *a, int 
*lda, double *b, int *ldb,
                         double *beta, double *c, int *ldc );

int main( ){

         int n=100, k=1;
         double A[n*n], b[n*k], Acopy[n*n], bcopy[n*k];
         int ipiv[n], info;
         int i;
         double normr, normb;

         int ione=1, int01;
         char charN = 'N';
         double dpone = 1.e0, dmone = -1.e0;

                 A[i] = ((double) rand()) / ((double) RAND_MAX) - 0.5;
                 b[i] = ((double) rand()) / ((double) RAND_MAX) - 0.5;

         int01 = n*k; dcopy_(&int01,b,&ione,bcopy,&ione);
         int01 = n*n; dcopy_(&int01,A,&ione,Acopy,&ione);

         dgesv_(&n, &k, A, &n, ipiv, b, &n, &info);

         normb = dnrm2_(&n,bcopy,&ione);
         dgemm_ (&charN, &charN, &n, &k, &n, &dpone, Acopy, &n, b, &n, &dmone, 
bcopy, &n);
         normr = dnrm2_(&n,bcopy,&ione);
         printf("\tDGESV [n=%d,k=%d] \t\t(check:%e)\n",

         return 0;


On Fri, 10 Mar 2006, Yao-ban Chan wrote:


      I am having problems using LAPACK for my C++ program. I tried 
using CLAPACK at first, and I was able to compile and run a test program 
in C, using the compiler gcc. However, either switching the compiler to 
g++ or renaming the file extension to .cpp resulted in errors. The IT 
staff at my university think that this is because CLAPACK cannot be used 
in C++. I understand there is a version of LAPACK for C++ (LAPACK++) but 
it doesn't seem to have the routine that I need (dsyevr). I understand 
that there may be a way to build and link the Fortran files directly to 
the C++ - is this possible? I can't seem to find any instructions on how 
to do it. If is it possible, could you please tell me how to go about 
doing it?

Thanks a lot,
Yao-ban Chan
Lapack mailing list

<Prev in Thread] Current Thread [Next in Thread>

For additional information you may use the LAPACK/ScaLAPACK Forum.
Or one of the mailing lists, or