“undefined reference to 'cblas_ddot'” when using cblas library

微笑、不失礼 提交于 2019-12-06 04:39:08

问题


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?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/19507841/undefined-reference-to-cblas-ddot-when-using-cblas-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!