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
...