0

I am using TFLite through C++ building my app with the NDK and would appreciate help in addressing a linkage error. The guide I'm following is https://www.tensorflow.org/lite/guide/android#use_tflite_c_api

I have included the TFLite C API header files and dynamic shared library in my program, which I build using CMake.

I get the following error:

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

Some additional information: I included the header files in app/src/main/includes and the .so dynamic library in app/src/main/jni. The CMakeLists.txt file is in app/src/main. Below are the relevant components of 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)

Here is 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

Here is TFLiteExample.cpp:

#include "TFLiteExample.h"

TFLiteExample::TFLiteExample(){
}

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

Headers common.h and c_api.h are both code from Tensorflow. I haven't modified the code except for replacing relative include paths such as #include "../../../../../../../../../../../Library/Android/sdk/ndk/20.0.5594570/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/c++/v1/stdbool.h" with #include <cstdbool>.

common starts with the following includes:

#ifndef TENSORFLOW_LITE_C_COMMON_H_
#define TENSORFLOW_LITE_C_COMMON_H_

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

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

And c_api with:

#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
...
FFT
  • 929
  • 8
  • 17
  • "The C header files include the C++ standard headers using relative paths" - Almost in any case this is a **wrong way** for overcome constraints. If standard header files are not accessible in a "short" way, then they shouldn't be used at all. – Tsyvarev Aug 21 '20 at 21:09
  • @Tsyvarev I replaced the relative path includes with short includes `#include `. Now, the program is instead unable to read the TFLite C API: `../../../../src/main/cpp/TFLiteExample.cpp:39: error: undefined reference to 'TfLiteModelDelete'` – FFT Aug 21 '20 at 21:29
  • "Now, the program is instead unable to read the TFLite C API" - No, the program has successfully included the *headers*. "undefined reference" is a error of the **linking** stage, which comes after *compiling*. Please, actualize the code and the error message in the question post. – Tsyvarev Aug 21 '20 at 21:55
  • Thanks @Tsyvarev. I just updated the post. – FFT Aug 21 '20 at 22:18

2 Answers2

1

Update: the program was built successfully following these instructions: https://stackoverflow.com/a/63372486/4008884.

I made the following modifications:

First, I replaced file structure

app/src/main/jni/libtensorflowlite_jni.so

with

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

Second, I moved the TFLite C API headers to the same directory, app/src/main/jni/.

Third, I included the following to the build.gradle in the android { section:

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

Finally, I added ${ANDROID_ABI} to the linking in 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)
FFT
  • 929
  • 8
  • 17
-1

I have a problem with the function the displayed error is undefined reference to TfLiteModelCreateFromFile; I want to use API C for TensorFlow in a program in C. this is my Program

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