我有两个程序在C中使用LAPACK例程dgeev
。一个似乎正在工作,另一个不编译声明对dgeev
的未定义引用。我试图理解为什么。
下面的第一个代码 - 名为mamapack.c - 在编译和运行时产生合理的结果:
ludi @ ludi-M17xR4:〜/ Desktop / tests $ gcc -o mamapack mamapack.c -L / usr / local / lib -llapack -lblas&& ./mamapack
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
//...........................................................................
void dgeTranspose( double *Transposed, double *M ,int n) {
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
Transposed[i+n*j] = M[i*n+j];
}
//...........................................................................
// MatrixComplexEigensystem: computes the eigenvectors and eigenValues of input matrix A
// The eigenvectors are stored in columns
//............................................................................
void MatrixComplexEigensystem( double *eigenvectorsVR, double *eigenvaluesW, double *A, int N) {
int i;
double *AT = (double *) malloc( N*N*sizeof(double ) );
dgeTranspose( AT, A , N);
char JOBVL ='N'; // Compute Right eigenvectors
char JOBVR ='V'; // Do not compute Left eigenvectors
double VL[1];
int LDVL = 1;
int LDVR = N;
int LWORK = 4*N;
double *WORK = (double *)malloc( LWORK*sizeof(double));
double *RWORK = (double *)malloc( 2*N*sizeof(double));
int INFO;
double *eigenvaluesWR =eigenvaluesW;
double *eigenvaluesWI = eigenvaluesW + N;
dgeev_( &JOBVL, &JOBVR, &N, AT , &N,
eigenvaluesWR, eigenvaluesWI,
VL, &LDVL,
eigenvectorsVR, &LDVR,
WORK, &LWORK, &INFO );
printf("\nping1\n");
dgeTranspose( AT, eigenvectorsVR , N);
for(i=0;i<N*N;i++) eigenvectorsVR[i]=AT[i];
free(WORK);
free(RWORK);
free(AT);
}
int main() {
int i,j;
const int N = 3;
double A[] = { 1. , 0. , 0. , 0. , 1., 0. , 0., 0., 1.};
double eigenVectors[N*N];
double eigenValues[2*N];
MatrixComplexEigensystem( eigenVectors, eigenValues, A, N);
printf("\nEigenvectors\n");
for(i=0;i<N;i++){
for(j=0;j<N;j++) printf("%e ", eigenVectors[i*N + j]);
printf("\n");
}
printf("\nEigenvalues \n");
for(i=0;i<N;i++) printf("%e ", eigenValues[i] );
printf("\n--------------------------------------------------------\n");
return 0;
}
然后我运行了另一个我称之为lapack1.c的代码,它只是本文档中官方的“C语言示例程序”:https://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/dgeev.htm (由于潜在的版权限制,我不敢单独发布)
ludi @ ludi-M17xR4:〜/ Desktop / tests $ gcc -o lapack1 lapack1.c -L / usr / local / lib -llapack -lblas&amp;&amp; ./lapack1
产生
tmp / cciRzQru.o:在函数
main': lapack1.c:(.text+0xf3): undefined reference to
dgeev'lapack1.c :(。text + 0x1b6):未定义引用 `dgeev'cols2:错误:ld返回1退出状态
答案 0 :(得分:2)
仔细检查您的功能名称。链接器抱怨对函数dgeev()
的未定义引用。工作代码调用另一个名为dgeev_()
的函数。
使用选项-D进行编译,如下所示:
ludi @ ludi-M17xR4:〜/ Desktop / tests $ gcc -Ddgeev = dgeev_ -o lapack1 lapack1.c -L / usr / local / lib -llapack -lblas&amp;&amp; ./lapack1
确实会奏效。