这些 LAPACK 计划有何不同之处?一个编译,另一个不编译
What distinguishes these LAPACK programmes? One compiles, the other does not
我有两个程序使用 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 的代码,它只是本文档中的官方 "Example program in C":https://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/dgeev.htm
(由于潜在的版权限制,我不敢post分开)
ludi@ludi-M17xR4:~/Desktop/tests$ gcc -o lapack1 lapack1.c
-L/usr/local/lib -llapack -lblas && ./lapack1
生产
tmp/cciRzQru.o: In function main': lapack1.c:(.text+0xf3): undefined
reference to
dgeev' lapack1.c:(.text+0x1b6): undefined reference to
`dgeev' collect2: error: ld returned 1 exit status
仔细检查您的函数名称。链接器抱怨对函数 dgeev()
的未定义引用。工作代码正在调用一个名为 dgeev_()
.
的不同函数
编译选项-D如下:
ludi@ludi-M17xR4:~/Desktop/tests$ gcc -Ddgeev=dgeev_ -o lapack1
lapack1.c -L/usr/local/lib -llapack -lblas && ./lapack1
确实有用。
我有两个程序使用 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 的代码,它只是本文档中的官方 "Example program in C":https://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/dgeev.htm (由于潜在的版权限制,我不敢post分开)
ludi@ludi-M17xR4:~/Desktop/tests$ gcc -o lapack1 lapack1.c -L/usr/local/lib -llapack -lblas && ./lapack1
生产
tmp/cciRzQru.o: In function
main': lapack1.c:(.text+0xf3): undefined reference to
dgeev' lapack1.c:(.text+0x1b6): undefined reference to `dgeev' collect2: error: ld returned 1 exit status
仔细检查您的函数名称。链接器抱怨对函数 dgeev()
的未定义引用。工作代码正在调用一个名为 dgeev_()
.
编译选项-D如下:
ludi@ludi-M17xR4:~/Desktop/tests$ gcc -Ddgeev=dgeev_ -o lapack1 lapack1.c -L/usr/local/lib -llapack -lblas && ./lapack1
确实有用。