cygwin下LAPACK链接错误
Linking error with LAPACK under cygwin
与this question中描述的问题类似,我想在Windows 8.1下使用LAPACK编译一个C++程序,使用cygwin(并用NetBeans编写)。我在cygwin中得到了必要的包(lapack、liblapack-devel、liblapack0),并写了一个函数原型:
extern "C" void dgeev_(char* jobvl, char* jobvr, int* n, double* a,
int* lda, double* wr, double* wi, double* vl, int* ldvl,
double* vr, int* ldvr, double* work, int* lwork, int* info);
但是尝试编译仍然会从链接器中抛出错误:
$ g++ -llapack -lblas main.cpp
/tmp/cc3opfN5.o:main.cpp:(.text+0x349): undefined reference to `dgeev_'
/tmp/cc3opfN5.o:main.cpp:(.text+0x403): undefined reference to `dgeev_'
collect2: error: ld returned 1 exit status
代码如下所示(改编自 this example):
/* Locals */
int n = N, lda = LDA, ldvl = LDVL, ldvr = LDVR, info, lwork;
double wkopt;
double* work;
double h = k_max / N;
/* Local arrays */
double wr[N], wi[N], vl[LDVL * N], vr[LDVR * N];
double a[LDA * N];
/* Initialize matrix */
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
a[i * N + j] = integral(i + 1, j + 1);
if (i == j) {
a[i * N + j] += diagonal((i + 1) * h);
}
}
}
/* Executable statements */
printf(" DGEEV Example Program Results\n");
/* Query and allocate the optimal workspace */
lwork = -1;
char vec[] = "Vectors";
dgeev_(vec, vec, &n, a, &lda, wr, wi, vl, &ldvl, vr, &ldvr,
&wkopt, &lwork, &info);
lwork = (int) wkopt;
work = (double*) malloc(lwork * sizeof (double));
/* Solve eigenproblem */
dgeev_(vec, vec, &n, a, &lda, wr, wi, vl, &ldvl, vr, &ldvr,
work, &lwork, &info);
我错过了什么? :-/
假设你已经正确安装了BLAS
和LAPACK
库,并且系统可以定位到它们,那么你只需要在编译命令中改变顺序即可。
$ g++ main.cpp -llapack -lblas
编译器应该知道 main.cpp
的哪些函数在声明它们之前应该查看库。
与this question中描述的问题类似,我想在Windows 8.1下使用LAPACK编译一个C++程序,使用cygwin(并用NetBeans编写)。我在cygwin中得到了必要的包(lapack、liblapack-devel、liblapack0),并写了一个函数原型:
extern "C" void dgeev_(char* jobvl, char* jobvr, int* n, double* a,
int* lda, double* wr, double* wi, double* vl, int* ldvl,
double* vr, int* ldvr, double* work, int* lwork, int* info);
但是尝试编译仍然会从链接器中抛出错误:
$ g++ -llapack -lblas main.cpp
/tmp/cc3opfN5.o:main.cpp:(.text+0x349): undefined reference to `dgeev_'
/tmp/cc3opfN5.o:main.cpp:(.text+0x403): undefined reference to `dgeev_'
collect2: error: ld returned 1 exit status
代码如下所示(改编自 this example):
/* Locals */
int n = N, lda = LDA, ldvl = LDVL, ldvr = LDVR, info, lwork;
double wkopt;
double* work;
double h = k_max / N;
/* Local arrays */
double wr[N], wi[N], vl[LDVL * N], vr[LDVR * N];
double a[LDA * N];
/* Initialize matrix */
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
a[i * N + j] = integral(i + 1, j + 1);
if (i == j) {
a[i * N + j] += diagonal((i + 1) * h);
}
}
}
/* Executable statements */
printf(" DGEEV Example Program Results\n");
/* Query and allocate the optimal workspace */
lwork = -1;
char vec[] = "Vectors";
dgeev_(vec, vec, &n, a, &lda, wr, wi, vl, &ldvl, vr, &ldvr,
&wkopt, &lwork, &info);
lwork = (int) wkopt;
work = (double*) malloc(lwork * sizeof (double));
/* Solve eigenproblem */
dgeev_(vec, vec, &n, a, &lda, wr, wi, vl, &ldvl, vr, &ldvr,
work, &lwork, &info);
我错过了什么? :-/
假设你已经正确安装了BLAS
和LAPACK
库,并且系统可以定位到它们,那么你只需要在编译命令中改变顺序即可。
$ g++ main.cpp -llapack -lblas
编译器应该知道 main.cpp
的哪些函数在声明它们之前应该查看库。