2014-02-11 83 views
3

我正在嘗試使用dgesv_函數解決一個簡單問題Ax = b。但是我遇到了一個我無法克服的問題。我的代碼是:使用dgesv_時發生錯誤

#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); 

} 

我認爲的代碼是正確的,但是,每當我運行它,我得到以下錯誤:

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 *)" ([email protected]@[email protected]) referenced in function "void __cdecl ttt(void)" ([email protected]@YAXXZ) 
1>C:\Users\Daniel\documents\visual studio 2010\Projects\Eye Tracker\Debug\Eye Tracker.exe : fatal error LNK1120: 1 unresolved externals 

任何人有任何想法如何解決這個問題?

謝謝。

+1

你所鏈接對LAPACK庫?如果您是使用visual studio 2010編譯lapack或使用visual studio 2010的libararies? – drescherjm

回答

1

您的錯誤可能來自LAPACK未鏈接到您的程序。 CLAPACK很容易鏈接到C程序,但鏈接到C++需要添加幾行。據http://wwwx.cs.unc.edu/~cquammen/wp/2010/08/12/calling-clapack-code-from-c/以下行應該做的伎倆:

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; 
}