gpt4 book ai didi

Android studio 1.3 NDK 无法解析 c++ header /代码

转载 作者:行者123 更新时间:2023-11-28 06:03:35 25 4
gpt4 key购买 nike

我是 Android Studio 1.3.2 的新手,使用 NDK 更是如此。我正在尝试使用 OpenCV 2.4.11 和 C++。我正在使用 Gradle 2.6。

奇怪的是,到目前为止,OpenCV 没有在 C++ 代码中生成任何错误。就在我包含像 or 这样的 C++ header 时,它无法解析它们。 Unresolved Try...Catch 也是如此。同样,我在 2 周前开始使用 Android Studio,1 周前开始使用 NDK。我仍然不确定自己在做什么。

#include "jni.h"
#include <vector>
#include <string>
#include <opencv2/contrib/contrib.hpp>

#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT jlong JNICALL Java_face_rt_facert_FisherFaceRecognizer_createFisherFaceRecognizer_10(JNIEnv *env,
jclass type) {

try {
cv::Ptr<cv::FaceRecognizer> pfr = cv::createFisherFaceRecognizer();
pfr.addref(); // this is for the 2.4 branch, 3.0 would need a different treatment here
return (jlong) pfr.obj;
} catch (...) {
jclass je = env->FindClass("java/lang/Exception");
env->ThrowNew(je, "sorry, dave..");
}
return 0;
}

#ifdef __cplusplus
}
#endif

项目的 build.gradle 文件:

apply plugin: 'com.android.model.application'

model {
android {
compileSdkVersion = 23
buildToolsVersion = "23.0.1"

defaultConfig.with {
applicationId = "face.rt.facert"
minSdkVersion.apiLevel = 19
targetSdkVersion.apiLevel = 19
versionCode = 1
versionName = "1.0.1"
}

}

android.buildTypes {
release {
minifyEnabled = false
proguardFiles += file('proguard-rules.txt')
}
}

android.ndk { // keeping it to make AS correctly support C++ code editing and debugging
moduleName = "Face"
ldLibs += ['log']
cppFlags += "-std=c++11"
cppFlags += "-fexceptions"
cppFlags += "-I${file("src/main/jni/prebuilts/include")}".toString()
cppFlags += "-I${file("C:/Android-dev/SDKs/OpenCV-android-sdk/sdk/native/jni/include")}".toString()
cppFlags += "-I${file("C:/Android-dev/SDKs/OpenCV-android-sdk/sdk/native/jni/include/opencv")}".toString()
cppFlags += "-I${file("C:/Android-dev/SDKs/OpenCV-android-sdk/sdk/native/jni/include/opencv2")}".toString()
stl = "gnustl_shared" //"stlport_static"
}


android.productFlavors {
// for detailed abiFilter descriptions, refer to "Supported ABIs" @
// https://developer.android.com/ndk/guides/abis.html#sa
create("arm") {
ndk.with {
abiFilters += "armeabi"

File curDir = file('./')
curDir = file(curDir.absolutePath)
String libsDir = curDir.absolutePath+"\\src\\main\\jniLibs\\armeabi\\" //"-L" +

ldLibs += libsDir + "libnative_camera_r4.3.0.so"
ldLibs += libsDir + "libopencv_contrib.a"
ldLibs += libsDir + "libopencv_core.a"
ldLibs += libsDir + "libopencv_highgui.a"
ldLibs += libsDir + "libopencv_imgproc.a"
ldLibs += libsDir + "libopencv_info.so"
ldLibs += libsDir + "libopencv_java.so"
ldLibs += libsDir + "libopencv_legacy.a"
ldLibs += libsDir + "libopencv_ml.a"
ldLibs += libsDir + "libopencv_ts.a"

}
}
create("armv7") {
ndk.with {
abiFilters += "armeabi-v7a"

File curDir = file('./')
curDir = file(curDir.absolutePath)
String libsDir = curDir.absolutePath+"\\src\\main\\jniLibs\\armeabi-v7a\\" //"-L" +

ldLibs += libsDir + "libnative_camera_r4.3.0.so"
ldLibs += libsDir + "libopencv_contrib.a"
ldLibs += libsDir + "libopencv_core.a"
ldLibs += libsDir + "libopencv_highgui.a"
ldLibs += libsDir + "libopencv_imgproc.a"
ldLibs += libsDir + "libopencv_info.so"
ldLibs += libsDir + "libopencv_java.so"
ldLibs += libsDir + "libopencv_legacy.a"
ldLibs += libsDir + "libopencv_ml.a"
ldLibs += libsDir + "libopencv_ts.a"

}
}
create("x86") {
ndk.with {
abiFilters += "x86"
}
}
create("mips") {
ndk.with {
abiFilters += "mips"
}
}
create("fat") {

}

// To include all cpu architectures, leaves abiFilters empty
create("all")
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.0.1'
compile project(':openCVLibrary2411')
}

这是我在项目中的依赖项:

classpath 'com.android.tools.build:gradle-experimental:0.3.0-alpha3'

编辑:

出于测试目的,从函数中删除了 Try...Catch。奇怪的是, vector 和字符串 header 现在已解析并正常工作。

但现在我在编译时遇到了一些关于 OpenCV 的错误。

Error:(16) undefined reference to `cv::createFisherFaceRecognizer(int, double)'

Error:(2607) undefined reference to `cv::fastFree(void*)'

Error:Execution failed for task ':app:linkArm64-v8aDebugAllFaceSharedLibrary'. A build operation failed. Linker failed while linking libFace.so.

最佳答案

我在我的 build.gradle 中发现:

    // To include all cpu architectures, leaves abiFilters empty
create("all")

all 表示 32 位和 64 位。由于我的库只有 32 位,编译器找不到 64 位版本并失败。

我删除了这一行并且效果更好(仍然有一些错误但不是编译错误)

关于Android studio 1.3 NDK 无法解析 c++ header /代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32816265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com