- OpenCV in Android Studio’s NDK fails with «undefined reference» error #17519
- Comments
- orangesomethingorange commented Jun 10, 2020 •
- alalek commented Jun 10, 2020
- orangesomethingorange commented Jun 14, 2020
- orangesomethingorange commented Jun 14, 2020
- alalek commented Jun 14, 2020 •
- orangesomethingorange commented Jun 15, 2020
- alalek commented Jun 15, 2020
- orangesomethingorange commented Jun 16, 2020 •
- orangesomethingorange commented Jun 17, 2020
- orangesomethingorange commented Jun 17, 2020
- orangesomethingorange commented Jun 18, 2020
- alalek commented Jun 18, 2020
- orangesomethingorange commented Jun 23, 2020 •
- orangesomethingorange commented Jun 23, 2020
- ndk-build
- alalek commented Jun 23, 2020
- orangesomethingorange commented Jun 24, 2020
- ‘undefined reference to’ функция, использующая Android Studio NDK
- undefined reference to `__android_log_print’
- Related Posts
- android – How do I display a CalendarView in an AlertDialog?
- How do I convert a PSD design to Android xml?
OpenCV in Android Studio’s NDK fails with «undefined reference» error #17519
Comments
orangesomethingorange commented Jun 10, 2020 •
System information (version)
- OpenCV => ❔ 4.3 / 3.4
- Operating System / Platform => ❔ Linux Ubuntu 16.04
- Compiler => ❔ Android Studio (Gradle / Ninja / CMake / clang++)
Detailed description
I’m trying to use OpenCV on Android’s NDK.
I have two similar environments — one integrated with OpenCV 4.3 and the other with OpenCV 3.4.
I created an Android C++ app.
For the 4.3 integration I followed this tutorial.
For the 3.4 integration I followed this tutorial.
I have a very basic native-lib.cpp file (see below), which is trying to use cv::imread .
In my code I provide the last trial — which includes the OpenCVLoader.initAsync instance, but I also tried the OpenCVLoader.initDebug way and System.loadLibrary(«opencv_java4») , but the error remains.
I’m aware of the following issues:
and noticed the recent update regarding link issues (which I assumed was my problem):
I tried to build for my SDK version or against libc++, as described in the following:
and noticed some other issues, which do not use exactly the same setup (for example, using ndk-build instead of cmake):
but failed to find anything new.
When trying to run the app I get the following errors:
Isn’t undefined reference a linker issue?
Did I forget a compiler flag or something?
Steps to reproduce
Follow one of the tutorials (4.3 tutorial or 3.4 tutorial) to integrate OpenCV library into Android Studio.
Create the following simple native-lib.cpp:
and call it from this simple MainActivity.java:
NOTES:
- When calling OpenCVLoader.initAsync , both the 4.3 and the 3.4 version use the OPENCV_VERSION_3_4_0 flag, since OPENCV_VERSION on 4.3 results in an error. I also tried the OpenCVLoader.initDebug and it produces the same error.
- When commenting out the image = imread line in native-lib.cpp , the app runs OK, and returns the string (all cases).
the app’s build.gradle:
CMakeLists.txt:
Error
ERROR:
undefined reference to «cv::imread»
CLANG++:
/path/to/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ —target=aarch64-none-linux-android29 —gcc-toolchain=/path/to/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64 —sysroot=/path/to/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -frtti -fexceptions -O0 -fno-limit-debug-info -Wl,—exclude-libs,libgcc.a -Wl,—exclude-libs,libgcc_real.a -Wl,—exclude-libs,libatomic.a -static-libstdc++ -Wl,—build-id -Wl,—fatal-warnings -Wl,—no-undefined -Qunused-arguments -shared -Wl,-soname,libnative-lib.so -o /path/to/app/build/intermediates/cmake/debug/obj/arm64-v8a/libnative-lib.so CMakeFiles/native-lib.dir/native-lib.cpp.o /path/to/opencv-4.3.0-android-sdk/OpenCV-android-sdk/sdk/native/libs/arm64-v8a/libopencv_java4.so /path/to/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/29/liblog.so -ljnigraphics -llog -ldl -lz -latomic -lm
The text was updated successfully, but these errors were encountered:
alalek commented Jun 10, 2020
What is about OpenCV Android samples from SDK package?
orangesomethingorange commented Jun 14, 2020
What is about OpenCV Android samples from SDK package?
I assume you’re referring to this example
These are somewhat old and refer to Android app development on Eclipse, using ndk-build .
My (and maybe every other recent) Android app project is built on Android Studio, with CMake.
That’s exactly the reason I tried the above tutorials — working with ndk-build on Eclipse seemed obsolete.
Also, the Hello World sample uses the Camera, which I have no intention of using.
All I need is just some basic functionality 🙂
orangesomethingorange commented Jun 14, 2020
I can see that Mat is well defined, for example, but imread not.
Could this be a linking issue with CMake?
alalek commented Jun 14, 2020 •
Unpack OpenCV SDK and load «samples» into Android Studio.
You have found a link on EOL version of OpenCV. «gradle» support is available on 4.x versions.
Usage questions should go to Users OpenCV Q/A forum: http://answers.opencv.org
orangesomethingorange commented Jun 15, 2020
Unpack OpenCV SDK and load «samples» into Android Studio.
You have found a link on EOL version of OpenCV. «gradle» support is available on 4.x versions.
Usage questions should go to Users OpenCV Q/A forum: http://answers.opencv.org
Not sure what you mean that:
Following the docs link at docs.opencv.org, from this repository, I get to this tutorial, and following opencv.org/ or a Google search I get to the link I sent before.
Both cases describe the integration of OpenCV (I’ve tried versions 4.3 or 3.4) into Eclipse.
I currently have Android Studio integrated with both OpenCV versions (in separate projects), but trying to use it via Gradle results in the above described undefined reference error.
Do you have a link describing the Gradle support you’ve mentioned?
alalek commented Jun 15, 2020
Almost all Android tutorials are outdated — added the «documentation» label.
OpenCV 4.3.0 binaries were built using NDK r18b.
Related symbols are:
Note about the std::__ndk1:: prefix.
- use NDK16 in your application which uses __ndk1 for std
- or rebuild OpenCV binaries from sources with NDK21.1
orangesomethingorange commented Jun 16, 2020 •
Almost all Android tutorials are outdated — added the «documentation» label.
OpenCV 4.3.0 binaries were built using NDK r18b.
Related symbols are:
Note about the std::__ndk1:: prefix.
Downgrading 4.3 project to NDK16 keeps the undefined reference to cv::imread exactly the same.
Trying to build 4.3 project with NDK18 (for symbols), posed an Expected NDK STL shared object file libgnustl_shared.so issue.
Downgrading 3.4 project to NDK16 enabled it to build, but it fails to run when including imread , and crash on app startup. I can’t see anything on the debug log, and the Logcat seems to run the same regardless. I’m not even sure what exactly crashes and where to look.
(*) BTW, for anyone else’s future reference, using NDK16 poses a different issue — it only supports up to API level 26/27, and I need API 28 (above preferred).
- A CMake build with NDK21 was successful, produced some static libraries ( libopencv_calib3d.a , libopencv_core.a , libopencv_dnn.a , libopencv_features2d.a , etc. ) and shared library libopencv_java4.so .
Though linking to the shared library reports successful, I can’t find it using the NDK.
Something is still missing.
orangesomethingorange commented Jun 17, 2020
Here’s an example for a build output (after a successful previous build):
Linking the shared library didn’t pose any errors, but still — there’s no OpenCV code which may be included in the NDK.
Any «#include [some_opencv_header]» results in fatal error: ‘[some_opencv_header]’ file not found
orangesomethingorange commented Jun 17, 2020
The same build, with adding (in CMakeLists.txt ) the following:
results in undefined reference error from protobuf :
orangesomethingorange commented Jun 18, 2020
I still can’t figure out what’s preventing the link / packaging.
Here’s a CMakeLists.txt file I’m using to configure build and link.
alalek commented Jun 18, 2020
set(CMAKE_ANDROID_STL_TYPE gnustl_static ) # GNU libstdc++ Static
By default OpenCV uses:
I believe different symbols come from this difference. We need the same compiler and the same compiler flags.
orangesomethingorange commented Jun 23, 2020 •
Thanks for the comment @alalek .
I’m using clang++ and changed CMAKE_ANDROID_STL_TYPE gnustl_static to CMAKE_ANDROID_STL_TYPE c++_shared .
I still get the same error, though.
Build log shows that build and link went OK, so my guess is that now it’s a packaging issue. I think that Gradle fails to pack the library into the APK for some reason.
From here I can assume it all should be packed correctly (though I also tried creating jniLibs directory and copying the shared library into it).
Still, I can’t access the OpenCV library from native code 😞
orangesomethingorange commented Jun 23, 2020
ndk-build
Another attempt to build OpenCV, using ndk-build instead of cmake (rest is the same — using Gradle in Android Studio, with clang++).
Android.mk:
Application.mk:
App’s build.gradle:
I manually build the library by cd -ing into the app/src/main/jni (where the .mk files are) and running /path/to/ndk-build , before running build with Android Studio (as suggested in step 6 of the Native/C++ section of this OpenCV doc).
Shared library is being created and build and link seem to be successful, when no calls are done from native code.
If and when I try to include some OpenCV code in native code ( #include or #include «opencv2/core.hpp» for example) I get file not found error.
Including function calls in code, again, results in undefined reference error.
alalek commented Jun 23, 2020
Another attempt to build OpenCV
I see that you trying to use OpenCV, instead of rebuilding it from the sources.
Again, existed OpenCV binaries are not suitable for your configuration with your Android NDK/SDK versions.
You should use «platforms/android/build_sdk.py» script first and get new libopencv_java4.so.
Android package build logs are here.
orangesomethingorange commented Jun 24, 2020
Another attempt to build OpenCV
I see that you trying to use OpenCV, instead of rebuilding it from the sources.
Again, existed OpenCV binaries are not suitable for your configuration with your Android NDK/SDK versions.
You should use «platforms/android/build_sdk.py» script first and get new libopencv_java4.so.
Android package build logs are here.
You’re correct — I was, again, trying to use the packaged SDK.
Regarding build_sdk.py :
This script was used by Gradle. In previous attempts to build it locally the script was used and created a new libopencv_java4.so shared library. When I tried to use it — it got me the undefined reference error (the first time).
After your comment I thought — maybe you meant that I should run the script standalone?
Invoking the script outside the environment (not setting the —use_android_buildtools flag) makes the script call cmake v.3.6 via the NDK, and makes it fail on cmake_minimum_required error, when called by cmake v.3.5.1.
Also, exporting all the CMAKE variables and debugging is just like creating the whole environment from scratch.
So I ran build_sdk.py with the —use_android_buildtools flag, and no libopencv_java4.so was created (all the build and output folders were created, but not the library).
So, I’m guessing you meant to use it via Gradle, correct?
Источник
‘undefined reference to’ функция, использующая Android Studio NDK
У меня есть 3 класса, native-lib.cpp, tracker.cpp и tracker.hpp. Однако во время сборки я получаю сообщение об ошибке, если я использую функцию isTracking из tracker.cpp в файле navtive-lib.cpp.
- Что пошло не так: выполнение выполнено для задачи ‘: app: externalNativeBuildDebug’.
Сбой команды сборки. Ошибка при выполнении процесса /home/user/Android/Sdk/cmake/3.6.4111459/bin/cmake с аргументами <--build/home/user/AndroidStudioProjects/project/app/.externalNativeBuild/cmake/debug/x86 --target tracker>[1/2] Создание объекта CXX CMakeFiles/tracker.dir/src/main/cpp/native-lib.cpp.o [2/2] Связывание общей библиотеки CXX../../../../build/intermediates/cmake/debug/obj/x86/libtracker.so FAILED:: &&/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ —target = i686-none-linux-android — -g cc-toolchain =/home/user/Android/Sdk/ndk-bundle/toolchains/x86-4.9/prebuilt/linux-x86_64 —sysroot =/home/user/Android/Sdk/ndk-bundle/sysroot -fPIC -система /home/user/Android/Sdk/ndk-bundle/sysroot/usr/include/i686-linux-android -D__ANDROID_API__ = 23 -g -DANDROID -ffunction- разделы -funwind-tables -fstack-protector-strong -no-canonical-prefixes -mstackrealign -Wa, —noexecstack -Wformat -Werror = format-security -O0 -fno-limit-debug-info -Wl, —exclude-libs, libgcc.a —sysroot/home/user/Android/Sdk/ndk-bun dle/platform/android-23/arch-x86 -Wl, —build -id -Wl, — warn -shared -textrel -Wl, — фатальные предупреждения -Wl, — no-undefined -Wl, — z, noexecstack -Qunused-arguments -Wl, — z, relro -Wl, — z, теперь -shared -Wl, -soname, libtracker.so -o../../../../build/intermediates/cmake/debug/obj/x86/libtracker.so CMakeFiles/tracker.dir/src/main/cpp/tracker.cpp.o CMakeFiles/tracker.dir/src/main/cpp/native-lib.cpp.o/home/user/Android/Sdk/ndk-bundle/platforms/android-23/arch-x86/usr/lib/liblog.so../../../../src/main/jniLibs/x86/libopencv_java3.so -lm «/home/user/Android/Sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/x86/libgnustl_static.a» &&: /home/user/AndroidStudioProjects/project/app/src/main/cpp/native-lib.cpp:28: ошибка: неопределенная ссылка на «Tracker :: isTracking()» clang++:
ошибка: команда компоновщика завершилась с кодом выхода 1 (используйте -v, чтобы увидеть вызов) ninja: build stopped: подкоманда не удалась.
Источник
undefined reference to `__android_log_print’
Posted by: admin November 28, 2017 Leave a comment
What is wrong with my make file?
Try the following in your Android.mk file:
You need to add
If you use Android Studio and gradle, it ignores Android.mk. Add this to your build.gradle file:
If you upgrade to Android Studio 2.1, above answers do not work, need use ldLibs.add() to load the lib as below:
For Android Studio 2.2 and tools.build:gradle:2.2.0 using CMake add or edit row in CMakeLists.txt:
Thats connecting log library to yours.
We can link a shared library in Android in 3 ways.
In below 3 cases, the lines mentioned should be added in Android.mk
So here are the three ways.
For some reason if 1 doesnt work(it did not work for me), You can try below 2 ways
Of course you also need to include #include in your C/H file.
If using the new Gradle NDK integration in Android Studio 1.3, you need to add ldLibs = [“android”, “log”] to your android.ndk options – Stephen Kaiser Sep 24 at 4:20
use ldLibs.addAll([«android», «log»]) for the experimental plugin
Yes, you do need to add: LOCAL_LDLIBS := -llog as the other answers/comments have specified, however the original question did not specify if he use the jni library as: LOCAL_JNI_SHARED_LIBRARIES or as LOCAL_REQUIRED_MODULES .
I can pretty much say for sure that he has it used it as: LOCAL_REQUIRED_MODULES because of the LOCAL_EXPORT_LDLIBS := -llog in the question… unless that was added after an edit.
If you use LOCAL_REQUIRED_MODULES the shared library is installed in /system/lib instead of into the apk, because it is a required module. Therefore you will need to add LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog instead of just LOCAL_LDLIBS := -llog so that when the build system is building & linking the jni shared library, it will have the -llog definitions in the correct place, available to be built under $OUT/root/system/lib . Otherwise you will continue to get the same answer, even if you only add LOCAL_LDLIBS := -llog .
So, those who commented that the -L is not needed, and the other answer was correct, they were actually incorrect in this situation.
This helped for me:
In the android studio version 2.2 and higher, there is inbuilt support for CPP when you create a new project. Also, the liblog.so is included by default. Nothing to be done apart from including the header file (android/log.h).
Checkout app/CMakeLists.txt that is created by the studio when we create new android studio project. We can see that the find_library() block and target_link_libraries() block for loglib are already present.
Also, pay attention towards the function syntax. It should be:
__android_log_print (int priority, const char *tag, const char *fmt,…);
In my case, I had left out tag parameter and ended up spending good 3 days in figuring it out.
Related Posts
android – How do I display a CalendarView in an AlertDialog?
Questions: I’m trying to display the CalendarView in an Alert Dialog, but all that shows up is the month/year and the days of the week. These are the contents of the layout file:
How do I convert a PSD design to Android xml?
Questions: Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want to improve this question? Update the question so it’s on-topic for Stack Over.
Источник