Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linking issues with tensorflow-lite.a with android-studio and CMake. #19642

Closed
uelordi01 opened this issue May 30, 2018 · 20 comments
Closed

Linking issues with tensorflow-lite.a with android-studio and CMake. #19642

uelordi01 opened this issue May 30, 2018 · 20 comments
Assignees
Labels
comp:lite TF Lite related issues stat:awaiting response Status - Awaiting response from author

Comments

@uelordi01
Copy link

System information

  • Have I written custom code:

  • OS Platform and Distribution ( ):
    Linux Ubuntu 16.04 / Android

  • TensorFlow installed from:
    source

  • Python version:
    2.7

  • Bazel version (if compiling from source):
    0.13

  • GCC/Compiler version (if compiling from source):
    NDK16b

Describe the problem

I am having problems linking libtensorflow-lite.a with CMake and Android Studio.
It seems that the tf-lite library is not compatible with my architecture, but I do not know why this is happening.
I have succesfully compiled the libtensorflow-lite.a library, following these steps:

export NDK_ROOT=<your_ndk>
tensorflow/contrib/lite/download_dependencies.sh
make -f tensorflow/contrib/lite/Makefile TARGET=ANDROID ANDROID_ARCH=armeabi-v7a

The problem comes when I try to link the library with CMake. it seems that even I compiled the armeabi-v7a the linker, I have these errors:

incompatible target

the vtable symbol may be undefined because the class is missing its key function

Source code / logs

This my error log:

Build command failed.
Error while executing process /home/VICOMTECH/uelordi/Android/Sdk/cmake/3.6.4111459/bin/cmake with arguments {--build /home/VICOMTECH/uelordi/projects/SwitchTrackingEngine/kanvas_tracker/kt/tools/Deep-learning-tools/android/tfliteTest/app/.externalNativeBuild/cmake/debug/armeabi-v7a --target native-lib}
[1/1] Linking CXX shared library ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so
FAILED: : && /home/VICOMTECH/uelordi/SDK/android-ndk-r16b/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++  --target=armv7-none-linux-androideabi --gcc-toolchain=/home/VICOMTECH/uelordi/SDK/android-ndk-r16b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 --sysroot=/home/VICOMTECH/uelordi/SDK/android-ndk-r16b/sysroot -fPIC -isystem /home/VICOMTECH/uelordi/SDK/android-ndk-r16b/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security  -std=c++11 -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a --sysroot /home/VICOMTECH/uelordi/SDK/android-ndk-r16b/platforms/android-21/arch-arm -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o  /home/VICOMTECH/uelordi/SDK/tensorflow/tensorflow/contrib/lite/gen/lib/libtensorflow-lite.a /home/VICOMTECH/uelordi/SDK/android-ndk-r16b/platforms/android-21/arch-arm/usr/lib/liblog.so -latomic -lm "/home/VICOMTECH/uelordi/SDK/android-ndk-r16b/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/libgnustl_static.a" && :
/home/VICOMTECH/uelordi/SDK/android-ndk-r16b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: /home/VICOMTECH/uelordi/SDK/tensorflow/tensorflow/contrib/lite/gen/lib/libtensorflow-lite.a(error_reporter.o): incompatible target
/home/VICOMTECH/uelordi/SDK/android-ndk-r16b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: /home/VICOMTECH/uelordi/SDK/tensorflow/tensorflow/contrib/lite/gen/lib/libtensorflow-lite.a(interpreter.o): incompatible target
/home/VICOMTECH/uelordi/SDK/android-ndk-r16b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: /home/VICOMTECH/uelordi/SDK/tensorflow/tensorflow/contrib/lite/gen/lib/libtensorflow-lite.a(register.o): incompatible target
/home/VICOMTECH/uelordi/SDK/android-ndk-r16b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: /home/VICOMTECH/uelordi/SDK/tensorflow/tensorflow/contrib/lite/gen/lib/libtensorflow-lite.a(model.o): incompatible target
/home/VICOMTECH/uelordi/SDK/android-ndk-r16b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: /home/VICOMTECH/uelordi/SDK/tensorflow/tensorflow/contrib/lite/gen/lib/libtensorflow-lite.a(op_resolver.o): incompatible target
/home/VICOMTECH/uelordi/projects/SwitchTrackingEngine/kanvas_tracker/kt/tools/Deep-learning-tools/android/tfliteTest/app/src/main/cpp/native-lib.cpp:23: error: undefined reference to 'tflite::DefaultErrorReporter()'
/home/VICOMTECH/uelordi/projects/SwitchTrackingEngine/kanvas_tracker/kt/tools/Deep-learning-tools/android/tfliteTest/app/src/main/cpp/native-lib.cpp:23: error: undefined reference to 'tflite::FlatBufferModel::BuildFromFile(char const*, tflite::ErrorReporter*)'
/home/VICOMTECH/uelordi/projects/SwitchTrackingEngine/kanvas_tracker/kt/tools/Deep-learning-tools/android/tfliteTest/app/src/main/cpp/native-lib.cpp:27: error: undefined reference to 'tflite::ops::builtin::BuiltinOpResolver::BuiltinOpResolver()'
/home/VICOMTECH/uelordi/projects/SwitchTrackingEngine/kanvas_tracker/kt/tools/Deep-learning-tools/android/tfliteTest/app/src/main/cpp/native-lib.cpp:28: error: undefined reference to 'tflite::InterpreterBuilder::InterpreterBuilder(tflite::FlatBufferModel const&, tflite::OpResolver const&)'
/home/VICOMTECH/uelordi/projects/SwitchTrackingEngine/kanvas_tracker/kt/tools/Deep-learning-tools/android/tfliteTest/app/src/main/cpp/native-lib.cpp:30: error: undefined reference to 'tflite::InterpreterBuilder::operator()(std::unique_ptr<tflite::Interpreter, std::default_delete<tflite::Interpreter> >*)'
/home/VICOMTECH/uelordi/SDK/tensorflow/tensorflow/contrib/lite/op_resolver.h:70: error: undefined reference to 'vtable for tflite::MutableOpResolver'
/home/VICOMTECH/uelordi/SDK/android-ndk-r16b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: the vtable symbol may be undefined because the class is missing its key function
/home/VICOMTECH/uelordi/SDK/android-ndk-r16b/sources/cxx-stl/gnu-libstdc++/4.9/include/bits/unique_ptr.h:76: error: undefined reference to 'tflite::FlatBufferModel::~FlatBufferModel()'
/home/VICOMTECH/uelordi/SDK/android-ndk-r16b/sources/cxx-stl/gnu-libstdc++/4.9/include/bits/unique_ptr.h:76: error: undefined reference to 'tflite::Interpreter::~Interpreter()'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

My CmakeScript:

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.
`
SET(PROJECT_NAME  native-lib)
cmake_minimum_required(VERSION 3.4.1)
set(PREBUILT_DIR ${TENSORFLOW_ROOT_DIR}/tensorflow/contrib/lite)

add_library(lib_tflite STATIC IMPORTED )
set_target_properties(lib_tflite PROPERTIES IMPORTED_LOCATION
${PREBUILT_DIR}/gen/lib/libtensorflow-lite.a)

INCLUDE_DIRECTORIES(${TENSORFLOW_ROOT_DIR}
    ${PREBUILT_DIR}/downloads/flatbuffers/include)

add_library( # Sets the name of the library.
            ${PROJECT_NAME}
             SHARED
             src/main/cpp/native-lib.cpp )

find_library( log-lib
              log )


target_link_libraries( # Specifies the target library.
                       ${PROJECT_NAME}
                       lib_tflite
                       ${log-lib} )

My build_gradle (I have only copy the relevant part)

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.androidapp.kt.kanvas.tflitetest"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11"
                arguments "-DTENSORFLOW_ROOT_DIR=" + getTensorflowDir().toString()
                abiFilters  'armeabi-v7a' 
            }
        }
    }
@gargn gargn added the comp:lite TF Lite related issues label May 31, 2018
@reedwm
Copy link
Member

reedwm commented Jun 4, 2018

/CC @aselle

@reedwm reedwm assigned aselle and unassigned reedwm Jun 4, 2018
@tensorflowbutler
Copy link
Member

Nagging Assignee @aselle: It has been 14 days with no activity and this issue has an assignee. Please update the label and/or status accordingly.

@WeiboXu
Copy link

WeiboXu commented Jun 26, 2018

@uelordi01 Have you resolved this issue? I also meet the similar problem.

@uelordi01
Copy link
Author

Unfortunately not, but I think my error is related to the architecture mismatch, actually when I tried to make objdump in the tf-lite static library it says that is x86-64 architecture.

I think this make -f tensorflow/contrib/lite/Makefile TARGET=ANDROID ANDROID_ARCH=armeabi-v7a is it does not really take into account the android architecture.

Furthermore I tried to use this issue explanations:
#14688
to build armv7 target abi with no success.

So it seems that we need an android toolchain to say to gcc or clang compiler the correct compilation flgs.

@tensorflowbutler
Copy link
Member

Nagging Assignee @aselle: It has been 14 days with no activity and this issue has an assignee. Please update the label and/or status accordingly.

@shashishekhar
Copy link
Contributor

@uelordi01 any reason you can use the bazel build?

@shashishekhar shashishekhar added the stat:awaiting response Status - Awaiting response from author label Jul 17, 2018
@shashishekhar
Copy link
Contributor

@uelordi01 : Let me know if you are still having issues, reopen the issue.

@zhyj3038
Copy link

hi, have you solved the "incompatible target" problem ?

@uelordi01
Copy link
Author

Currently I am able to compile the tensorflow-lite.a through bazel, and also I can build shared lib adding in tese lines to build_file:

cc_binary(
name = "libtensorflowLite.so",

linkopts=["-shared", "-Wl"],
linkshared=1,

copts = tflite_copts(),
deps = [
    ":framework",
    "//tensorflow/contrib/lite/kernels:builtin_ops",
],

@zhyj3038
Copy link

@uelordi01 hi, follow your instruction, I get a libtensorflowLite.so and run it on mobile phone. But the size is nearly 37M, is it normal ?

@uelordi01
Copy link
Author

uelordi01 commented Oct 16, 2018

My tensorflow-lite library size is 34.7 MB, so I guess depending on the version of the tensorflow-lite is the reason that you have 37 MB size library.
Probably if you link a static library, you will reduce your final library size. But I couln't compile for android ndk, and now I am not on it.

@zhyj3038
Copy link

@uelordi01 yes, now I have integrate th dynamic library in my android project.

@holyhao
Copy link

holyhao commented Nov 9, 2018

@zhyj3038 Hi, how can you use the libtensorflowLite.so in an android project? which head files do you include ?

@zhyj3038
Copy link

zhyj3038 commented Nov 9, 2018

@holyhao the whole tflite folder

@holyhao
Copy link

holyhao commented Nov 9, 2018

@zhyj3038 Thans for your reply, i have some trouble about compiling to the dynamic library. If any possible, would you please share it ?

@zhyj3038
Copy link

zhyj3038 commented Nov 9, 2018

just add

cc_binary(
name = "libtensorflowLite.so",

linkopts=["-shared", "-Wl"],
linkshared=1,

copts = tflite_copts(),
deps = [
":framework",
"//tensorflow/contrib/lite/kernels:builtin_ops",
],

to any positon of tensorflow's BUILD file. the BUILD file is in tensorflow's root folder

@holyhao
Copy link

holyhao commented Nov 9, 2018

@zhyj3038 Because of some proxy problem , i failed compiling with bazel .
The following are the error message, do you have some idea about this?:
Encountered error while reading extension file 'closure/defs.bzl': no such package '@io_bazel_rules_closure//closure': Error downloading [https://mirror.bazel.build/github.com/bazelbuild/rules_closure/archive/dbb96841cc0a5fb2664c37822803b06dab20c7d1.tar.gz, https://github.com/bazelbuild/rules_closure/archive/dbb96841cc0a5fb2664c37822803b06dab20c7d1.tar.gz] to /home/haolei/.cache/bazel/_bazel_haolei/3a2d9699e4c676169595171b9b4d83da/external/io_bazel_rules_closure/dbb96841cc0a5fb2664c37822803b06dab20c7d1.tar.gz: All mirrors are down: []

@holyhao
Copy link

holyhao commented Nov 20, 2018

@uelordi01 hi, I get a libtensorflowLite.so sucessfully, But the size is almost 40MB. I know that the offical libtensorflowlite_jni.so only 2.04MB, how could it be so different, do you have some idea?

@uelordi01
Copy link
Author

I cannot assure, but this can be due to the lite jni library could work as a wrapper to make calls to tensorflow-lite core functions, so in concluision, probably in this library are placed only wrapper functions.

Another idea could be because the jni lit library linking could be done through static library where this library would have had only the operations that the jni library.
However, I think is the first option.

@Timshel
Copy link

Timshel commented Nov 27, 2018

@uelordi01, hi, I have the same problem when using make the resulting library appear to be for x86-64.
On 19 Sep You said :

Currently I am able to compile the tensorflow-lite.a through bazel

Would you mind sharing this ? :) (I'm able to build a shared lib but due to external limitations I need a static lib).

tensorflow-copybara pushed a commit that referenced this issue Feb 5, 2019
This target exports the interpreter C++ API and the kernel C API, and
includes the builtin set of ops.

Fixes #25278 and fixes #19642.

PiperOrigin-RevId: 232396927
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:lite TF Lite related issues stat:awaiting response Status - Awaiting response from author
Projects
None yet
Development

No branches or pull requests

10 participants