ATLAS gemm将未定义的引用链接到'cblas_sgemm'

时间:2012-05-28 14:24:19

标签: c++ c blas atlas

这是我第一次尝试使用ATLAS。我无法正确链接。这是一个非常简单的sgemm程序:

...
#include <cblas.h>


const int M=10;
const int N=8;
const int K=5;

int main()
{
    float *A = new float[M*K];
    float *B = new float[K*N];
    float *C = new float[M*N];

    // Initialize A and B

    cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, M, N, K, 1.0, A, K, B, N, 0.0, C, N);

        ...
}

当我在安装了标准ATLAS的linux平台上编译它时,它会给出链接错误:

g++ test.c -lblas -lcblas -latlas -llapack
/tmp/cc1Gu7sr.o: In function `main':
test.c:(.text+0x29e): undefined reference to `cblas_sgemm(CBLAS_ORDER, CBLAS_TRANSPOSE, CBLAS_TRANSPOSE, int, int, int, float, float const*, int, float const*, int, float, float*, int)'
collect2: ld returned 1 exit status

正如您所看到的,我尝试过提供不同的库组合,但没有帮助。我做错了什么?

2 个答案:

答案 0 :(得分:12)

你需要

extern "C"
{
   #include <cblas.h>
}

因为您使用g++进行编译。

或者你甚至可以做到

#ifdef __cplusplus
extern "C"
{
#endif
   #include <cblas.h>
#ifdef __cplusplus
}
#endif

也可以编译为C.

在C ++中编译时,名称应该被破坏。但由于cblas是用C编译的,因此导出的符号没有错位名称。所以你必须指示编译器寻找C风格的符号。

答案 1 :(得分:2)

小心代码。它是“C”,而不是C. 所以,代码最终是

#ifdef __cplusplus
extern "C"
{
#endif //__cplusplus
   #include <cblas.h>
#ifdef __cplusplus
}
#endif //__cplusplus
相关问题