2009-07-16 2 views
2

저는 CLAPACK ATLAS 라이브러리를 사용하는 C++ 프로그램을 작성하고 있습니다. 그러나 프로그램을 라이브러리에 성공적으로 연결할 수 없습니다. 문제를 더 잘 보여주기 위해 작은 C 프로그램을 작성했습니다. 흥미롭게도이 작은 데모 프로그램은 GCC로 컴파일하면 잘 연결되지만 G ++로 컴파일하려고하면 동일한 링커 오류가 발생합니다. 나는 누군가가 내가 G ++과 GCC가 다르게하고 있다는 것을 알아낼 수 있기를 바랬다. 원래의 프로그램을 링크하기 위해서였다. (원래의 프로그램은 C++ 프로그램이고 나는 단지 "GCC를 사용할 수 없다")C C++ 링크 오류

여기에 작은 데모 프로그램입니다 :

#include <stdlib.h> 
#include <stdio.h> 
#include <time.h> 
#include <cblas.h> 
#include <clapack.h> 

// a is a column-major array of all the values in the matrix to invert 
// The matrix's height and width are the same because it is a square matrix. 
void invertMatrix(float *a, unsigned int height) 
{ 
    int info, ipiv[height]; 
    info = clapack_sgetrf(CblasColMajor, height, height, a, height, ipiv); 
    info = clapack_sgetri(CblasColMajor, height, a, height, ipiv); 
} 

void displayMatrix(float *a, unsigned int height, unsigned int width) 
{ 
    int i, j; 
    for(i = 0; i < height; i++) 
    { 
      for(j = 0; j < width; j++) 
      { 
        printf("%1.3f ", a[height*j + i]); 
      } 
      printf("\n"); 
    } 
    printf("\n"); 
} 

void multiplyMatrix(float *a, unsigned int aheight, unsigned int awidth, float *b, unsigned int bwidth, float *c) 
{ 
    cblas_sgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, aheight, bwidth, awidth, 1.0f, a, aheight, b, awidth, 0.0f, c, aheight); 
} 

int main(int argc, char *argv[]) 
{ 
    int i; 
    float a[9], b[9], c[9]; 
    srand(time(NULL)); 
    for(i = 0; i < 9; i++) 
    { 
      a[i] = 1.0f*rand()/RAND_MAX; 
      b[i] = a[i]; 
    } 
    displayMatrix(a, 3, 3); 
    invertMatrix(a, 3); 
    multiplyMatrix(a, 3, 3, b, 3, c); 
    displayMatrix(c, 3, 3); 
    return 0; 
} 

은 내가 GCC와 함께이 컴파일 할 때, 그것은 잘 작동 :

$ gcc -o linearalgebra linearalgebra.c -I /usr/include/atlas -L /usr/lib64/atlas/ -llapack -lblas 
$ ./linearalgebra 
0.723 0.755 0.753 
0.179 0.912 0.349 
0.642 0.265 0.530 

1.000 -0.000 0.000 
0.000 1.000 0.000 
0.000 0.000 1.000 

$ 

을 내가 G ++로 이것을 컴파일 할 때, 그것은 링커 오류가 있습니다 :

$ g++ -o linearalgebra linearalgebra.c -I /usr/include/atlas -L /usr/lib64/atlas/ -llapack -lblas 
/tmp/ccuhmDKE.o: In function `multiplyMatrix(float*, unsigned int, unsigned int, float*, unsigned int, float*)': 
linearalgebra.c:(.text+0x7b): undefined reference to `cblas_sgemm(CBLAS_ORDER, CBLAS_TRANSPOSE, CBLAS_TRANSPOSE, int, int, int, float, float const*, int, float const*, int, float, float*, int)' 
/tmp/ccuhmDKE.o: In function `invertMatrix(float*, unsigned int)': 
linearalgebra.c:(.text+0x182): undefined reference to `clapack_sgetrf(CBLAS_ORDER, int, int, float*, int, int*)' 
linearalgebra.c:(.text+0x1a0): undefined reference to `clapack_sgetri(CBLAS_ORDER, int, float*, int, int const*)' 
collect2: ld returned 1 exit status 
$ 

하지 적어도 마지막 :
운영 체제 : Fedora 10 (Linux hostname 2.6.27.25-170.2.72.fc10.x86_64 #1 SMP Sun Jun 21 18:39:34 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux)
도서관 : 또한

$ yum list | grep lapack 
lapack.x86_64      3.1.1-4.fc10     installed 
lapack-debuginfo.x86_64    3.1.1-4.fc10     installed 
lapack-devel.x86_64     3.1.1-4.fc10     @fedora 
$ yum list | grep blas 
blas.x86_64       3.1.1-4.fc10     installed 
blas-devel.x86_64     3.1.1-4.fc10     installed 
$ yum list | grep atlas 
atlas.x86_64       3.6.0-15.fc10    installed 
atlas-debuginfo.x86_64    3.6.0-15.fc10    installed 
atlas-devel.x86_64     3.6.0-15.fc10    @fedora 

, 보너스 포인트 : 정확히 LAPACK 사이의 역사와 기능의 관계는 무엇입니까 내 시스템에 대한 일부 정보 아틀라스?

답변

5

C 라이브러리에 C++ 프로그램을 연결하려면 C 라이브러리의 모든 함수 등 앞에 extern "C" 접두사가 있어야합니다. C++ 컴파일러는 링커가 심볼과 C 라이브러리의 이름을 일치시키는 것을 불가능하게하는 C++ 심볼 이름을 변형합니다. 당신은 당신의 C 기호를 선언 블록을 사용할 수 있습니다

extern "C" { 
    ... 
} 

당신은 cblas.hclapack.h 모든 선언 앞에 필요한 extern "C"을 포함하도록 지시하는 전 처리기 기호를 정의 할 수 있습니다.

+1

고마워, 마틴. 나는 G ++가 선언을 가로 질러 왔을 때 이름을 맹 글링하지 않는다는 것을 알고 있기 때문에 extern "C"{} 블록에 lapack과 blas 헤더 #include 문을 싸서 G ++과 함께 작동합니다. – Litherum