Linking error with LAPACK under cygwin

549 views Asked by At

Similar to the problem described in this question, I want to compile a C++ program using LAPACK under Windows 8.1, using cygwin (and written in NetBeans). I got the necessary packages in cygwin (lapack, liblapack-devel, liblapack0), and have written a function prototype:

extern "C" void dgeev_(char* jobvl, char* jobvr, int* n, double* a,
    int* lda, double* wr, double* wi, double* vl, int* ldvl,
    double* vr, int* ldvr, double* work, int* lwork, int* info);

But trying to compile still throws an error from the linker:

$ g++ -llapack -lblas main.cpp 
/tmp/cc3opfN5.o:main.cpp:(.text+0x349): undefined reference to `dgeev_'
/tmp/cc3opfN5.o:main.cpp:(.text+0x403): undefined reference to `dgeev_'
collect2: error: ld returned 1 exit status

The code looks like this (adapted from this example):

/* Locals */
int n = N, lda = LDA, ldvl = LDVL, ldvr = LDVR, info, lwork;
double wkopt;
double* work;
double h = k_max / N;
/* Local arrays */
double wr[N], wi[N], vl[LDVL * N], vr[LDVR * N];
double a[LDA * N];
/* Initialize matrix */
for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
        a[i * N + j] = integral(i + 1, j + 1);
        if (i == j) {
            a[i * N + j] += diagonal((i + 1) * h);
        }
    }
}
/* Executable statements */
printf(" DGEEV Example Program Results\n");
/* Query and allocate the optimal workspace */
lwork = -1;
char vec[] = "Vectors";
dgeev_(vec, vec, &n, a, &lda, wr, wi, vl, &ldvl, vr, &ldvr,
        &wkopt, &lwork, &info);
lwork = (int) wkopt;
work = (double*) malloc(lwork * sizeof (double));
/* Solve eigenproblem */
dgeev_(vec, vec, &n, a, &lda, wr, wi, vl, &ldvl, vr, &ldvr,
        work, &lwork, &info);

What am I missing? :-/

1

There are 1 answers

1
ztik On BEST ANSWER

Supposing that you have installed BLAS and LAPACK libraries correctly and the system can locate them, then you just have to change the order in the compile command.

$ g++ main.cpp -llapack -lblas

The compiler should know which functions of main.cpp should look into the libraries before declare them.