1

I'm having trouble linking a simple cblas program, here's my minimal example:

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

int main() {

    int lda = 3;

    float A[] = { 0.11, 0.12, 0.13,
                  0.21, 0.22, 0.23 };

    int ldb = 2; 

    float B[] = { 1011, 1012,
                  1021, 1022,
                  1031, 1032 };

    int ldc = 2;

    float C[] = { 0.00, 0.00,
                  0.00, 0.00 };

    cblas_sgemm(CblasRowMajor,
                CblasNoTrans,
                CblasNoTrans,
                2, 2, 3, 1.0, A, lda, B, ldb, 0.0, C, ldc);

    std::cout << "[ " << C[0] << ", " << C[1] << "\n " << C[2] 
              << ", " << C[3] << " ]\n";
}

Now i'm trying to compile the program with g++ -lcblas test.cpp, but this gives me

gdev-home@gdev-home:~/Documents$ g++ -lcblas test.cpp
/tmp/ccEwRr40.o: In function `main':
test.cpp:(.text+0x112): undefined reference to `cblas_sgemm'
collect2: error: ld returned 1 exit status

which means it's not linking properly. The weird part is I do have the cblas libraries, I installed them via atlas

gdev-home@gdev-home:~/Documents$ dpkg --get-selections | grep atlas
libatlas-base-dev               install
libatlas-dev                    install
libatlas3-base                  install

if you look inside /usr/lib I have

gdev-home@gdev-home:/usr/lib$ ls | grep blas
libblas
libblas.a
libblas.so
libblas.so.3
libblas.so.3gf
libcblas.a
libcblas.so
libcblas.so.3
libcblas.so.3gf
libf77blas.a
libf77blas.so
libf77blas.so.3
libf77blas.so.3gf

Does anyone know why this program is failing to link?

4

1 回答 1

4

我认为 -lcblas arg 应该在 test.cpp 之后。

于 2015-10-29T16:44:21.877 回答