Entangled with Google test - android

I am having trouble to get up and running with Google test. I have read the suggested steps from Google, I also looked a previous post, and read some other examples, but it doesn't clear much things up.
To keep things simple, I am trying the suggested example from Google test that is available from the directory in the Android ndk - sample1:
// main.cpp
#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include "gtest/gtest.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
testing::InitGoogleTest(&argc, argv);
QtQuick2ApplicationViewer viewer;
viewer.setMainQmlFile(QStringLiteral("qml/factorial/main.qml"));
viewer.showExpanded();
return RUN_ALL_TESTS();
}
// sample1_unittest.cpp
#include <limits.h>
#include "sample1.h"
#include "gtest/gtest.h"
// Tests factorial of 0.
TEST(FactorialTest, Zero) {
EXPECT_EQ(1, Factorial(0));
}
The files sample1.h, sample1.cpp are also in the project, which contain the factorial function. Google test was equally informed to the project file - factorial.pro:
INCLUDEPATH +=
/opt/android-studio/ndk/sources/third_party/googletest/googletest/include
When I press [Build > Build Project "factorial"] it gets the following error:
main.cpp:8: error: undefined reference to 'testing::InitGoogleTest(int*, char**)'
sample1_unittest.cpp:17: error: undefined reference to 'testing::Test::Test()'
I am working with Ubuntu, QtCreator, Android and C++. Indeed I have spent already 3 days mocking around, but getting not much anywhere so far. Thus, I am posting here in hope some guru may give any hint on this. Any help will be mostly appreciated.

It seems you haven't built Google Test from what you describe. You need to compile the project into a library and then link against it. If you have CMake installed, then you have two options:
Use CMake's GUI (it's fairly intuitive) to generate the build system files, and then use those as usual (e.g. if you generate a Visual Studio solution, open the .sln file and build the project).
Use the command line to do the same thing; essentially you just create a new directory and do cmake <path-to-google-test> inside of it. The rest is the same.
You could also build the library by yourself. The distribution contains a folder named fused-src which should contain at least two files: gtest_main.cpp and gtest-all.cpp. Compile those files and you're done. You need to generate two libraries here: gtest out of gtest-all.cpp and gtest_main out of gtest_main.cpp.
Another alternative would be to get already built libraries. I've never searched for them, but they might be out there.

Try something like this:
$ g++ -I $GTEST_HOME/include -L $GTEST_HOME/lib -lgtest -lgtest_main -lpthread test.cpp
For more details:
How to setup googleTest as a shared library on Linux
If it still doesn't work, may find interesting to consider to use Makefile:
# A sample Makefile for building Google Test and using it in user
# tests. Please tweak it to suit your environment and project. You
# may want to move it to your project's root directory.
#
# SYNOPSIS:
#
# make [all] - makes everything.
# make TARGET - makes the given target.
# make clean - removes all files generated by make.
# Please tweak the following variable definitions as needed by your
# project, except GTEST_HEADERS, which you can use in your own targets
# but shouldn't modify.
# Points to the root of Google Test, relative to where this file is.
# Remember to tweak this if you move this file.
GTEST_DIR = ..
# Where to find user code.
USER_DIR = ../samples
# Flags passed to the preprocessor.
# Set Google Test's header directory as a system directory, such that
# the compiler doesn't generate warnings in Google Test headers.
CPPFLAGS += -isystem $(GTEST_DIR)/include
# Flags passed to the C++ compiler.
CXXFLAGS += -g -Wall -Wextra -pthread
# All tests produced by this Makefile. Remember to add new tests you
# created to the list.
TESTS = sample1_unittest
# All Google Test headers. Usually you shouldn't change this
# definition.
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
$(GTEST_DIR)/include/gtest/internal/*.h
# House-keeping build targets.
all : $(TESTS)
clean :
rm -f $(TESTS) gtest.a gtest_main.a *.o
# Builds gtest.a and gtest_main.a.
# Usually you shouldn't tweak such internal variables, indicated by a
# trailing _.
GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)
# For simplicity and to avoid depending on Google Test's
# implementation details, the dependencies specified below are
# conservative and not optimized. This is fine as Google Test
# compiles fast and for ordinary users its source rarely changes.
gtest-all.o : $(GTEST_SRCS_)
$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
$(GTEST_DIR)/src/gtest-all.cc
gtest_main.o : $(GTEST_SRCS_)
$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
$(GTEST_DIR)/src/gtest_main.cc
gtest.a : gtest-all.o
$(AR) $(ARFLAGS) $# $^
gtest_main.a : gtest-all.o gtest_main.o
$(AR) $(ARFLAGS) $# $^
# Builds a sample test. A test should link with either gtest.a or
# gtest_main.a, depending on whether it defines its own main()
# function.
sample1.o : $(USER_DIR)/sample1.cc $(USER_DIR)/sample1.h $(GTEST_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample1.cc
sample1_unittest.o : $(USER_DIR)/sample1_unittest.cc \
$(USER_DIR)/sample1.h $(GTEST_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample1_unittest.cc
sample1_unittest : sample1.o sample1_unittest.o gtest_main.a
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $#
If you have to use Makefile to get gtest working, you probably may need to adjust the given template for your case, as you intend to build it to use with Android.

Related

Compile libraries for Kotlin android,rust and go

This question is exploratory in nature, not sure if this fits stack overlflow Q&A.
Context:
I have a library written in golang that I need to compile for multiple services to use.
These services are in Kotlin android,Rust,Golang.
The only option I am aware of is using something like SWIG to compile the go library for different languages.
Problem:
I don't think SWIG works for Kotlin.
I am trying to fish for the best methods to do this and different approaches this can be done.
For any language that can generate a C shared library and header file you can use SWIG to wrap it. Equally for any language that runs within a JVM and can call Java classes you can make use of SWIG's auto generated Java bindings.
With that we can therefore do a sequence of things that looks like this:
Go -> C -> JNI -> Java -> Kotlin
It's actually fairly sane. I've put together an example below for this to show how it works since I was curious having never written Go nor Kotlin before. (Take this with a pinch of salt therefore, I've probably not hit "best practice" for either!)
This example assumes you have a working JDK/JRE, C compiler, Go installation and kotlinc.
My demo.go looks like this:
package main
import (
"C"
"fmt"
)
//export TestGoFunc
func TestGoFunc(str *C.char) *C.char {
fmt.Printf("Got string: %s\n", C.GoString(str))
return nil
}
func main() {}
And hello.kt looks like this:
fun main() {
println("Hello, World!")
test.TestGoFunc("Another string")
}
To wrap this I wrote the following SWIG interface:
%module test
%{
#include "golib.h"
%}
%include <typemaps.i>
%pragma(java) jniclasscode=%{
static {
System.loadLibrary("test");
}
%}
// Assuming you don't care about these in your library silence/neaten stuff
#define _Complex
%ignore _GoString_;
%ignore GoComplex64;
%ignore GoComplex128;
%ignore GoSlice;
%ignore GoInterface;
%include "golib.h"
This is a fairly standard SWIG interface for targeting Java - it hides some of the stuff in the generated header file we don't care about and autoloads the .so file inside Java using a pragma for us.
Then I put together a small Makefile to build everything since there's a bunch of steps to this build:
all: libtest.so hello.jar
golib.so: demo.go
go build -o golib.so -buildmode=c-shared demo.go
test_wrap.c: golib.so test.i
swig3.0 -java -Wall test.i
libtest.so: test_wrap.c
gcc -shared -Wall -Wextra test_wrap.c -o libtest.so ./golib.so -I/usr/lib/jvm/default-java/include/ -I/usr/lib/jvm/default-java/include/linux
hello.jar: hello.kt
javac *.java
kotlinc hello.kt -include-runtime -d hello.jar -cp .
jar uvf hello.jar *.class
If we build and run this then it all works nicely:
$ make
go build -o golib.so -buildmode=c-shared demo.go
swig3.0 -java -Wall test.i
gcc -shared -Wall -Wextra test_wrap.c -o libtest.so ./golib.so -I/usr/lib/jvm/default-java/include/ -I/usr/lib/jvm/default-java/include/linux
javac *.java
kotlinc hello.kt -include-runtime -d hello.jar -cp .
jar uvf hello.jar *.class
adding: test.class(in = 302) (out= 216)(deflated 28%)
adding: testJNI.class(in = 389) (out= 268)(deflated 31%)
$ LD_LIBRARY_PATH=. java -jar hello.jar
Hello, World!
Got string: Another string
I'd be tempted to use -buildmode=c-archive for Go to build a static library and then link that into the SWIG shared object instead just to keep things simpler in that regards though.

Clang linking .so library libc++_shared.so

I'm having an Error in my Native C++ Code in Android NDK Application
My main.cpp
#include <stdio.h>
int main()
{
printf("Hello, world\n");
return 0;
}
The main.c is exactly the same.
If i run
/home/rip/Music/android-ndk-r19b/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android26-clang -pie main.c
then
adb push a.out /data/local/tmp
and
adb shell /data/local/tmp/a.out
all works fine. But if i run
/home/rip/Music/android-ndk-r19b/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android26-clang++ -pie main.cpp
then
adb push a.out /data/local/tmp
and
adb shell /data/local/tmp/a.out
The error message is:
CANNOT LINK EXECUTABLE "/data/local/tmp/a.out": library "libc++_shared.so" not found
Then i tried to run
/home/rip/Music/android-ndk-r19b/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android26-clang++ -pie hello1.cpp /home/rip/Music/android-ndk-r19b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/libc++_shared.so
to link the library, but it doesn't work anyway.
The error message is:
CANNOT LINK EXECUTABLE "/data/local/tmp/a.out": library "libc++_shared.so" not found
That's the expected behavior. Unlike the standard C library (to which your program is linking when building with simple *-clang), C++ is not a system library. You have to make it available on the device just like any other third party library.
Quoted from official documentation:
Note: libc++ is not a system library. If you use libc++_shared.so, it must be included in your APK. If you're building your application with Gradle this is handled automatically.
And:
If you're using clang directly in your own build system, clang++ will use c++_shared by default. To use the static variant, add -static-libstdc++ to your linker flags.
So either link with C++ statically by passing -static-libstdc++ to compiler. Or copy the libc++_shared.so (from <NDK>/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/ in your case) and run like:
adb push a.out libc++_shared.so /data/local/tmp/
adb shell
cd /data/local/tmp/
LD_LIBRARY_PATH=. ./a.out
Other than the LLVM's Standard C++ library discussed above, there's also a limited system C++ runtime (/system/lib(64)/libstdc++.so) which "provides support for the basic C++ Runtime ABI". But "The system STL will be removed in a future NDK release."
I compiled the same "hello world" code in a .c and .cpp file and didn't have the same issue when I pushed the application to my device. I assume you're having the same issue as in this topic:
Application can't find libc++_shared.so
The issue may come from your toolchain or toolchain parameters as you are calling clang manually.
I created a simple project that you can run and test:
android-ndk-example
add_executable( # Sets the name of the library.
ndk_example_c
# Provides a relative path to your source file(s).
main.c
)
add_executable( # Sets the name of the library.
ndk_example_cpp
# Provides a relative path to your source file(s).
main2.cpp
)
In generated cmake script, I can see the following definition for cpp compiler:
rule CXX_COMPILER__ndk_example_cpp
depfile = $DEP_FILE
deps = gcc
command = D:\Users\$USER\AppData\Local\Android\Sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv7-none-linux-androideabi19 --gcc-toolchain=D:/Users/$USER/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=D:/Users/$USER/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot $DEFINES $INCLUDES $FLAGS -MD -MT $out -MF $DEP_FILE -o $out -c $IN_ABS
description = Building CXX object $out
I write a new Answer for the Solution because i cannot edit my question.
The Solution is the following command for android devices with armv7:
/home/tony/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=armv7-none-linux-androideabi19 --gcc-toolchain=/home/tony/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/tony/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot main.cpp
For aarch64 armv8 the command is:
/home/tony/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=aarch64-none-linux-android21 --gcc-toolchain=/home/tony/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/tony/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot main.cpp
A CMakeLists.txt file should look as:
cmake_minimum_required(VERSION 3.1)
set(CMAKE_CXX_COMPILER /home/tony/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++)
project(Test CXX)
set(CMAKE_CXX_FLAGS "--target=aarch64-none-linux-android21 --gcc-toolchain=/home/tony/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/tony/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot")
set(SOURCES
main.cpp
)
add_executable(Test ${SOURCES})
Then can the app build with
cmake
make
adb push Test /data/local/tmp
adb shell /data/local/tmp/Test

Compiling Qt and OpenCV for arm64-v8a

I am trying to compile a program using Qt and OpenCV for arm64-v8a
I created a kit to use an arm64-v8a version of Qt, but I get tons of undefined reference errors
I have compiled Qt using the following parameters :
./configure -xplatform android-clang \
--disable-rpath -nomake tests -nomake examples \
-android-ndk <ndk-r16b-root> -android-sdk <sdk-root> -android-ndk-host linux-x86_64 \
-android-toolchain-version 4.9 -android-ndk-platform android-21 -android-arch arm64-v8a \
-skip qttranslations -skip qtserialport -skip qtwebengine -no-warnings-are-errors \
-prefix <install-dir> -opensource -confirm-license
Then I added 2 compilers, the first for C and the second for C++ :
<ndk-r16b-root>/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
with ABI : <custom> arm-linux-android-elf-64-bit
<ndk-r16b-root>/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++
with ABI : <custom> arm-linux-android-elf-64-bit
I cloned the auto-detected kit for this version of Qt and used the following settings :
Device type : Android Device
Device : Run on Android (default for Android)
Sysroot : <ndk-r16b-root>/platforms/android-21/arch-arm64
Compilers : the 2 above
Android GDB server : <ndk-r16b-root>/prebuilt/android-arm64/gdbserver/gdbserver
Qt version : the one I just compiled
Qt mkspec : android-clang
Other fields are left empty/by default
My .pro file looks like this :
QT += quick multimedia multimediawidgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
HEADERS += <my-headers>
SOURCES += <my-sources>
RESOURCES += qml/qml.qrc
INCLUDEPATH += OpenCV-android-sdk/sdk/native/jni/include
LIBS += -L"OpenCV-android-sdk/sdk/native/3rdparty/libs/arm64-v8a"\
-L"OpenCV-android-sdk/sdk/native/libs/arm64-v8a"\
-llibtiff\
-llibjpeg\
-llibjasper\
-llibpng\
-lIlmImf\
-llibwebp\
-lopencv_core\
-ltbb\
-lopencv_flann\
-lopencv_imgproc\
-lopencv_highgui\
-lopencv_features2d\
-lopencv_calib3d\
-lopencv_ml\
-lopencv_objdetect\
-lopencv_video\
-lopencv_photo\
-lopencv_videostab\
-ltegra_hal\
-lopencv_imgcodecs\
-lopencv_shape\
-lopencv_stitching\
-lopencv_superres\
-lopencv_videoio
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
DISTFILES += \
android/AndroidManifest.xml \
android/gradle/wrapper/gradle-wrapper.jar \
android/gradlew \
android/res/values/libs.xml \
android/build.gradle \
android/gradle/wrapper/gradle-wrapper.properties \
android/gradlew.bat
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
The OpenCV-android-sdk folder comes from here using 3.2.0 Android pack.
When I try to compile, I get tons of undefined references like the followings :
OpenCV-android-sdk/sdk/native/libs/arm64-v8a/libopencv_core.a(matrix.cpp.o): In function `cv::SparseMat::newNode(int const*, unsigned long)':
matrix.cpp:(.text._ZN2cv9SparseMat7newNodeEPKim+0x4b8): undefined reference to `std::__throw_length_error(char const*)'
OpenCV-android-sdk/sdk/native/libs/arm64-v8a/libopencv_core.a(matrix.cpp.o): In function `std::vector<cv::Mat, std::allocator<cv::Mat> >::_M_fill_insert(__gnu_cxx::__normal_iterator<cv::Mat*, std::vector<cv::Mat, std::allocator<cv::Mat> > >, unsigned long, cv::Mat const&)':
matrix.cpp:(.text._ZNSt6vectorIN2cv3MatESaIS1_EE14_M_fill_insertEN9__gnu_cxx17__normal_iteratorIPS1_S3_EEmRKS1_[_ZNSt6vectorIN2cv3MatESaIS1_EE14_M_fill_insertEN9__gnu_cxx17__normal_iteratorIPS1_S3_EEmRKS1_]+0xcd4): undefined reference to `std::__throw_length_error(char const*)'
OpenCV-android-sdk/sdk/native/libs/arm64-v8a/libopencv_core.a(matrix.cpp.o): In function `std::vector<cv::UMat, std::allocator<cv::UMat> >::_M_fill_insert(__gnu_cxx::__normal_iterator<cv::UMat*, std::vector<cv::UMat, std::allocator<cv::UMat> > >, unsigned long, cv::UMat const&)':
matrix.cpp:(.text._ZNSt6vectorIN2cv4UMatESaIS1_EE14_M_fill_insertEN9__gnu_cxx17__normal_iteratorIPS1_S3_EEmRKS1_[_ZNSt6vectorIN2cv4UMatESaIS1_EE14_M_fill_insertEN9__gnu_cxx17__normal_iteratorIPS1_S3_EEmRKS1_]+0xbb0): undefined reference to `std::__throw_length_error(char const*)'
The actual linker command used (where errors happen) is :
<ndk-r16b-root>/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ -D__ANDROID_API__=26 -target aarch64-none-linux-android -gcc-toolchain <ndk-r16b-root>/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64 -Wl,--exclude-libs,libgcc.a --sysroot=<ndk-r16b-root>/platforms/android-26/arch-arm64/ -Wl,-soname,libTest.so -Wl,-rpath-link=<newly-compiled-Qt-root>/lib -Wl,--no-undefined -Wl,-z,noexecstack -shared -o libTest.so myfilter.o qml_main_qml.o qmlcache_loader.o -L<ndk-r16b-root>/sources/cxx-stl/llvm-libc++/libs/arm64-v8a -LOpenCV-android-sdk/sdk/native/3rdparty/libs/arm64-v8a -LOpenCV-android-sdk/sdk/native/libs/arm64-v8a -llibtiff -llibjpeg -llibjasper -llibpng -lIlmImf -llibwebp -lopencv_core -ltbb -lopencv_flann -lopencv_imgproc -lopencv_highgui -lopencv_features2d -lopencv_calib3d -lopencv_ml -lopencv_objdetect -lopencv_video -lopencv_photo -lopencv_videostab -ltegra_hal -lopencv_imgcodecs -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videoio -L<newly-compiled-Qt-root>/lib -lQt5Quick -lQt5MultimediaWidgets -lQt5Multimedia -lQt5Widgets -lQt5Gui -lQt5Qml -lQt5Network -lQt5Core -lGLESv2 -lc++ -llog -lz -lm -ldl -lc
Edit: tried to compile OpenCV myself, does not work better.
Edit2: I just saw Petesh's answer there: https://stackoverflow.com/a/12920944/9987834
Could this be because my version of Qt and OpenCV have been compiled using different standard libraries?
Edit3: Cannot manage to compile OpenCV correctly, I get errors like this one:
skipping incompatible /usr/local/lib/libopencv_core.a when searching for -lopencv_core
Edit4: For some reason, OpenCV stopped compiling at all, giving me errors like these:
In file included from /path/to/opencv-3.2.0/modules/core/src/algorithm.cpp:43:
In file included from /path/to/opencv-3.2.0/modules/core/src/precomp.hpp:84:
In file included from /path/to/opencv-3.2.0/modules/core/include/opencv2/core/hal/intrin.hpp:297:
/path/to/opencv-3.2.0/modules/core/include/opencv2/core/hal/intrin_neon.hpp:285:49: error: expected
unqualified-id
template <typename T> static inline float16x4_t vld1_f16(const T* ptr)
^
<ndk-r16b-root>/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/5.0.300080/include/arm_neon.h:7745:24: note:
expanded from macro 'vld1_f16'
#define vld1_f16(__p0) __extension__ ({ \
^
[ 31%] Built target libwebp
[ 31%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/command_line_parser.cpp.o
In file included from /path/to/opencv-3.2.0/modules/core/src/algorithm.cpp:43:
In file included from /path/to/opencv-3.2.0/modules/core/src/precomp.hpp:84:
In file included from /path/to/opencv-3.2.0/modules/core/include/opencv2/core/hal/intrin.hpp:297:
/path/to/opencv-3.2.0/modules/core/include/opencv2/core/hal/intrin_neon.hpp:778:8: error: unknown type
name 'v_float16x4'; did you mean 'v_float32x4'?
inline v_float16x4 v_load_f16(const short* ptr)
^~~~~~~~~~~
v_float32x4
My cake (gui) output is here : https://pastebin.com/ZBrtizUi
Does anyone have an idea why?
Even if it is a late reply, my comment might help another user updating a deprecated Android project...
Here is my typed CMake command (configured variables are detailled in the linked logfiles):
cmake ${OPENCV_CMAKEFILE} -DCMAKE_TOOLCHAIN_FILE=${ANDROID_TOOLCHAIN_FILE} -DANDROID_NDK=${ANDROID_NDK_PATH} -DANDROID_NATIVE_API_LEVEL=android-25 -DBUILD_JAVA=OFF -DBUILD_ANDROID_EXAMPLES=ON -DBUILD_ANDROID_PROJECTS=ON-DANDROID_STL=c++_static -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX:PATH=${OPENCV_INSTALL_PATH} -DANDROID_ABI=arm64-v8a -DCMAKE_BUILD_TYPE=Release
1. With your settings, when FP16 support is enabled using OpenCV 4.5.3, CMake logs indicates that:
the test 'HAVE_CPU_NEON_SUPPORT' is performed and validated (executing the check file 'cmake/checks/cpu_neon.cpp')
the test 'HAVE_CPU_FP16_SUPPORT' is performed and validated (executing the check file 'cmake/checks/cpu_fp16.cpp')
and the resulting identified CPU/HW features baseline is identified as "NEON FP16" (see the linked logfile).
Finally, the libraries and example applications are successfully built.
2. With your settings, when FP16 support is enabled using OpenCV 3.2.0, CMake logs indicates that:
the test 'CXX_HAS_MFPU_NEON' is performed and validated
the test 'C_HAS_MFPU_NEON' is performed and validated
the check file 'cmake/checks/fp16.cpp' is ignored.
Finally, the libraries and example applications are not successfully built (with the same errors you described). I linked typed commands and both CMake configuration logs and compilation error logs.
I finally disabled FP16 support and rebuilt OpenCV 3.2.0 successfully. Prebuilts are accessible here.
From my investigation, it appears that some progress on FP16 compiler support for OpenCV has been integrated (reviewed and merged) following the release 3.2.0. This merge from June 21 is the latest work on this matter, a comment from #alalek introduces some previous pull requests.

What is the proper syntax for getting a Makefile to print the output directory of one of its output zip files?

I'm trying to edit an Android Makefile in the hopes of getting it to print out the directory (path) location of one the ZIP files it creates. Ideally, since the build process is long and does many things, I would like for it print out the pathway to the ZIP file to a text file in a different directory I can access later:
Pseudo-code idea:
# print the desired pathway to output file
print(getDirectoryOf(variable-name.zip)) > ~/Desktop/location_of_file.txt
The Makefile snippet where I would like to insert this new bit of code is shown below. I am interested in finding the directory of $(name).zip (that is specific file I want to locate):
# -----------------------------------------------------------------
# A zip of the directories that map to the target filesystem.
# This zip can be used to create an OTA package or filesystem image
# as a post-build step.
#
name := $(TARGET_PRODUCT)
ifeq ($(TARGET_BUILD_TYPE),debug)
name := $(name)_debug
endif
name := $(name)-target_files-$(FILE_NAME_TAG)
intermediates := $(call intermediates-dir-for,PACKAGING,target_files)
BUILT_TARGET_FILES_PACKAGE := $(intermediates)/$(name).zip
$(BUILT_TARGET_FILES_PACKAGE): intermediates := $(intermediates)
$(BUILT_TARGET_FILES_PACKAGE): \
zip_root := $(intermediates)/$(name)
# $(1): Directory to copy
# $(2): Location to copy it to
# The "ls -A" is to prevent "acp s/* d" from failing if s is empty.
define package_files-copy-root
if [ -d "$(strip $(1))" -a "$$(ls -A $(1))" ]; then \
mkdir -p $(2) && \
$(ACP) -rd $(strip $(1))/* $(2); \
fi
endef
As part of your rule's action (that is, as one of the lines indented by a tab, or as a command that follows a semicolon):
#echo $(dir $(variable-name).zip)
If output to a file is desired,
#echo $(dir $(variable-name).zip) >~/Desktop/location_of_file.txt
and you can make the > a >> if you prefer to append to the file rather than to overwrite.
UPDATE
What does it mean, "As part of your rule's action (that is, as one of the lines indented by a tab, or as a command that follows a semicolon)"?
Answer: It means that you can #echo like this:
file-to-be-built: dependencies
some-command
#echo foo
another-command
Or like this:
file-to-be-built: dependencies
#some-command; \
echo foo; \
another-command
(note the placement of the #). Or like this:
file-to-be-built: dependencies; #some-command; echo foo; another-command
But not like this:
#echo foo
file-to-be-built: dependencies
some-command
another-command
(Here is yet another parenthetical statement, which you can ignore if you like: The #, as you may know, suppresses Make's copying of the command itself to standard output. You can omit it, of course, but if you include it then you must put it before the specific command you don't want Make to copy. Formally, the versions without semicolons issue multiple, separate commands in their own, separate environments, using separate invocations of the shell; whereas the versions with semicolons only invoke the shell once, with a single environment, and leave it to the shell to separate and execute the commands. Does this make sense? Maybe not, if you just read it -- I can't make much sense of such tangled verbiage, either, even though I wrote it, myself -- but do try it with the # in the various spots indicated and it should soon start to make sense to you. The # is no big deal in any case, but it's there to use to keep Make's output clean if you understand how to use it.)

Want to compile native Android binary I can run in terminal on the phone

I've been trying for a couple days to compile a native ARM Android binary that will execute on my phone using a terminal application. I want to generate the same type of binary as the standard Posix binaries installed on the phone like ls, mkdir etc. I've downloaded the Android NDK under Mac OS X and have been able to compile simple ELF binaries without errors. However, when I transfer them to the phone, they always segfault. That is, they segfault when compiled with -static in GCC. If I don't use -static, they complain about not being linked, etc. Put simply, they don't work.
My hypothesis is that they are not linking to the Android standard C library properly. Even though I am linking my binaries with the libc provided by the NDK, they still don't work. I read that Android uses the Bionic C library, and tried to download source for it but I'm not sure how to build a library from it (it's all ARM assembly, it seems).
Is it true that the Android C library on the phone is different from the one provided with the Android NDK? Will the one included with the NDK not allow me to compile native binaries I can execute through a terminal? Any guidance here is greatly appreciated!
Update:
I finally got this to work using GCC 4.7.0 on Mac OS X. I downloaded the Bionic headers and then compiled a dynamically linked binary using the C library that comes with the Android NDK. I was able to get a test app to work on the phone using the phone's C lib (the binary was 33K). I also tried to statically link against the NDK's C library, and that also worked.
In order to get this all working I had to pass -nostdlib to GCC and then manually add crtbegin_dynamic.o and crtend_android.o to GCC's command line. It works something like this:
$CC \
$NDK_PATH/usr/lib/crtbegin_dynamic.o \
hello.c -o hello \
$CFLAGS \
$NDK_PATH/usr/lib/crtend_android.o
For static binaries, use "crtbegin_static.o." This is explained in the crtbegin_dynamic.S/crtbegin_static.S source.
For this experiment, I only used plain 'ol GCC 4.7.0 and Binutils 2.22. I also compiled GCC with newlib, but I am not actually linking my ARM binaries with newlib at all. I am forcing GCC/ld to link directly to the libc provided with the Android NDK, or in the case of dynamic binaries, to the libc on the phone.
Just use the android-ndk. And build a Android.mk like so.
include $(BUILD_EXECUTABLE) is what tells it build a executable instead of a JNI .lib
Android.mk
ifneq ($(TARGET_SIMULATOR),true)
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_CFLAGS += -Wall
LOCAL_LDLIBS := -L$(LOCAL_PATH)/lib -llog -g
LOCAL_C_INCLUDES := bionic
LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
LOCAL_SRC_FILES:= main.cpp
LOCAL_MODULE := mycmd
include $(BUILD_EXECUTABLE)
endif # TARGET_SIMULATOR != true
First, make sure you have the NDK:
http://developer.android.com/tools/sdk/ndk/index.html
Here is the easiest way to compile a C binary for your phone:
http://developer.android.com/tools/sdk/ndk/index.html
http://www.kandroid.org/ndk/docs/STANDALONE-TOOLCHAIN.html
Usually $NDK(may be different) =
Linux:
/home/<user>/android-ndk
Mac OS X:
/Users/<user>/android-ndk
In Terminal:
# create tool-chain - one line
# New method in ndk 12.
$NDK/build/tools/make_standalone_toolchain.py --arch arm --install-dir=/tmp/my-android-toolchain
# Old method.
#$NDK/build/tools/make-standalone-toolchain.sh --platform=android-3 --install-dir=/tmp/my-android-toolchain
# add to terminal PATH variable
export PATH=/tmp/my-android-toolchain/bin:$PATH
# make alias CC be the new gcc binary
export CC=arm-linux-androideabi-gcc
# compile your C code(I tried hello world)
$CC -o foo.o -c foo.c
# push binary to phone
adb push foo.o /data/local/tmp
# execute binary
adb /data/local/tmp/foo.o
Using CMake with the Android NDK is a nice way to compile Android console applications.
Download CMake and android-cmake (set it up like this). If your program is called main.c, then write the following in file CMakeLists.txt:
project(test)
cmake_minimum_required(VERSION 2.8)
add_executable(test ./main.c)
and run cmake -DCMAKE_TOOLCHAIN_FILE=$ANDTOOLCHAIN .
You will then have a Makefile for your program, you can run make to have your test executable.
In CMake, you can cross build using toolchain files.
From google developers:
cmake \
    -DCMAKE_TOOLCHAIN_FILE=$NDK/build/cmake/android.toolchain.cmake \
    -DANDROID_ABI=$ABI \
    -DANDROID_PLATFORM=android-$MINSDKVERSION \
    $OTHER_ARGS
CMake has its own built-in NDK support. Before CMake 3.21, this workflow is not supported by Android and is often broken with new NDK releases. Starting from CMake 3.21, the implementations are merged.
Starting from cmake 3.21 you can:
mkdir build
cd build
cmake ..
-DCMAKE_SYSTEM_NAME=Android
-DCMAKE_SYSTEM_VERSION=23 # API level. optional, recommanded
-DCMAKE_ANDROID_NDK=path/to/ndk
-DCMAKE_ANDROID_ARCH=arm # optional, recommanded
-DCMAKE_ANDROID_ARCH_ABI=armeabi # optional, recommanded
Note: in the command above, line endings (<line feed>) are not escaped, please don't copy-paste this command directly in your shell
See Cross Compiling for Android with the NDK for more information about variables, possible values, and determenation algorithms.
Try if if the agcc wrapper can help you as referenced in the Android-tricks blog. According to the blog post you want to use the bionic library, but the one already installed on the phone, not some separately compiled version.

Categories

Resources