I try to compile a .so library using Visual Studio 2019 along with OpenCV Android in order to use this library in Unity.
There are some answers on how to configure Visual Studio to use OpenCV Android (here or here) but none of these work for me. Below you can see my configurations.
Visual Studio 2019 (running on Windwos 10)
android-ndk-r21e // also tried with android-ndk-r15c android-ndk-r16b and android-ndk-r17c
OpenCV Android 4.5.2 // also tried with OpenCV Android 4.0.0, 4.0.1 and 4.1.0
My settings in Visual Studio 2019 look as follows:
Additional Include Directories âPath to OpenCV_4_5_2_Android\sdk\native\jni\includeâ
Code Generation Enable C++ Exceptions âYes(-fexceptions)â
Language C++17(-std=c++1z)
Precompilied Headers Not using Precompiled Headers
Linker
- General
Additional Library Directories Path to OpenCV_4_5_2_Android\sdk\native\libs\armeabi-v7a
- Input
Additional Dependencies Path to OpenCV_4_5_2_Android\sdk\native\libs\armeabi-v7a\libopencv_java4.so
My Source.cpp I try to compile is just a single function for testing purposes
#include <opencv2/core.hpp>
extern "C" float test(float a, float b)
{float c = a * b; return c;}
Wich gives me the following errors:
E0035 #error directive: This constructor has not been ported to this platform
E0020 identifier "__fp16" is undefined
use of undeclared identifier 'ANDROID_LOG_INFO'
The ANDROID_LOG_INFO error can be fixed when I add #include "android/log.h" at the top of the file that throws this error. But the other two errors still remain.
I too have this same problem. I will add that I tried it with OpenCV 3.4.14 (in addition to 4.5.2).
I also followed the same directions from the sites linked above (and was unable to find any other pages that talked about creating a .so through Visual Studios).
I found that the error goes away when no OpenCV header file is included, which of course defeats the purpose. I have very little C++ experience but I believe OpenCV must somewhere have its own âtypedef __fp16 ???â line, because I can generate the error by adding to my code (after removing any OpenCV includes) my own typedef line. By adding
typedef __fp16 float16_t;
This is in fact what arm_neon.h is defining it as, and arm_neon.h is included as an External Dependency (I donât know where the statement is that ultimately points to it so that it is included).
So I believe an OpenCV header file defines __fp16 as something and arm_neon.h also defines it as something (possibly the same thing, either way this error occurs).
Same problem, exactly. To add to the data here, I followed Amin Ahmadiâs tutorial from scratch, including cross compiling from the source, using 4.0.1. and NDK 16rb to keep with his example and my clientâs needs, and it didnât help.
I suspect it is an issue with the LLVM toolchain defaults that Visual Studio now populates into the Shared Library solution template, but Iâm not sure what weâd replace those with.
Also, force-fixing the float16 issues in arm_neon.h uncovers a whole lot of other errorsâŚ
UPDATE: This is going to sound really dumb, but try simply building it (not running) with your main .cpp file highlighted. I find that it succeeds despite the slew of complaints from arm_neon.h. which could just be some kind of Intellisense issue. Whether or not what the compiler spit out for me works is TBD.
donât worry about compiler warnings. plenty of libraries were/are written with hardly any compiler warnings enabled⌠and then the craft evolves and ever more warnings are enabled by default, which causes warnings all over the place. youâd have to look at specific instances and judge them.
do be careful if there were actual errors. some build systems show you those once, but still generate an object file for that module (or some other thing that causes the source to be ignored next time), which means it wonât be compiled again (until the source changes)⌠so you wonât see those errors again either, until you do a clean build of everything.
Well, In Unity on my OpenPlus 6T I get a âFailed to find cascade definitionâ instead of the old âMissing DLLâ error, so I guess thatâs progress? Iâm using the object finding code, and I must have just put the xml file in the wrong spot.
I finally tried to compile my .so libraries in Android Studio. There I also get some compiler errors but as crackwitz said my libraries are build anyways somehow.
Did you get the camera stream on android to work with the code? My WebCamTexture is definitely working and showing my face in the camera, but OpenCV doesnât find the stream.
Cool. Can I ask what android ndk you used with it? The one I solved the issues for was 4.0.1 â I assumed it would also work for 4.5.2 but it hasnât yet for me and I need to update, but 21e seems broken.
Youâre right, Iâm not sure that it does. I was able to build 4.5.2 from the source using ndk 21eâs toolchain instead of the one that came with 4.5.2 (on the advice of the github repositoryâs developers), but the Visual Studio compiler still gives a million errors about not recognizing all the std::math functions. Hrm.
Any advice here, anyone? Not asking to troubleshoot Visual Studio, but I am wondering why errors like these would come up if everything else is correct:
error : no member named âislessâ in the global namespace
1>using ::isless;
error : no member named âislessequalâ in the global namespace
1>using ::islessequal;
(etc.)
Figured out how to actually build from 4.5.2 successfully (my previous solution above only worked for 4.0.1).
-build from the source with CMake using Android NDK 21e, but instead of the toolchain in OpenCV 4.5.2, use the toolchain in the NDK build folder (this comes from the horseâs mouth, and it works).
-once youâre in Visual Studio, use these changes:
in Properties/ Configuration / General, use LLVM libc++ static library instead of the GNU static library. This is because GNUc++ was removed from Android NDKs after 17. THAT is why all those nutty errors pop up with standard libaries missing when you try to build with it.
In Linker / Input, put the library file name(s) in âLibrary Dependenciesâ instead of âAdditional Dependenciesâ, or the linker will never find them.
-do everything else the same as most tutorials (c++ 11, etc.)
So I built the library and ignored the errors (that is any that didnât prevent building). But I still canât call the library from Unity.
I tried a different route and made libraries strictly using text editors and CMake yesterday and the day before. I created both windows and Android libraries that I successfully called from Unity in their respective builds (for both 32-bit and 62-bit on Android), but I am having trouble getting OpenCV included in those builds (but Iâll post separately for that somewhere, I have pretty much no experience with CMake). However, this is proof that I am able to call .so libraries from Unity.
Because of my troubles with CMake I decided to try to just get a bare bones .so library built in Visual Studios from a clean project. Nothing in the code but a function that returns a hardcoded int. However, when using this library Unity still comes back with an error in Logcat stating:
âDLLNotFoundExepction: Unable to load DLL ânameâ: The specified module could not be found.â
In the same run I have my simple CMake made .so library that has the same simple function, and it works.
In Visual Studios I again selected âDynamic Shared Library (Android)â and left all the project settings alone except I set:
LLVM libc++ shared library (c++_shared)
C++11 (-std=c++11)
Only other settings that were bolded, though they were set this way from the start were:
Clang 5.0
Dynamic Library (.so)
Anybody know why such a simple setup wonât work?