NDK 未定义对 {extern "C" function} 的引用?
NDK undefined reference to {extern "C" function}?
我试图开发一个Android应用程序。我已经设法使用NDK实现了一个相当小的c++函数。但现在我想使用我在谷歌文档上找到的一个相当大的c++库。当我现在尝试用ndk构建工具构建它时,我在cygwin控制台中得到了以下错误:
Compile++ thumb : ndkfoo <= subspace.cpp
Compile++ thumb : ndkfoo <= classifier.cpp
Compile++ thumb : ndkfoo <= eigen.cpp
Compile++ thumb : ndkfoo <= image.cpp
Compile++ thumb : ndkfoo <= imageio.cpp
Compile++ thumb : ndkfoo <= local.cpp
Compile++ thumb : ndkfoo <= matrix.cpp
Compile++ thumb : ndkfoo <= sample.cpp
SharedLibrary : libndkfoo.so
./obj/local/armeabi/objs/ndkfoo/eigen.o: In function `LibSubspace::geneigen(doub
le*, double*, int, double*, int, bool)':
U:workspacetest/jni/eigen.cpp:91: undefined reference to `ilaenv_'
U:workspacetest/jni/eigen.cpp:96: undefined reference to `dsygv_'
U:workspacetest/jni/eigen.cpp:128: undefined reference to `dggev_'
./obj/local/armeabi/objs/ndkfoo/eigen.o: In function `LibSubspace::eigen(double*
, double*, double*, int, bool)':
U:workspacetest/jni/eigen.cpp:55: undefined reference to `ilaenv_'
U:workspacetest/jni/eigen.cpp:58: undefined reference to `dsyev_'
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi/libndkfoo.so] Error 1
此功能的代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include <assert.h>
#include <string.h>
#include "eigen.h"
extern "C" int ilaenv_(int *ispec, const char *name__, const char *opts,
int *n1, int *n2, int *n3, int *n4, int name_len, int opts_len);
extern "C" int dsyev_(const char *jobz, const char *uplo, int *n, double *a,
int *lda, double *w, double *work, int *lwork,
int *info);
extern "C" int dsygv_(int *itype, const char *jobz, const char *uplo, int *
n, double *a, int *lda, double *b, int *ldb,
double *w, double *work, int *lwork, int *info);
extern "C" int dggev_(const char *jobvl, const char *jobvr, int *n, double *
a, int *lda, double *b, int *ldb, double *alphar,
double *alphai, double *beta, double *vl, int *ldvl,
double *vr, int *ldvr, double *work, int *lwork,
int *info);
namespace LibSubspace {
int eigen(double* A, double* V, double* E, int n, bool verbose) {
int info;
int ispec = 1;
int lwork;
lwork = (ilaenv_(&ispec,"DSYEV","U",&n,&n,&n,&n,5,1)+2)*n;
double *work = (double *)malloc(lwork*sizeof(double));
memcpy(V,A,n*n*sizeof(double));
dsyev_("V","U",&n,V,&n,E,work,&lwork,&info);
free(work);
//check the return value
if(info!=0) {
if(verbose) {
printf("Error computing eigenvectors: ");
if(info>0) {
printf("Algorithm failed to convergen");
} else if(info<0) {
printf("Illegal argumentn");
} else {
printf("Unknown errorn");
}
}
return 0;
}
return 1;
}
int geneigen(double *A, double *B, int n, double *W, int algorithm,
bool verbose) {
//getting the optimal lwork
int ispec = 1;
int lwork;
int info;
switch(algorithm) {
case EIGEN_CHOL:
{
//computing eigenvectors
lwork = (ilaenv_(&ispec,"DSYGV","U",&n,&n,&n,&n,5,1)+2)*n;
double *work = (double *)malloc(lwork*sizeof(double));
int problemType = 1;
char job = 'V';
char uplo = 'U';
dsygv_(&problemType,&job,&uplo,&n,A,&n,B,&n,W,work,&lwork,&info);
free(work);
//check the return value
if(info!=0) {
if(verbose) {
printf("Error computing eigenvectors: ");
if(info>n) {
printf("Matrix B is not positive definiten");
} else if(info<=n) {
printf("The problem failed to convergen");
} else if(info<0) {
printf("Illegal argumentn");
} else {
printf("Unknown errorn");
}
}
return 0;
}
}
break;
case EIGEN_QZ:
{
//more general algorithm
double *alphar = (double *)malloc(n*sizeof(double));
double *alphai = (double *)malloc(n*sizeof(double));
double *beta = (double *)malloc(n*sizeof(double));
double *VR = (double *)malloc(n*n*sizeof(double));
lwork = 8*n;
double *work = (double *)malloc(lwork*sizeof(double));
dggev_("N","V",&n,A,&n,B,&n,alphar,alphai,beta,
NULL,&n,VR,&n,work,&lwork,&info);
//eigenvalues
for(long i=0;i<n;i++) {
if(beta[i]!=0) {
W[i] = alphar[i]/beta[i];
} else W[i] = 0;
}
//eigenvectors
for(long i=0;i<n;i++) {
for(long j=0;j<n;j++) {
A[i*n+j] = VR[i*n+j];
}
}
free(alphar);
free(alphai);
free(beta);
free(VR);
free(work);
if(info!=0) {
printf("Error computing eigenvectors: ");
if(info<0) {
printf("Illegal argumentn");
} else if(info<=n) {
printf("QZ iteration failedn");
} else {
printf("Unknown errorn");
}
return 0;
}
}
break;
}
return 1;
}
} //namespace
不幸的是,我完全不明白为什么会出现这些错误。我对C++不太熟悉,这就是为什么我想使用NDK,希望不必更改现有库的任何内容。
Google告诉我,这些函数来自Fortran-to-C编译器的运行时。因此,您需要链接到一个名为libf2c
的库,或者找到源代码并将其作为NDK项目的一部分。后者听起来是一种更有前景的方法——我认为不存在现成的NDK构建。
相关文章:
- C++初始化 std::function 时如何将占位符绑定到引用/引用参数?
- VSCode - C++对"CLASS::FUNCTION"的未定义引用
- 在 C++ 中对"Class::function()"错误的未定义引用 [CLion IDE]
- std::function 中不允许引用返回类型吗?
- 对 Class::Class/Function 的未定义引用(OOP 中的初学者)
- 在将代码放入 C 语言时对"function()"的未定义引用C++
- 为什么通过引用传递从 std::function 派生的对象会使程序崩溃?
- 解决方法:未定义对"class::function"的引用
- C :将对象引用作为参数给出错误:呼叫class :: function()的匹配函数
- 在类中引用函数时的"No Matching Function for Call"
- C++:对 class::function() 的未定义引用
- 对 class::function 的未定义引用
- 对无效"function" C++的未定义引用
- Qt 5 C++ 对 Class::function(QString, Qstring, ..) 的未定义引用
- 为什么 std::function 在签名中接受此引用
- 设置 std::function 变量以引用 std::sin 函数
- 对[class::function]的未定义引用/对[class]的typeinfo的未定义参考
- 对'Class::Class(function)'的未定义引用
- 对 CLASS::function() 的未定义引用
- 对"function name from external library"的未定义引用