Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"undefined reference to 'cblas_ddot'" when using cblas library

Tags:

c

blas

I was testing the cblas ddot, and the code I used is from the link and I fixed it as

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

int main()
{
    double  m[10],n[10];
    int i;
    int result;

    printf("Enter the elements into first vector.\n");
    for(i=0;i<10;i++)
        scanf("%lf",&m[i]);

    printf("Enter the elements into second vector.\n");
    for(i=0;i<10;i++)
        scanf("%lf",&n[i]);

    result = cblas_ddot(10, m, 1, n, 1);
    printf("The result is %d\n",result);

    return 0;
}

Then when I compiled it, it turned out to be:

/tmp/ccJIpqKH.o: In function `main':
test.c:(.text+0xbc): undefined reference to `cblas_ddot'
collect2: ld returned 1 exit status

I checked the cblas file in /usr/include/cblas.h, and noticed there is

double cblas_ddot(const int N, const double *X, const int incX,
              const double *Y, const int incY);

I don't know where it is going wrong. Why does the compiler said the "cblas_ddot" is undefined reference?

like image 802
Old Panda Avatar asked Oct 22 '13 02:10

Old Panda


2 Answers

You can't just include the header - that only tells the compiler that the functions exist somewhere. You need to tell the linker to link against the cblas library.

Assuming you have a libcblas.a file, you can tell GCC about it with -lcblas.

The web site for GNU Scientific Library tells you how to do this:

  • 2.2 Compiling and Linking
like image 112
Jonathon Reinhart Avatar answered Sep 25 '22 19:09

Jonathon Reinhart


My problem was just solved. The reason is that I made a mistake when inputed the link path. Thanks for Jonathon Reinhart's answers, they are really helpful when learning how to code in linux.

The compile commands are:

gcc -c test.c
gcc -L/usr/lib64 test.o -lgsl -lgslcblas -lm

Where "/usr/lib64" is the correct link path.

like image 39
Old Panda Avatar answered Sep 22 '22 19:09

Old Panda