1

I'm making a project with 2 variations, a desktop version and an android version. Both of them are using the same c++ code base. I want to use TFLite without getting into any android configuration like gradle or an aar file. I just want a pure .so file that my c++ can use thus android can through ndk. I know there are some complications like ndk using clang and requiring arm64 architecture, but I think it should be possible. Note that my project requires ndk version 22 and above.

Here is how I configured tflite for my desktop/debian project:

  1. On ~/Desktop/ I ran git clone https://github.com/tensorflow/tensorflow.git tensorflow_src
  2. mkdir tflite_build & cd ~/Desktop/tflite_build
  3. cmake ../tensorflow_src/tensorflow/lite
  4. cmake --build . I've removed the -J flag regardless of what the docs says because it causes my pc to freeze.
  5. mkdir ~/Desktop/tf_test & cd ~/Desktop/tf_test
  6. Create a CMakeLists.txt and a main.cpp file inside tf_testdirectory.
  7. Put the main code from the minimal example on the github repo provided above then this code in CMake:
cmake_minimum_required(VERSION 3.16)
project(minimal C CXX)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTFLITE_DISABLE_TELEMETRY=1")

set(TENSORFLOW_SOURCE_DIR "" CACHE PATH
  "Directory that contains the TensorFlow project" )
if(NOT TENSORFLOW_SOURCE_DIR)
  get_filename_component(TENSORFLOW_SOURCE_DIR
    "/home/user/Desktop/tensorflow_src" ABSOLUTE)
endif()

add_subdirectory(
  "${TENSORFLOW_SOURCE_DIR}/tensorflow/lite"
  "${CMAKE_CURRENT_BINARY_DIR}/tensorflow-lite" EXCLUDE_FROM_ALL)

add_executable(minimal minimal.cc)
target_link_libraries(minimal tensorflow-lite)
  1. Created the folder tf_Test/build and ran cmake .. inside it.
  2. After cmake is completed I run make inside the build directory

This works just fine on debian. However when I try to add this to my cmake on my android ndk project, connect my tablet and run the project I get all these errors:

...
- Looking for strtoull_l - found
-- Using toolchain file: /home/user/Android/Sdk/ndk/25.0.8775105/build/cmake/android.toolchain.cmake.
-- CMAKE_CXX_FLAGS: -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security  -frtti -fexceptions -fvisibility=hidden -std=c++17 -O0 -Wall -Wextra -Wshadow -Wnon-virtual-dtor -pthread -frtti -fexceptions
-- Found Python3: /usr/bin/python3.9 (found suitable version "3.9.2", minimum required is "3.5") found components: Interpreter 
-- Downloading pthreadpool to /home/user/Desktop/native/examples/android/app/.cxx/Debug/5c1w1z3w/arm64-v8a/pthreadpool-source (define PTHREADPOOL_SOURCE_DIR to avoid it)
-- Configuring incomplete, errors occurred!
See also "/home/user/Desktop/native/examples/android/app/.cxx/Debug/5c1w1z3w/arm64-v8a/pthreadpool-download/CMakeFiles/CMakeOutput.log".
-- The ASM compiler identification is Clang with GNU-like command-line
-- Found assembler: /home/user/Android/Sdk/ndk/25.0.8775105/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
-- Downloading FP16 to /home/user/Desktop/native/examples/android/app/.cxx/Debug/5c1w1z3w/arm64-v8a/FP16-source (define FP16_SOURCE_DIR to avoid it)
-- Configuring incomplete, errors occurred!
See also "/home/user/Desktop/native/examples/android/app/.cxx/Debug/5c1w1z3w/arm64-v8a/FP16-download/CMakeFiles/CMakeOutput.log".
-- Downloading FXdiv to /home/user/Desktop/native/examples/android/app/.cxx/Debug/5c1w1z3w/arm64-v8a/FXdiv-source (define FXDIV_SOURCE_DIR to avoid it)
-- Configuring incomplete, errors occurred!
See also "/home/user/Desktop/native/examples/android/app/.cxx/Debug/5c1w1z3w/arm64-v8a/FXdiv-download/CMakeFiles/CMakeOutput.log".
-- RUY is enabled.
-- Configuring incomplete, errors occurred!
See also "/home/user/Desktop/native/examples/android/app/.cxx/Debug/5c1w1z3w/arm64-v8a/CMakeFiles/CMakeOutput.log".
See also "/home/user/Desktop/native/examples/android/app/.cxx/Debug/5c1w1z3w/arm64-v8a/CMakeFiles/CMakeError.log".

CMake Warning at /home/user/Desktop/native/examples/android/app/.cxx/Debug/5c1w1z3w/arm64-v8a/abseil-cpp/CMakeLists.txt:70 (message):
  A future Abseil release will default ABSL_PROPAGATE_CXX_STD to ON for CMake
  3.8 and up.  We recommend enabling this option to ensure your project still
  builds correctly.


CMake Error: CMake was unable to find a build program corresponding to "Ninja".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool.
No such file or directory
CMake Error: Generator: execution of make failed. Make command was:  && 
CMake Error at /home/user/Desktop/official_stuff/tensorflow_src/tensorflow/lite/CMakeLists.txt:167 (add_subdirectory):
  add_subdirectory given source
  "/home/user/Desktop/native/examples/android/app/.cxx/Debug/5c1w1z3w/arm64-v8a/pthreadpool-source"
  which is not an existing directory.


CMake Error: CMake was unable to find a build program corresponding to "Ninja".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool.
No such file or directory
CMake Error: Generator: execution of make failed. Make command was:  && 
CMake Error: CMake was unable to find a build program corresponding to "Ninja".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool.
No such file or directory
CMake Error: Generator: execution of make failed. Make command was:  && 
CMake Error at /home/user/Desktop/native/examples/android/app/.cxx/Debug/5c1w1z3w/arm64-v8a/xnnpack/CMakeLists.txt:844 (ADD_SUBDIRECTORY):
  ADD_SUBDIRECTORY given source
  "/home/user/Desktop/native/examples/android/app/.cxx/Debug/5c1w1z3w/arm64-v8a/pthreadpool-source"
  which is not an existing directory.


CMake Error at /home/user/Desktop/native/examples/android/app/.cxx/Debug/5c1w1z3w/arm64-v8a/xnnpack/CMakeLists.txt:884 (ADD_SUBDIRECTORY):
  ADD_SUBDIRECTORY given source
  "/home/user/Desktop/native/examples/android/app/.cxx/Debug/5c1w1z3w/arm64-v8a/FXdiv-source"
  which is not an existing directory.


CMake Error at /home/user/Desktop/native/examples/android/app/.cxx/Debug/5c1w1z3w/arm64-v8a/xnnpack/CMakeLists.txt:908 (ADD_SUBDIRECTORY):
  ADD_SUBDIRECTORY given source
  "/home/user/Desktop/native/examples/android/app/.cxx/Debug/5c1w1z3w/arm64-v8a/FP16-source"
  which is not an existing directory.

What's the right way to approach this? Following the android build documentation on tflite's website causes a lot of problems.

Update: Installing ninja fixed almost all my problems, the only error I have now is:

In file included from /home/user/Desktop/official_stuff/tensorflow_src/tensorflow/lite/python/interpreter_wrapper/python_error_reporter.cc:16:
/home/user/Desktop/official_stuff/tensorflow_src/tensorflow/lite/python/interpreter_wrapper/python_error_reporter.h:19:10: fatal error: 'Python.h' file not found

python3 is in fact installed on my system, I used it on a c code before.

Update: Adding this to top of my cmake solved the python issue:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/usr/include/python3.9 -I/usr/include/python3.9  -Wno-unused-result -Wsign-compare -g -ffile-prefix-map=/build/python3.9-RNBry6/python3.9-3.9.2=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector -Wformat -Werror=format-security  -DNDEBUG -g -fwrapv -Wall")

Update: Now I get:

In file included from /home/user/Desktop/SmartAlpha/official_stuff/tensorflow_src/tensorflow/lite/python/interpreter_wrapper/numpy.cc:17:
In file included from /home/user/Desktop/SmartAlpha/official_stuff/tensorflow_src/tensorflow/lite/python/interpreter_wrapper/numpy.h:49:
In file included from /usr/include/python3.9/Python.h:8:
/usr/include/python3.9/pyconfig.h:9:12: fatal error: 'aarch64-linux-gnu/python3.9/pyconfig.h' file not found
Turgut
  • 711
  • 3
  • 25
  • 1
    ***CMake Error: CMake was unable to find a build program corresponding to "Ninja".*** Should be something you can easily fix. – drescherjm Feb 02 '23 at 13:18
  • 1
    Also I've mentioned this already in your previous post, but `-DTFLITE_DISABLE_TELEMETRY=1` does nothing as this define is no where to be found in the `TFLITE` repo – Milan Š. Feb 02 '23 at 13:21
  • @MilanŠ. you are right, I just forgot to delete it, I copy and paste the code. – Turgut Feb 02 '23 at 13:27
  • @Turgut the new error is most likely related to the fact that when building for Android you don't have a python interpreter/python development packages. Refer to the TFLite official documentation regarding this. – Milan Š. Feb 02 '23 at 13:37
  • @MilanŠ. Can you provide a link please? – Turgut Feb 02 '23 at 13:39
  • @Turgut I'm not a TFlite expert, I will update my answer with a possible solution, but the rest is up to you to figure out really. – Milan Š. Feb 02 '23 at 13:42
  • @MilanŠ. Thank you, you have been really helpful so far already. I will accept the answer as soon as I can fix the issue. My only concern was to know whether the tflite I compile on my debian machine will work similarly on an android device. Seeing those errors gone I think I'm on a good start. – Turgut Feb 02 '23 at 13:44
  • 1
    It's really not about getting "accepted answers", the reputation system is somewhat of a joke. It's about providing you with enough information so you can figure it out on your own and even better - learn from it. – Milan Š. Feb 02 '23 at 13:46
  • @MilanŠ. I really didn't know android was unix based, I thought it was it's own thing completely seperate from everything – Turgut Feb 02 '23 at 13:51

1 Answers1

1

LAST EDIT: You seem to completely overlook what I'm trying to explain you the whole time about different architectures. Your phone is not running on the same cpu architecture as your desktop. Your desktop machine is most likely running on amd64, while your phone is most likely arm64 which is also known as Aarch64 (do you now see what the latest error means? - It can't find the appropriate packages for the correct architecture - you can see that from the path in the error message).

You installed the amd64 package by issuing sudo apt-get install python-dev not the arm64 package. Check this official debian wiki on how to use MultiArch packages

You need to:

  • Update your system so that it can download the packages for different architectures
  • And sudo apt-get install python-dev:arm64

EDIT: Regarding the error in the updated question, you are most likely missing python development packages. Since you are on Debian I've looked for a possible package of interested and this might be it: python-dev, try to apt-get it for the correct architecture that you need and maybe it will resolve your issues.


  1. Make sure that Ninja is installed. CMake is trying to use it - this confuses a lot of users as they most commonly use Makefiles and don't know that an alternative exists. I don't see you explicitly stating that you use CMake + Ninja and hence I mention it.

  2. If you have Ninja installed then the issue is most likely caused by the fact that it can't resolve the path via just the executable name. Take a look at CMAKE_MAKE_PROGRAM to better understand what I mean.

If 2. is your issue then you can try and set the whole path to this CMAKE_MAKE_PROGRAM variable, i.e. set(CMAKE_MAKE_PROGRAM /absolute/path/to/bin/ninja) and see if resolves the issue.

Milan Š.
  • 1,353
  • 1
  • 2
  • 11
  • Cosnidering I do everything with ninja and fix cmake, will the compiled version work on android? That's my real concern. – Turgut Feb 02 '23 at 13:29
  • Installing ninja fixed majority of my problems. However I'm getting a new error now which I have updated the question for. – Turgut Feb 02 '23 at 13:32
  • 1
    As long as you link everything as you should there shouldn't be any issues. Android is just another "linux distro" after all – Milan Š. Feb 02 '23 at 13:34
  • Python-dev did not solve the issue, I'll try to include python in my cmake, I'll keep you updated. – Turgut Feb 02 '23 at 13:49
  • @Turgut Make sure you download it for the right architecture and that it is available via androids toolchain file. It is 100% related to `python-dev` - if you check the contents of the installed package you will find `Python.h` – Milan Š. Feb 02 '23 at 13:54
  • Yea it shows that file instantly, but like I said I've used Python.h on a C++ project before. Maybe It's because of the android toolchain? I kinda don't know how to update it tho... Also adding python to cmake didn't work – Turgut Feb 02 '23 at 13:57
  • I've fixed the issue and updated the question accordingly, I'm wating for the compilation at the moment but it seems to be going smoothly. – Turgut Feb 02 '23 at 14:01
  • @Turgut android phones don't run on `amd64` architecture. You using it means nothing, if there is a dependency on the same library but for a different `architecture` then you can't use it – Milan Š. Feb 02 '23 at 14:01
  • And I faced another error. Which I think happened because of what you just commented – Turgut Feb 02 '23 at 14:02
  • @Turgut how did you install the `python-dev` package? I'm sorry to inform you but if you won't answer my questions with the appropriate details I won't be able to help you. – Milan Š. Feb 02 '23 at 14:03
  • Of course, I did install it when you updated the answer the first time – Turgut Feb 02 '23 at 14:05
  • **HOW**, what is the exact command you used? – Milan Š. Feb 02 '23 at 14:05
  • I tried both `sudo apt-get install python-dev` and `sudo apt install python-dev`, Am I missing something? – Turgut Feb 02 '23 at 14:06
  • 1
    Yes... and this is what I'm explaining to you the whole time. I'll update my answer for the last time. – Milan Š. Feb 02 '23 at 14:10
  • Oh you are right, I completely missed I'm terribly sorry. I tried following the instructions on the link you provided and ran `sudo apt-get install python-dev:arm64` which resulted in apt not finding the package. I tried running `sudo apt-get install libpython3-dev:arm64 ` which did install but the problem is still there. – Turgut Feb 02 '23 at 14:32