Search code examples
androidc++opencvcmakecross-compiling

Compiling OpenCV for Android on Windows with CMake - make still tries to link "dll" files


This is how I set up the cmake, in the root of OpenCV 4.9.0. repository:

cmake -DCMAKE_ANDROID_NDK=D:\...\lib\AndroidSDK\ndk\25.1.8937393 \
 -DCMAKE_SYSTEM_NAME=Android \
 -B..\OpenCV_4.9.0_builds\android_clang_ndk25 -S. \
 -DANDROID_TOOLCHAIN=clang++ \
 -DANDROID_ABI=arm64-v8a \
 -D CMAKE_BUILD_TYPE=Release \
 -D ANDROID_NATIVE_API_LEVEL=24 \
 -D WITH_CUDA=OFF \
 -D WITH_MATLAB=OFF \
 -D BUILD_ANDROID_EXAMPLES=OFF \
 -D BUILD_DOCS=OFF \
 -D BUILD_PERF_TESTS=OFF \
 -D BUILD_TESTS=OFF \
 -D ANDROID_STL=c++_shared \
 -D BUILD_SHARED_LIBS=ON

But when I then try to go to android_clang_ndk25, I can see it still uses mingw header files and mingw linker.

I compile with:

D:\...\ndk\21.1.6352462\prebuilt\windows-x86_64\bin\make.exe install

And get errors:

ld: error: undefined symbol: std::__once_callable
>>> referenced by C:/Qt/Tools/mingw1120_64/lib/gcc/x86_64-w64-mingw32/11.2.0/include/c++/mutex:710
>>>               objects.a(caffe_importer.cpp.obj):(cv::dnn::dnn4_v20231225::(anonymous namespace)::CaffeImporter::extractLayerParams(google::protobuf::Message const&, cv::dnn::dnn4_v20231225::LayerParams&, bool))
>>> referenced by C:/Qt/Tools/mingw1120_64/lib/gcc/x86_64-w64-mingw32/11.2.0/include/c++/mutex:718
>>>               objects.a(caffe_importer.cpp.obj):(cv::dnn::dnn4_v20231225::(anonymous namespace)::CaffeImporter::extractLayerParams(google::protobuf::Message const&, cv::dnn::dnn4_v20231225::LayerParams&, bool))
>>> referenced by C:/Qt/Tools/mingw1120_64/lib/gcc/x86_64-w64-mingw32/11.2.0/include/c++/mutex:710
>>>               objects.a(caffe_importer.cpp.obj):(cv::dnn::dnn4_v20231225::(anonymous namespace)::CaffeImporter::extractLayerParams(google::protobuf::Message const&, cv::dnn::dnn4_v20231225::LayerParams&, bool))
>>> referenced 1093 more times

ld: error: undefined symbol: std::__once_call
>>> referenced by C:/Qt/Tools/mingw1120_64/lib/gcc/x86_64-w64-mingw32/11.2.0/include/c++/mutex:712
>>>               objects.a(caffe_importer.cpp.obj):(cv::dnn::dnn4_v20231225::(anonymous namespace)::CaffeImporter::extractLayerParams(google::protobuf::Message const&, cv::dnn::dnn4_v20231225::LayerParams&, bool))
>>> referenced by C:/Qt/Tools/mingw1120_64/lib/gcc/x86_64-w64-mingw32/11.2.0/include/c++/mutex:719
>>>               objects.a(caffe_importer.cpp.obj):(cv::dnn::dnn4_v20231225::(anonymous namespace)::CaffeImporter::extractLayerParams(google::protobuf::Message const&, cv::dnn::dnn4_v20231225::LayerParams&, bool))
>>> referenced by C:/Qt/Tools/mingw1120_64/lib/gcc/x86_64-w64-mingw32/11.2.0/include/c++/mutex:712
>>>               objects.a(caffe_importer.cpp.obj):(cv::dnn::dnn4_v20231225::(anonymous namespace)::CaffeImporter::extractLayerParams(google::protobuf::Message const&, cv::dnn::dnn4_v20231225::LayerParams&, bool))
>>> referenced 1082 more times
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [modules\dnn\CMakeFiles\opencv_dnn.dir\build.make:2858: bin/libopencv_dnn490.dll] Error 1
make[1]: *** [CMakeFiles\Makefile2:1870: modules/dnn/CMakeFiles/opencv_dnn.dir/all] Error 2
make: *** [Makefile:165: all] Error 2

Notably, it also says after configuring it is using MinGW:

-- General configuration for OpenCV 4.9.0 =====================================
--   Version control:               4.9.0
--
--   Platform:
--     Timestamp:                   2024-04-08T09:17:02Z
--     Host:                        Windows 10.0.19045 AMD64
--     CMake:                       3.25.2
--     CMake generator:             MinGW Makefiles
--     CMake build tool:            C:/Qt/Tools/mingw1120_64/bin/mingw32-make.exe
--     Configuration:               Release

Solution

  • The final command that worked for me was the following:

    cmake
     -D CMAKE_ANDROID_NDK=D:/.../lib/AndroidSDK/ndk/25.1.8937393
     -D ANDROID_SDK_ROOT=D:/.../lib/AndroidSDK
     -D CMAKE_SYSTEM_NAME=Android
     -B../OpenCV_4.9.0_builds/android_clang_ndk25
     -S.
     -D ANDROID_TOOLCHAIN=clang++
     -D CMAKE_BUILD_TYPE=Release
     -D WITH_CUDA=OFF
     -D WITH_MATLAB=OFF
     -D BUILD_ANDROID_EXAMPLES=OFF
     -D BUILD_DOCS=OFF
     -D BUILD_PERF_TESTS=OFF
     -D BUILD_TESTS=OFF
     -D ANDROID_STL=c++_shared
     -D BUILD_SHARED_LIBS=ON
     -G "Unix Makefiles"
     -DCMAKE_MAKE_PROGRAM=D:/.../lib/AndroidSDK/ndk/25.1.8937393/prebuilt/windows-x86_64/bin/make.exe
     -DCMAKE_SYSTEM_VERSION=33
     -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a
     -DBUILD_JAVA=OFF
     -DBUILD_FAT_JAVA_LIB=OFF
    

    The system version was needed to force version that supports NdkCameraManager.h, I am not sure what is the minimal version that would work.

    The java is turned off because otherwise you get this error on windows when it tries to install:

    [100%] Built target opencv_java
    [100%] Built target opencv_java_android_source_copy
    [100%] Building OpenCV Android library project
    '.' is not recognized as an internal or external command,
    operable program or batch file.
    make[2]: *** [modules/java/android_sdk/CMakeFiles/opencv_java_android.dir/build.make:74: opencv_android/opencv/build/outputs/aar/opencv-release.aar] Error 1
    make[1]: *** [CMakeFiles/Makefile2:2353: modules/java/android_sdk/CMakeFiles/opencv_java_android.dir/all] Error 2
    

    With this setup, I was able to compile and - after a lot of other issues - deploy this as a qt project.