Compile opencv library suitable for NDK18 and above in Windows environment

Preface

    Because NDK canceled GUN support after updating to version 18, gnustl was also removed (refer to https://github.com/android-ndk/ndk/wiki/Changelog-r18 ), and the Android version of opencv uses It is compiled by gunstl (you can check android.toolchain.cmake in platforms\android under the source code folder), so after updating the NDK, if you directly compile the original project, some functions will only be declared but not implemented. Many people have chosen to downgrade the NDK version, but obsessive-compulsive disorder means that they cannot bear to use the latest version and upgrade decisively.

    I did it under Windows 10 and used the source code of opencv3.4.2 (the most "comfortable" version) to compile. I haven't tried other versions yet. It should be similar. I guess you can draw inferences from one example.

    After the NDK update, there is only one clang/clang++ compiler left. We only need to use it to recompile the source code of opencv.

references:

Compile opencv4android+opencv_contrib_Xiao19950820's blog under Windows - CSDN blog

Android NDK 18 OPENCV upgrade problem_fff2666's column-CSDN Blog

Zero, pitfalls encountered

    I try to use NDK19 to compile opencv3.4.2, and an error will be reported when the build reaches 100%. However, using the same method to compile with NDK17 will be no problem, and the compiled library will not report an error when used under NDK19. I don’t know if 3.4.2 does not support such a new NDK. After updating the library, if the original Android project cannot be compiled, clean it and then relink the C++ part.

    In addition, the library compiled with clang is very large. I don't know why. It was originally about 10 MB, but this one is 100 MB. I made a simple demo and found that the library size did not affect the APP size, so I left it alone for the time being.

    Then, for version 3.4. Some files cannot be found, but this problem was not found when compiling 4.1.0, which is a CXX version. This is amazing.

1. Environment configuration

    This step is similar to the content of this article on compiling opencv4android+opencv_contrib_Xiao19950820's blog-CSDN blog under Windows , so I won't go into details here (I'm too lazy to install the environment again). Generally speaking, we need to install cmake, mingw, ant and configure environment variables.

    There are also python and LLVM. Since I have these two things installed here, I am not sure whether they are necessary. You can download these two from the official website:

    Download Python | Python.org

    LLVM Download Page

    Just select the corresponding version of Pre-Built Binaries for LLVM. They will have settings for whether to add environment variables during installation. I chose to add them.

2. Source code download

    There are many ways to download the source code. I went directly to github to download: https://github.com/opencv

       

    Note, try not to download the master version, choose the version we need to download.

    

    If you need the contrib library, you need to download the same version as the opencv library, otherwise the compilation will report an error~

    After downloading, unzip it and you're done.

    

    This is my folder structure.

3. Compile

    It is worth noting that version 3.4.2 only supports Android SDK Tools version 25.2.5. This needs to be downloaded by yourself ( https://dl.google.com/android/repository/tools_r25.2.5-windows.zip ). I don't know if there is such a problem with other OpenCV versions. After downloading, replace it directly (delete the original one first, if you are worried, please make a backup copy) the tools folder under the SDK directory is fine.

    First of all, open cmake-gui, the graphical interface is much more convenient to operate (I think so).

    

    Fill in the path to store the opencv source code in place 1 in the figure, and fill in the path to store the generated result in place 2.

    Then click "add entry" to add some flags.

    By the way, which flag bits to add are determined by the tool chain used. Most tutorials will let us use the tool chain in the opencv source code. If this is the case, it will not be adapted to the latest NDK. So here we have to choose to use the toolchain that comes with NDK. The toolchain is located at ndk-bundle\build\cmake\android.toolchain.cmake.

    There is ANDROID_ABI to be added, this adds the ABI you need according to your needs

    

    Then there is ANDROID_STL, because gunstl is no longer supported, you can use c++_shared or c++_static here.

    

PS: If it prompts that there is no Android SDK path after Configure, then add another ANDROID_HOME, the value is the path of the SDK, such as: C:\software\Android_SDK

After adding, click the "Configure" button, select mingw as the build tool, then choose to use the toolchain to cross-compile, and click "Next".

    

    After that, the tool chain selection will appear. Here we choose the tool chain that comes with NDK, which is located in /build/cmake/android.toolchain.cmake in the NDK directory. If you are using the SDK installed by Android Studio, the NDK directory is the ndk-bundle under the SDK directory. I am not sure about the rest. Then click Finish.

    It can be found that cmake has automatically selected the compiler as clang

    

PS: If you encounter the following error:

Android SDK: Can't build Android projects as requested by BUILD_ANDROID_PROJECTS=ON variable.

The complete error is as follows:

CMake Error at cmake/android/OpenCVDetectAndroidSDK.cmake:184 (message):
  Android SDK: Can't build Android projects as requested by
  BUILD_ANDROID_PROJECTS=ON variable.

  Use BUILD_ANDROID_PROJECTS=OFF to prepare Android project files without
  building them
Call Stack (most recent call first):
  CMakeLists.txt:657 (include)

Just search for BUILD_ANDROID_PROJECTS in the search column, and then remove the hook, or remove the hooks of BUILD_JAVA and BUILD_ANDROID_EXAMPLES.

    Then select the options you need, then click "Configure" until there are no red-bottomed options, then click "Generate".

    

    Finally, enter the command line tool CMD and enter the address filled in the red box 2 in the first picture in the third section.

    cd (space) (the address filled in the red box 2 of the first picture in Section 3)

    ps: If the address is not on drive c, you need to use the drive letter to enter the corresponding drive. For example, if mine is on drive d, just enter d: and press Enter, and then use cd.

    Use the mingw32-make command to generate, this process takes a long time.

    

If an error like the following occurs, the NDK version may be too high. Just downgrade the NDK version.

bionic/libc/include/bits/fortify/stdio.h:76: error: undefined reference to '__vsnprintf_chk'
bionic/libc/include/bits/fortify/unistd.h:159: error: undefined reference to '__read_chk'
bionic/libc/include/bits/fortify/unistd.h:174: error: undefined reference to '__write_chk'
bionic/libc/include/bits/fortify/unistd.h:174: error: undefined reference to '__write_chk'

    Then use the mingw32-make install command to install it.

    The result is in the install/sdk/native folder under this path, including header files and library files.

At this point, the compilation work is completed.

This is my first time writing an article, so I apologize for the poor language organization. If you encounter any problems, please feel free to consult.

    

Guess you like

Origin blog.csdn.net/qq_19313495/article/details/89709405