Error using dgesv_

I am trying to solve a simple task Ax = b using the dgesv_ function. However, I ran into a problem that I can’t get through. My code is:

#include <cstdio>
#include <f2c.h>
#include <clapack.h>

void main(void)
{
    /* 3x3 matrix A
     * 76 25 11
     * 27 89 51
     * 18 60 32
     */
    double A[9] = {76, 27, 18, 25, 89, 60, 11, 51, 32};
    double b[3] = {10, 7, 43};

    int N = 3;
    int nrhs = 1;
    int lda = 3;
    int ipiv[3];
    int ldb = 3;
    int info;

    dgesv_(&N, &nrhs, A, &lda, ipiv, b, &ldb, &info);

}

I think the code is correct, however, whenever I run it, I get the following error:

LINK : C:\...\Eye Tracker.exe not found or not built by the last incremental link; performing full link
1>     Creating library C:\...\Eye Tracker\Debug\Eye Tracker.lib and object C:\Users\Daniel\documents\visual studio 2010\Projects\Eye Tracker\Debug\Eye Tracker.exp
1>ellipse_fit.obj : error LNK2019: unresolved external symbol "void __cdecl dgesv_(int const *,int const *,double *,int const *,int *,double *,int const *,int *)" (?dgesv_@@YAXPBH0PAN0PAH102@Z) referenced in function "void __cdecl ttt(void)" (?ttt@@YAXXZ)
1>C:\Users\Daniel\documents\visual studio 2010\Projects\Eye Tracker\Debug\Eye Tracker.exe : fatal error LNK1120: 1 unresolved externals

Does anyone know how to solve this?

Thank.

+3
source share
1 answer

Perhaps your mistake is due to the fact that LAPACK is not associated with your program. CLAPACK easily binds to C, but to communicate with C ++, you need to add a few lines. According to http://wwwx.cs.unc.edu/~cquammen/wp/2010/08/12/calling-clapack-code-from-c/ the following lines should do the trick:

extern "C" {
  #include <f2c.h>
  #include <clapack.h>
}

, , g++ main.cpp -o main -llapack -lblas -lm:

#include <iostream>

using namespace std;
extern "C"
{
void dgesv_(int* n,int* nrhs,double* a,int* lda,int* ipiv, double* b,int* ldb,int* info);
}

int main(void)
{
    /* 3x3 matrix A
     * 76 25 11
     * 27 89 51
     * 18 60 32
     */
    double A[9] = {1, 0, 0, 0, 2, 0, 0, 0,4};
    double b[3] = {42, 84, 168};

    int N = 3;
    int nrhs = 1;
    int lda = 3;
    int ipiv[3];
    int ldb = 3;
    int info;

    dgesv_(&N, &nrhs, A, &lda, ipiv, b, &ldb, &info);

    cout<<"solution : "<<b[0]<<"  "<<b[1]<<"  "<<b[2]<<endl;
}
+1

All Articles