2009-07-16 110 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和之间的历史和功能的关系,我的系统的一些信息ATLAS?

回答

5

如果要将C++程序链接到C库,则必须确保C库中的所有函数等都以extern "C"为前缀。 C++编译器破坏了C++符号名称,这使得链接器无法将符号与C库中的名称进行匹配。您可以使用块声明你的C符号:

extern "C" { 
    ... 
} 

您可以定义一个预处理符号指示cblas.hclapack.h,包括必要的extern "C"中的所有声明的前面。

+1

谢谢,马丁。我在extern“C”{}块中封装了lapack和blas头文件#include语句,现在它可以与G ++一起使用,因为G ++在遇到声明时不知道如何修改名称。 – Litherum 2009-07-16 13:36:22