未能将c代码链接到lapack:未定义的引用 [英] Failing to link c code to lapack: undefined reference

查看:651
本文介绍了未能将c代码链接到lapack:未定义的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从C中使用lapack函数。

以下是一些测试代码,从这个问题

  #include  
#include< stdio.h>
#include< time.h>
#includeclapack.h
#includecblas.h

void invertMatrix(float * a,unsigned int height){
int info,ipiv [高度];
info = clapack_sgetrf(CblasColMajor,height,height,a,height,ipiv);
info = clapack_sgetri(CblasColMajor,height,a,height,ipiv);
}

void displayMatrix(float * a,unsigned int height,unsigned int width)
{
int i,j;对于(j = 0; j <宽度; j ++)
{
printf(%1.3f),
为(i = 0; i }
printf(\\\
);
}
printf(\\\
);


$ b int main(int argc,char * argv [])
{
int i;
float a [9],b [9],c [9];
srand(time(NULL)); (i = 0; i <9; i ++)
{
a [i] = 1.0f * rand()/ RAND_MAX;
b [i] = a [i];
}
displayMatrix(a,3,3);
返回0;
}

我使用gcc进行编译:

  gcc -o test test.c \ 
-lblas -llapack -lf2c

nb:我试过了各种命令库,我也尝试过其他库,如latlas,lcblas,lgfortran等。

错误信息是:
$ b $ pre $ /tmp//cc8JMnRT.o:函数`inverseMatrix':
test.c :(。text + 0x94):对`clapack_sgetrf'的未定义引用
test.c :(。text + 0xb4):未定义对`clapack_sgetri'的引用
collect2:错误:ld返回1退出状态

找到并包含clapack.h(作为图集的一部分安装)。 clapack.h包含有问题的函数---所以不能找到它们?



符号实际上在库libalapack中(使用字符串)。但是,向gcc命令添加-lalapack似乎需要添加-lcblas(大量未定义的cblas_ *引用)。安装cblas会自动卸载地图集,这会删除clapack.h。



所以,这种感觉就像某种依赖性地狱。



我使用的是FreeBSD 10 amd64,所有相关的库似乎都已安装,并且路径正确。



任何帮助都非常感谢。



谢谢 $ b Ivan

解决方案

我卸载了所有与远程相关的东西--- blas,cblas,lapack,atlas等---然后单独重新安装atlas(从端口),然后是lapack和blas包。

这一次,/ usr / local / lib包含一个新的lib文件:libcblas.so ---之前的随机安装必须已经删除它。



现在编译的gcc代码如下:

  gcc -o test test.c \ 
-llapack -lblas -lalapack -lcblas

更改-l参数的顺序似乎没有任何区别。 p>

I am trying to use lapack functions from C.

Here is some test code, copied from this question

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "clapack.h"
#include "cblas.h"

void invertMatrix(float *a, unsigned int height){
  int info, ipiv[height];
  info = clapack_sgetrf(CblasColMajor, height, height, a, height, ipiv);
  info = clapack_sgetri(CblasColMajor, height, a, height, ipiv);
}

void displayMatrix(float *a, unsigned int height, unsigned int width)
{
  int i, j;
  for(i = 0; i < height; i++){
    for(j = 0; j < width; j++)
      {
        printf("%1.3f ", a[height*j + i]);
      }
    printf("\n");
  }
  printf("\n");
}


int main(int argc, char *argv[])
{
  int i;
  float a[9], b[9], c[9];
  srand(time(NULL));
  for(i = 0; i < 9; i++)
    {
      a[i] = 1.0f*rand()/RAND_MAX;
      b[i] = a[i];
    }
  displayMatrix(a, 3, 3);
  return 0;
}

I compile this with gcc:

gcc -o test test.c  \
    -lblas -llapack -lf2c

n.b.: I've tried those libraries in various orders, I've also tried others libs like latlas, lcblas, lgfortran, etc.

The error message is:

/tmp//cc8JMnRT.o: In function `invertMatrix':
test.c:(.text+0x94): undefined reference to `clapack_sgetrf'
test.c:(.text+0xb4): undefined reference to `clapack_sgetri'
collect2: error: ld returned 1 exit status

clapack.h is found and included (installed as part of atlas). clapack.h includes the offending functions --- so how can they not be found?

The symbols are actually in the library libalapack (found using strings). However, adding -lalapack to the gcc command seems to require adding -lcblas (lots of undefined cblas_* references). Installing cblas automatically uninstalls atlas, which removes clapack.h.

So, this feels like some kind of dependency hell.

I am on FreeBSD 10 amd64, all the relevant libraries seem to be installed and on the right paths.

Any help much appreciated.

Thanks

Ivan

解决方案

I uninstalled everything remotely relevant --- blas, cblas, lapack, atlas, etc. --- then reinstalled atlas (from ports) alone, and then the lapack and blas packages.

This time around, /usr/local/lib contained a new lib file: libcblas.so --- previous random installations must have deleted it.

The gcc line that compiles is now:

gcc -o test test.c  \
-llapack -lblas -lalapack -lcblas

Changing the order of the -l arguments doesn't seem to make any difference.

这篇关于未能将c代码链接到lapack:未定义的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆