Compiling the tensorflow lite minimal example - android

I'm trying to use tensorflow lite on android. I'm getting an error during compilation with android studio:
undefined reference to 'tflite::InterpreterBuilder::operator()(std::__ndk1::unique_ptr<tflite::Interpreter, std::__ndk1::default_delete<tflite::Interpreter> >*)'
I've managed to get a reproducible example while trying to compile this minimal example from the tf repo.
Steps to get the error
Download the android ndk (I've tried both 15c and 16b and they both failed) . Call build/tools/make_standalone_toolchain.py --arch arm --api 21 --stl=libc++ --install-dir android-toolchain
Build libtensorflow, this is in my build file.
tensorflow/lite/BUILD
cc_binary(
name = "libtensorflowlite.so",
linkopts=[
"-shared",
"-Wl,-soname=libtensorflowlite.so",
],
linkshared = 1,
copts = tflite_copts(),
deps = [
":framework",
"//tensorflow/lite/kernels:builtin_ops",
],
)
I call it like:
bazel build //tensorflow/lite:libtensorflowlite.so --crosstool_top=//external:android/crosstool --cpu=armeabi-v7a --host_crosstool_top=#bazel_tools//tools/cpp:toolchain --cxxopt="-std=c++11"
I clone the flatbuffer repo (a dependency) and compile it.
Then I try and compile minimal.cc.
I call it like:
android-toolchain/bin/clang++ -std=c++11 -Igit/tensorflow-android -Igit/flatbuffers/include -Lgit/tensorflow-android -Lgit/flatbuffers/build minimal.cc -ltensorflowlite -lflatbuffers
(libtensorflow.so is in git/tensorflow-android, libflatbuffers.a in git/flatbuffers/build)
What's going on? I have no idea why this method is being called, I'm not calling the object with two arguments.

The error was me not being on ndk 18. I hadn't switched to it because when compiling bazel was specifically telling me to use <=16!

Related

Error when attempting to configure cmake for cross compilation to android

I have been trying to build a c++ library for android using cmake cross compilation.
The library itself is a trivial test I made purely for testing the androind build process.
I have been using the cmake gui (v 3.25.0) on a windows machine.
I use a visual studio 2019 generator in cmake and specify a toolchain file.
Unfortunately, I seem to get the same error whatever I try. I tried searching for similar problems and trouble shooting, but have so far been unable to make any progress. If anyone with more experience could lend a hand, I would be very grateful!
I have attempted setting many different variables in the toolchain file in an attempt to resolve the problem, but the basic version of what I am working with is:
set(CMAKE_SYSTEM_NAME Android)
set(CMAKE_ANDROID_ARCH_ABI armeabi-v7a)
set(CMAKE_ANDROID_NDK C:/Microsoft/AndroidNDK64/android-ndk-r16b/)
The error I run into happens whenever I configure cmake. The outut I get from cmake looks something like this:
Android: Targeting API '27' with architecture 'arm', ABI 'armeabi-v7a', and processor 'armv7-a'
Android: Selected Clang toolchain 'arm-linux-androideabi-clang' with GCC toolchain 'arm-linux-androideabi-4.9'
The C compiler identification is Clang 5.0.300080
The CXX compiler identification is Clang 5.0.300080
Detecting C compiler ABI info
Detecting C compiler ABI info - failed
Check for working C compiler: C:/Microsoft/AndroidNDK64/android-ndk-r16b//toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe
Check for working C compiler: C:/Microsoft/AndroidNDK64/android-ndk-r16b//toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe - broken
CMake Error at C:/Program Files/CMake/share/cmake-3.25/Modules/CMakeTestCCompiler.cmake:70 (message):
The C compiler
"C:/Microsoft/AndroidNDK64/android-ndk-r16b//toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: C:/Users/username/Documents/Code Projects/Android Test/build/CMakeFiles/CMakeScratch/TryCompile-zqgcqm
Run Build Command(s):C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/MSBuild/Current/Bin/MSBuild.exe cmTC_b0f1d.vcxproj /p:Configuration=Debug /p:Platform=x64 /p:VisualStudioVersion=16.0 /v:m && Microsoft (R) Build Engine version 16.11.2+f32259642 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.
ANDROID_HOME=C:\\Microsoft\AndroidSDK\25
ANT_HOME=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Apps\apache-ant-1.9.3
JAVA_HOME=C:\Program Files\Eclipse Foundation\jdk-8.0.302.8-hotspot
NDK_ROOT=C:\\Microsoft\AndroidNDK64\android-ndk-r16b
testCCompiler.c
clang.exe : warning : argument unused during compilation: '-mthumb' [-Wunused-command-line-argument] [C:\Users\username\Documents\Code Projects\Android Test\build\CMakeFiles\CMakeScratch\TryCompile-zqgcqm\cmTC_b0f1d.vcxproj]
clang.exe : warning : argument unused during compilation: '-mfpu=vfpv3-d16' [-Wunused-command-line-argument] [C:\Users\username\Documents\Code Projects\Android Test\build\CMakeFiles\CMakeScratch\TryCompile-zqgcqm\cmTC_b0f1d.vcxproj]
clang.exe : warning : argument unused during compilation: '-mfloat-abi=softfp' [-Wunused-command-line-argument] [C:\Users\username\Documents\Code Projects\Android Test\build\CMakeFiles\CMakeScratch\TryCompile-zqgcqm\cmTC_b0f1d.vcxproj]
CLANGCOMPILE : error : unknown target CPU 'armv7-a' [C:\Users\username\Documents\Code Projects\Android Test\build\CMakeFiles\CMakeScratch\TryCompile-zqgcqm\cmTC_b0f1d.vcxproj]
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:4 (project)
Configuring incomplete, errors occurred!
I initially used used the NDK version found in the android tools for visual studio found at: "C:/Microsoft/AndroidNDK64/android-ndk-r16b/"
as an NDK version.
I have also tried directly downloading the latest version of the NDK and pointing to that with CMAKE_ANDROID_NDK in the toolchain file, though this did not seem to make any change.
I have tried directly using the toolchain.cmake files found at "NDK/build/cmake/android.toolchain.cmake"
for both the NDK versions described above. This too gave the same error.
Any ideas what could be causing this or how to fix?
Ok, so I was misunderstanding a few things about cross-compiling to android.
First, if I want to be able to use the static library (e.g in android studio), I will need to generate and build a solution for more than one android ABI.
The ABI's to build for are: armeabi-v7a, arm64-v8a, x86_64, x86. I then need to check for each android ABI in the CMakeLists.txt file of Android studio (e.g. elseif(ANDROID_ABI STREQUAL arm64-v8a)) and swap out the location of the library to import to the build folder of that particular ABI.
As far as I am aware, it is unfortunately not possible to generate for all these ABI's as seperate platforms within the same visual studio solution. Given that, cmake must be run 4 seperate times to different build folders, each time targeting a different ABI and generator platform. To save the hassle of doing this manually, creating a batch (.bat) script to automate the process seems the best way to go. This will involve using cmake from the command line instead of the GUI version. The script can also be made to build the generated projects to save opening each one in visual studio and building there.
The batch script I have made to generate and build cross-compilation projects for android is as follows:
#echo OFF
set BUILD_DIR=build
set ANDROID_NDK=C:\Microsoft\AndroidNDK\android-ndk-r23c
set GENERATOR="Visual Studio 17 2022"
set CMAKE_GENERATOR=-G %GENERATOR%
set CMAKE_TOOLCHAIN_FILE=-DCMAKE_TOOLCHAIN_FILE=%ANDROID_NDK%\build\cmake\android.toolchain.cmake
set CMAKE_SYSTEM_NAME=-DCMAKE_SYSTEM_NAME=Android
set EXTRA_CMAKE_ARGS=-DBUILD_SHARED_LIBS=true -DANDROID_TOOLCHAIN=clang -DANDROID_STL=c++_static
CALL :build_android armeabi-v7a ARM 16
CALL :build_android arm64-v8a ARM64 21
CALL :build_android x86_64 x64 21
CALL :build_android x86 x86 16
EXIT /B %ERRORLEVEL%
:build_android
set ABI_VERSION=%~1
set GENERATOR_PLATFORM=%~2
set MINIMUM_API_LEVEL=%~3
set CMAKE_ANDROID_ARCH_ABI=-DANDROID_ABI=%ABI_VERSION%
set ABI_BUILD_DIR=%BUILD_DIR%\%ABI_VERSION%
set CMAKE_GENERATOR_PLATFORM=-A %GENERATOR_PLATFORM%
set CMAKE_BUILD_DIR=-B %ABI_BUILD_DIR%
set CMAKE_MIN_API=-DANDROID_PLATFORM=android-%MINIMUM_API_LEVEL%
set CMAKE_ARGS=%CMAKE_BUILD_DIR% %CMAKE_ANDROID_ARCH_ABI% %CMAKE_GENERATOR% %CMAKE_GENERATOR_PLATFORM% %CMAKE_SYSTEM_NAME% %CMAKE_TOOLCHAIN_FILE% %CMAKE_MIN_API%
echo building for android ABI: %ABI_VERSION%
echo cmake arguments = %CMAKE_ARGS%
echo:
cmake %CMAKE_ARGS% %EXTRA_CMAKE_ARGS%
echo:
cmake --build %ABI_BUILD_DIR% --target ALL_BUILD
echo:
echo:
EXIT /B 0
This worked for my simple test library, but I guess depending on the project the arguments to the cmake commands may need to be tweaked.

Problems attempting to build oboe c++ library using cmake on windows

I am attempting to build 'oboe' on a windows machine using cmake (version 3.25.0) and a visual studio generator. The end goal is to create a c++ audio library that uses oboe as the back end for android, but as a first step simply geting oboe to build is the plan.
I have managed to succesfully generate build and use a static library with a few simple test functions using the method described below, but I run into errors when attempting to build oboe.
I have also managed to get oboe to build by using the method described in the documentation and
doing add_subdirectory etc. in the CMakeLists file of android studio. However I am trying to generate/ build using cmake outside of android studio.
The oboe repo is found here: https://github.com/google/oboe
The toolchain file I am using is: 'android.toolchain.cmake' included in the NDK at the location: ndk version/build/cmake/android.toolchain.cmake
I use a batch script to automate generating/ building to several different android ABI's, inspired by a similar shell script included with the oboe repo 'build_all_android.sh'. The .bat script I made is shown below:
#echo OFF
set BUILD_DIR=build
set ANDROID_NDK=C:\Microsoft\AndroidNDK\android-ndk-r23c
set GENERATOR="Visual Studio 17 2022"
set CMAKE_GENERATOR=-G %GENERATOR%
set CMAKE_TOOLCHAIN_FILE=-DCMAKE_TOOLCHAIN_FILE=%ANDROID_NDK%\build\cmake\android.toolchain.cmake
set CMAKE_SYSTEM_NAME=-DCMAKE_SYSTEM_NAME=Android
set EXTRA_CMAKE_ARGS=-DBUILD_SHARED_LIBS=true -DANDROID_TOOLCHAIN=clang -DANDROID_STL=c++_static
CALL :build_android armeabi-v7a ARM 16
CALL :build_android arm64-v8a ARM64 21
CALL :build_android x86_64 x64 21
CALL :build_android x86 x86 16
EXIT /B %ERRORLEVEL%
:build_android
set ABI_VERSION=%~1
set GENERATOR_PLATFORM=%~2
set MINIMUM_API_LEVEL=%~3
set CMAKE_ANDROID_ARCH_ABI=-DANDROID_ABI=%ABI_VERSION%
set ABI_BUILD_DIR=%BUILD_DIR%\%ABI_VERSION%
set CMAKE_GENERATOR_PLATFORM=-A %GENERATOR_PLATFORM%
set CMAKE_BUILD_DIR=-B %ABI_BUILD_DIR%
set CMAKE_MIN_API=-DANDROID_PLATFORM=android-%MINIMUM_API_LEVEL%
set CMAKE_ARGS=%CMAKE_BUILD_DIR% %CMAKE_ANDROID_ARCH_ABI% %CMAKE_GENERATOR% %CMAKE_GENERATOR_PLATFORM% %CMAKE_SYSTEM_NAME% %CMAKE_TOOLCHAIN_FILE% %CMAKE_MIN_API%
echo building for android ABI: %ABI_VERSION%
echo cmake arguments = %CMAKE_ARGS%
echo:
cmake %CMAKE_ARGS% %EXTRA_CMAKE_ARGS%
echo:
cmake --build %ABI_BUILD_DIR% --target ALL_BUILD
echo:
echo:
EXIT /B 0
When I run this using the ANDROID_NDK variable (line 3) pointing to the root of the up to date ndk that comes with the visual studio 2022 android tools, I get an error:
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe
-- Check for working C compiler: C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe - broken
CMake Error at C:/Program Files/CMake/share/cmake-3.25/Modules/CMakeTestCCompiler.cmake:70 (message):
The C compiler
"C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: C:/Users/user/Documents/Code Projects/oboe/build/x86/CMakeFiles/CMakeScratch/TryCompile-pp2ibg
Run Build Command(s):C:/Program Files/Microsoft Visual Studio/2022/Community/MSBuild/Current/Bin/amd64/MSBuild.exe cmTC_c21fb.vcxproj /p:Configuration=Debug /p:Platform=x86 /p:VisualStudioVersion=17.0 /v:m && MSBuild version 17.4.1+9a89d02ff for .NET Framework
ANDROID_HOME=C:\\Microsoft\AndroidSDK\25
ANDROID_SDK_ROOT=C:\\Microsoft\AndroidSDK\25
ANT_HOME=
JAVA_HOME=C:\Program Files\Android\jdk\jdk-8.0.302.8-hotspot\jdk8u302-b08
NDK_ROOT=C:\Microsoft\AndroidNDK\android-ndk-r23c
testCCompiler.c
In file included from <built-in>:349:
<command line>(1,9): warning : '__ANDROID_API__' macro redefined [-Wmacro-redefined] [C:\Users\user\Documents\Code Projects\oboe\build\x86\CMakeFiles\CMakeScratch\TryCompile-pp2ibg\cmTC_c21fb.vcxproj]
#define __ANDROID_API__ 1
^
<built-in>(342,9): note: previous definition is here
#define __ANDROID_API__ __ANDROID_MIN_SDK_VERSION__
^
1 warning generated.
ld: error: cannot open crtbegin_so.o: No such file or directory
ld: error: unable to find library -llog
ld: error: unable to find library -landroid
ld: error: cannot open crtend_so.o: No such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Application Type\Android\3.0\Android.Common.targets(125,5): error MSB6006: "clang.exe" exited with code 1. [C:\Users\user\Documents\Code Projects\oboe\build\x86\CMakeFiles\CMakeScratch\TryCompile-pp2ibg\cmTC_c21fb.vcxproj]
CMake will not be able to correctly generate this project.
and cmake fails to generate the project. I can actually get this to generate correctly, but only if I set the android NDK version within the cross-compilation options of visual studio to use the NDK bundled with my installation of Android studio, and also set the ANDROID_NDK variable of the batch script to point to the same root.
In this case, though cmake generates the project the build fails with a similar reason to the warning given above:
Building Custom Rule C:/Users/user/Documents/Code Projects/oboe/CMakeLists.txt
AAudioLoader.cpp
In file included from <built-in>:404:
<command line>(1,9): error : '__ANDROID_API__' macro redefined [-Werror,-Wmacro-redefined] [C:\Users\user\Documents\Co
de Projects\oboe\build\x86\oboe.vcxproj]
#define __ANDROID_API__ 16
^
<built-in>(394,9): note: previous definition is here
#define __ANDROID_API__ __ANDROID_MIN_SDK_VERSION__
^
1 error generated.
Thanks in advance for any help, it is much appreciated!
Your build fails because you instruct the compiler to view the warning '__ANDROID_API__' macro redefined as an error. And this warning seems to occur because Visual Studio still sets the macro __ANDROID_API__ instead of setting __ANDROID_MIN_SDK_VERSION__. I think that is a Visual Studio bug that was introduced when they started shipping the NDK 23 instead of 21.
EDIT:
This issue is being tracked here.

Unable to get qmake to cross compile correctly

I am new to this build system, but I've spent dozens of hours trying to get qmake + clang to produce a working android binary. I would appreciate any help or advice. Here is as simple an example as I can come up with:
First, here is a working build command. It gives me a shared object that I can call from android (with Unity, using C# native interops):
C:\Users\deltav\AppData\Local\Android\Sdk2\ndk\21.3.6528147\toolchains\llvm\prebuilt\windows-x86_64\bin\clang.exe ^
-target aarch64-none-linux-android21 -shared -v ^
-DANDROID_ABI=arm64-v8a -DANDROID_ARM_MODE=arm -DANDROID ^
M:\simpleExample\simpleExample.c ^
-o M:\simpleExample\libsimpleExample.so
Here is the verbose output of running that command: https://pastebin.com/18HcnDYh
On the other hand, here is the simpleExample.pro file that is NOT giving me a good binary: https://pastebin.com/wxrx6Myc
Here is the qmake output for building in release mode: https://pastebin.com/KWavwrb7
As you will know, qmake uses a kit to execute the project. My kit: https://imgur.com/a/Ehtmgin
The specific issue that has pushed me down this debugging path is from C# trying to load my shared object like this:
[DllImport("simpleExample")]
public static extern float getSpeedY();
The error that I get is this:
"DllNotFoundException: Unable to load DLL 'simpleExample': The
specified module could not be found."
Again, this error is not an issue if I just build from the CLI without qmake. I do not beleive this is a unity or C# error, as I worked the problem from that side first.
What I have tried so far:
I have used multiple ELF & DWARF tools to compare the ABIs of the generated binaries, which all told me the binaries were identical.
I tweaked and shaved down the clang calls that qmake generated until I got a vaild build command. Unfortunately I can't just replace qmake with the complile command because there are other projects that I need to apply this fix to.
QMake version 3.1
Ironically, CMake basically works out of the box.
Answer: Looking at the clang linking call, you can see QMake 3.1 was adding "libc++shared" as a dependency. I'm not sure why, as this was a C project.
The equivalent CMake project did not do this.
Packaging libc++shared with my binary fixed the issue.

Same android cmake git project compiles well on one Ubuntu machine but does not compile on another Ubuntu machine even the entire project is the same

I'm developing an android cmake project that compiles succesfully on my 32-bit Ubuntu 12.04 machine at work (I will call it WorkMachine). When I want to compile it at home, I installed a 32-bit Ubuntu 12.04 virtual machine (I will call it HomeMachine), I git cloned the project on it, and when I start the compilation, it will block with an error.
CMAKE_CFLAGS are:
CMAKE_CXXFLAGS are:
-- Configuring done
-- Generating done
-- Build files have been written to: /home/raa/Dropbox/Demo/android_build
[ 0%] Built target bitstream
[ 0%] Built target distrat
[ 1%] Built target fisher
[ 1%] Built target gtest
[ 7%] Built target jpeg-8c
[ 7%] Built target map
[ 8%] Built target resampler
[ 9%] Built target timer
[ 92%] Built target fftw3f-3
[ 96%] Built target vlfeat
[ 96%] Built target extract_shared
[ 97%] Built target shared
[ 97%] Built target cssc_train
Linking CXX executable ../../bin/extract
/home/raa/Dropbox/Demo/libraries/fftw-3.3.3/api/configure.c:28: error: undefined reference to 'fftwf_dft_conf_standard'
collect2: error: ld returned 1 exit status
make[2]: *** [../bin/extract] Error 1
make[1]: *** [src/CMakeFiles/extract.dir/all] Error 2
make: *** [all] Error 2
Here I am reporting gcc version (the same on both computers)
gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-linux-gnu/4.6/lto-wrapper
Target: i686-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu
Thread model: posix
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
But how is that possible?
It seems the problem is not due to some cmake cache or similar chaches, because if I git clone the project on another directory on the WorkMachine, the code compiles well too.
What can I check in order to resolve the problem? If you need additional details, please ask me.
Thank you for your time.
Riccardo
the solution to Riccardos problem is to change the commandline parameter of ar from r to q (see man page for difference, basically q does not check for duplicats)
do this via
SET(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> q <TARGET> <LINK_FLAGS> <OBJECTS>")
you probably want to check if CMAKE_AR is "ar" (might be different on other systems)
whoa! Solved! It was really a weird behaviour from cmake: I had three files conf.c that must be merged into a static library. When cmake run the AR program to create the library, it executed a command that was like:
ar cr dir1/source1.c dir1/source2.c ... dft/conf.c rdft/conf.c
ar r reodft/conf.c dir2/source3.c
so, maybe due to the huge number of object files that were to be linked, cmake splitted ar commands in two command, BUT second command overwrited one of the two file conf.c that were added with the first command, so symbols defined with the first ar command were lost! This is clearly a bug in ar because even if the source file is the same, maybe the content may vary, thus symbols are not to be deleted. Thanks to all! Your suggestions brought me to the right solution!

How can I get JHC to cooperate with the android ndk?

JHC is a Haskell compiler which portable generates C code and then invokes a compiler backend to make an executable.
I need to dump the header files and libraries for JHC's runtime system, so the android ndk can use it to compile the generated C code.
Alternatively, I need to figure out how to link the android API into JHC.
Basically, how can I plug JHC into the android ndk backend?
Edit: I've made a standalone compiler using one of the ndk's build tools that JHC can use, "build/tools/make-standalone-toolchain.sh" . Now I need to figure out a way for JHC to compile my program into a shared object library, and how to get JHC to not demand a "main" function.
Edit2: I have the following targets.ini and compile script. Strangely the shared library fails to load.
[android]
cc=arm-linux-androideabi-gcc
cflags+= -shared -fPIC -rdynamic -Wno-all
gc=static
executable_extension=.so
merge=mle32
and
#!/bin/sh
jhc --cross -mandroid \
--main=Main.android_main \
hs/Main.hs -o libs/armeabi/libnative-activity &&
ant debug &&
ant debug install
I figured out the problem.
I didn't put the function call "app_dummy()" at the start of my "android_main" function.

Categories

Resources