Having trouble building a very simple OpenCV C++ .so library using CMake for use on Android

  android, c++, cmake, opencv

I am trying to use CMake to create a .so library that uses OpenCV for use on Android.

I have no prior experience with CMake before this. I got a couple things working with CMake so far. I successfully built and ran a simple OpenCV program for windows and I’ve also created a .so file that doesn’t use OpenCV. I was able to successfully called that .so from a Unity program on Android. My goal is to create an OpenCV plugin for Unity. I turned to CMake because I tried making a simple .so in Visual Studios, but my Unity program couldn’t find it (though I was able to create a .dll that used OpenCV for a windows Unity program using Visual Studios that worked).

I have both r19c Android NDK and OpenCV 4.5.2 Android SDK on my C drive.

My code file is test.cpp, and it very simply uses some OpenCV elements. It’s as follows:

#include "opencv2/opencv.hpp"

extern "C" int get_number()
    cv::Mat img(2,2, CV_8UC3, cv::Scalar(126,0,255));
    cv::Size size = img.size();
    int width = size.width;
    return width;

I am thinking that my error may be in the CMakeLists.txt file, that I am probably pointing to the wrong OpenCV folders.

My CMakeLists.txt is:

cmake_minimum_required(VERSION 3.20)
set(OpenCV_DIR C:/OpenCVAndroid/sdk/native/jni/abi-arm64-v8a)
set(OpenCV_INCLUDE_DIRS C:/OpenCVAndroid/sdk/native/jni/include)

project( test )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_library( test SHARED test.cpp )

and I’m using a .bat file that has the following lines:

rmdir /s /q build
mkdir build
cd build
"C:/Program Files/CMake/bin/cmake.exe" ^
  -S=C:Usersm_knuDesktoptest ^
  -G"MinGW Makefiles" ^
  -DCMAKE_TOOLCHAIN_FILE=C:r19candroid-ndk-r19cbuildcmakeandroid.toolchain.cmake ^
  -DCMAKE_MAKE_PROGRAM=C:r19candroid-ndk-r19cprebuiltwindows-x86_64binmake.exe ^
  -DANDROID_NDK=C:r19candroid-ndk-r19c ^
  -DANDROID_ABI=arm64-v8a ^
  -DCMAKE_MODULE_PATH="C:/OpenCVAndroid/sdk/native/jni/abi-armeabi-v7a" ^
"C:/Program Files/CMake/bin/cmake.exe" --build .


all three files are located in the same folder.

When I run the .bat file I get the following output:

C:Usersm_knuDesktoptest>rmdir /s /q build

C:Usersm_knuDesktoptest>mkdir build

C:Usersm_knuDesktoptest>cd build

C:Usersm_knuDesktoptestbuild>"C:/Program Files/CMake/bin/cmake.exe"   -S=C:Usersm_knuDesktoptest   -G"MinGW Makefiles"   -DCMAKE_BUILD_TYPE=Release   -DCMAKE_TOOLCHAIN_FILE=C:r19candroid-ndk-r19cbuildcmakeandroid.toolchain.cmake   -DCMAKE_MAKE_PROGRAM=C:r19candroid-ndk-r19cprebuiltwindows-x86_64binmake.exe   -DANDROID_NDK=C:r19candroid-ndk-r19c   -DANDROID_NATIVE_API_LEVEL=android-21   -DANDROID_ABI=arm64-v8a   -DCMAKE_MODULE_PATH="C:/OpenCVAndroid/sdk/native/jni/abi-armeabi-v7a"
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/r19c/android-ndk-r19c/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/r19c/android-ndk-r19c/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenCV: C:/OpenCVAndroid (found version "4.5.2")
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/m_knu/Desktop/test/build

C:Usersm_knuDesktoptestbuild>"C:/Program Files/CMake/bin/cmake.exe" --build .
[ 50%] Building CXX object CMakeFiles/test.dir/test.cpp.o
[100%] Linking CXX shared library libtest.so
CMakeFiles/test.dir/test.cpp.o: In function `get_number':
C:Usersm_knuDesktoptest/test.cpp:5: undefined reference to `cv::Mat::Mat(int, int, int, cv::Scalar_<double> const&)'
C:Usersm_knuDesktoptest/test.cpp:9: undefined reference to `cv::Mat::~Mat()'
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [libtest.so] Error 1
make[1]: *** [CMakeFiles/test.dir/all] Error 2
make: *** [all] Error 2

Press any key to continue . . .

As I mentioned earlier, my guess is that I’ve incorrectly referenced the OpenCV directories, but I wouldn’t be surprised if I’m missing other things.

Source: Windows Questions C++