使用cblas库时“未定义引用'cblas_ddot'”

时间:2013-10-22 02:05:37

标签: c blas

我正在测试cblas ddot,我使用的代码来自link,我将其修复为

#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;
}

然后当我编译它时,结果是:

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

我检查了/usr/include/cblas.h中的cblas文件,发现有

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

我不知道哪里出错了。为什么编译器说“cblas_ddot”是未定义的引用?

2 个答案:

答案 0 :(得分:4)

你不能只包含标题 - 它只告诉编译器函数存在某处。您需要告诉链接器链接cblas库。

假设您有libcblas.a个文件,可以通过-lcblas告诉GCC。{/ p>

GNU Scientific Library的网站告诉您如何执行此操作:

答案 1 :(得分:2)

我的问题刚刚解决了。原因是我在输入链接路径时犯了一个错误。感谢Jonathon Reinhart的答案,在学习如何在linux中编写代码时,它们非常有用。

编译命令是:

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

其中“/ usr / lib64”是正确的链接路径。

相关问题