将TFLite C API包含在使用C ++和Java的Android应用中时,会产生问题

问题描述

我正在通过C ++使用TFLite与NDK一起构建我的应用程序,希望能帮助您解决链接错误。我遵循的指南是https://www.tensorflow.org/lite/guide/android#use_tflite_c_api

我已经在程序中包含了TFLite C API头文件和动态共享库,它们是使用CMake构建的。

我收到以下错误

../../../../src/main/cpp/TFLiteExample.cpp:17: error: undefined reference to 'TfLiteModelCreateFromFile'

一些其他信息:我在app/src/main/includes包括了头文件,在.so包括app/src/main/jni动态库。 CMakeLists.txt文件位于app/src/main中。以下是CMakeLists.txt的相关组件:

cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
set(CMAKE_VERBOSE_MAKEFILE ON)

###Internal Linking

#Add internal C++ libraries
add_library( native-lib SHARED src/main/cpp/native-lib.cpp )
add_library( TFLiteExample SHARED src/main/cpp/TFLiteExample.cpp)

add_library( tensorflowlite_jni SHARED IMPORTED )
set_property(TARGET tensorflowlite_jni
    PROPERTY IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jni/libtensorflowlite_jni.so)

#Make directories visible to all C++ files
include_directories(src/main/includes)
include_directories(src/main/cpp)
include_directories(src/main/jni)

# Specify the libraries which our native library is dependent on,including Oboe
target_link_libraries (native-lib tensorflowlite_jni TFLiteExample)

target_link_libraries(TFLiteExample tensorflowlite_jni)

这里是TFLiteExample.h

#ifndef TEST2_TFLITEEXAMPLE_H
#define TEST2_TFLITEEXAMPLE_H

// TFLite include files

#include <builtin_ops.h>
#include <c_api.h>
#include <c_api_experimental.h>
#include <common.h>


class TFLiteExample {
    public:
    TFLiteExample();
    static void TFLitePredict();
};


#endif //TEST2_TFLITEEXAMPLE_H

这里是TFLiteExample.cpp

#include "TFLiteExample.h"

TFLiteExample::TFLiteExample(){
}

void TFLiteExample::TFLitePredict() {
TfLiteModel* model = TfLiteModelCreateFromFile("/src/main/assets/_quant.tflite");
}

标头common.hc_api.h都是Tensorflow的代码。除了将诸如#include "../../../../../../../../../../../Library/Android/sdk/ndk/20.0.5594570/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/c++/v1/stdbool.h"之类的相对包含路径替换为#include <cstdbool>之外,我没有修改代码

common以以下内容开头:

#ifndef TENSORFLOW_LITE_C_COMMON_H_
#define TENSORFLOW_LITE_C_COMMON_H_

#include <cstdbool>
#include <stddef>
#include <stdint>

#ifdef __cplusplus
extern "C" {
#endif  // __cplusplus
...

c_api一起使用:

#ifndef TENSORFLOW_LITE_C_C_API_H_
#define TENSORFLOW_LITE_C_C_API_H_

#include <stdarg>
#include <stdint>

#include "common.h"

#ifdef __cplusplus
extern "C" {
#endif  // __cplusplus
...

解决方法

更新:按照以下说明成功构建了程序: https://stackoverflow.com/a/63372486/4008884

我进行了以下修改:

首先,我替换了文件结构

app/src/main/jni/libtensorflowlite_jni.so

使用

app/src/main/jni/arm64-v8a/libtensorflowlite_jni.so
app/src/main/jni/armeabi-v7a/libtensorflowlite_jni.so
app/src/main/jni/x86_64/libtensorflowlite_jni.so
app/src/main/jni/x86/libtensorflowlite_jni.so

第二,我将TFLite C API标头移到了同一目录app/src/main/jni/

第三,我在build.gradle部分的android {中添加了以下内容:

ndk {
    abiFilters 'x86','x86_64','armeabi-v7a','arm64-v8a'
}

最后,我将${ANDROID_ABI}添加到CMakeLists.txt中的链接:

set(JNI_DIR ${PROJECT_SOURCE_DIR}/src/main/jni)
add_library( tensorflowlite_jni SHARED IMPORTED)

set_target_properties(tensorflowlite_jni
        PROPERTIES IMPORTED_LOCATION
        ${JNI_DIR}/${ANDROID_ABI}/libtensorflowlite_jni.so)

include_directories( ${JNI_DIR} )

target_link_libraries(native-lib tensorflowlite_jni)
,

我的功能有问题 显示的错误是undefined reference to TfLiteModelCreateFromFile;我想在 C 程序中为 TensorFlow 使用 API C。这是我的程序

int32_t main(int32_t argc,char *argv[])   
{ 
  TfLiteModel* model = TfLiteModelCreateFromFile("iris.tflite");
  TfLiteInterpreterOptions*options =TfLiteInterpreterOptionsCreate();
}