【问题标题】:"undefined reference to 'cblas_ddot'" when using cblas library使用 cblas 库时“未定义对 'cblas_ddot' 的引用”
【发布时间】:2013-10-22 02:05:36
【问题描述】:

我正在测试 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”是未定义的引用?

【问题讨论】:

  • 你的 GCC 命令行或 Makefile 是什么样的?

标签: c blas


【解决方案1】:

你不能只包含头文件——它只会告诉编译器函数存在于某处。您需要告诉链接器链接到 cblas 库。

假设您有一个 libcblas.a 文件,您可以使用 -lcblas 告诉 GCC。

GNU 科学图书馆的网站告诉你如何做到这一点:

【讨论】:

  • 我试过 gcc -Wall -I/usr/include -c test.c 并且它有效。但后来我执行了gcc test.o -o test,它仍然是undefined reference to 'cblas_ddot'
  • gcc 的第一次调用只是将test.c 编译为test.o。该库还没有被使用。如果您运行readelf -s test.o,您将看到未解析的符号cblas_ddot。您对gcc 的第二次调用只是将test.olibc 链接起来并创建最终的输出可执行文件。您需要在此处指定与 cblas 库的链接。
  • 我明白了。然后我尝试了here的方法,还是有错误/usr/bin/ld: cannot find -lcblas
  • 你安装了库吗? -l 将在标准目录中查找,不包括当前目录。
  • find /lib* /usr/lib* -name lib*.a
【解决方案2】:

我的问题刚刚解决。原因是我输入链接路径时出错了。感谢Jonathon Reinhart 的回答,它们在学习如何在 linux 中编码时非常有帮助。

编译命令为:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多