Linker error when building arm84-v8

I have the same unsolved issue as Fabio in the old forum:
https://answers.opencv.org/question/229811/opencv4android-build-error-with-arm64-v8a/

I built opencv 4.5.1 which works fine for armeabi-v7a but throws a linker error for arm64-v8

[1241/1467] Linking CXX executable bin/opencv_waldboost_detector
FAILED: bin/opencv_waldboost_detector 
: && /opt/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=aarch64-none-linux-android21 --gcc-toolchain=/opt/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/opt/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/sysroot  -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security     -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments  -fvisibility=hidden -fvisibility-inlines-hidden -O2 -DNDEBUG   -DNDEBUG  -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 -Wl,--gc-sections   -Wl,--as-needed modules/xobjdetect/tools/waldboost_detector/CMakeFiles/opencv_waldboost_detector.dir/waldboost_detector.cpp.o  -o bin/opencv_waldboost_detector  lib/arm64-v8a/libopencv_core.a  lib/arm64-v8a/libopencv_imgproc.a  lib/arm64-v8a/libopencv_imgcodecs.a  lib/arm64-v8a/libopencv_videoio.a  lib/arm64-v8a/libopencv_highgui.a  lib/arm64-v8a/libopencv_xobjdetect.a  lib/arm64-v8a/libopencv_videoio.a  -landroid -llog -lmediandk  lib/arm64-v8a/libopencv_imgcodecs.a  3rdparty/lib/arm64-v8a/liblibjpeg-turbo.a  3rdparty/lib/arm64-v8a/liblibwebp.a  3rdparty/lib/arm64-v8a/liblibpng.a  3rdparty/lib/arm64-v8a/liblibtiff.a  3rdparty/lib/arm64-v8a/liblibopenjp2.a  3rdparty/lib/arm64-v8a/libIlmImf.a  lib/arm64-v8a/libopencv_objdetect.a  lib/arm64-v8a/libopencv_calib3d.a  lib/arm64-v8a/libopencv_features2d.a  lib/arm64-v8a/libopencv_imgproc.a  lib/arm64-v8a/libopencv_flann.a  lib/arm64-v8a/libopencv_core.a  3rdparty/lib/arm64-v8a/libzlib.a  3rdparty/lib/arm64-v8a/libcpufeatures.a  3rdparty/lib/arm64-v8a/libittnotify.a  -ldl  -lm  -llog  3rdparty/lib/arm64-v8a/libtegra_hal.a  3rdparty/lib/arm64-v8a/libquirc.a  -latomic -lm && :
lib/arm64-v8a/libopencv_core.a: error adding symbols: Archive has no index; run ranlib to add one
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

system:
ubuntu 20.10
openJDK 13
opencv 4.5.1 w/ contrib
ndk 21e
android sdk 30

Any ideas?

cmake seems to cache too much and thus building two different ABIs without deleting CMakeCache.txt between the runs will cause this problem.

This is my working build command:

rm CMakeCache.txt
cmake \
    -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
    -DANDROID_ABI=arm64-v8a  \
    -DANDROID_NATIVE_API_LEVEL=26 \
    -GNinja \
    -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules \
    -DCMAKE_BUILD_TYPE:STRING=Release \
    -DBUILD_TESTS=FALSE -DBUILD_PERF_TESTS=FALSE \
    -DBUILD_ANDROID_EXAMPLES=FALSE \
    -DCMAKE_CONFIGURATION_TYPES:STRING=Release \
    -DBUILD_opencv_highgui:BOOL=OFF \
    -DBUILD_opencv_js_bindings_generator:BOOL=OFF \
    -DBUILD_opencv_objc_bindings_generator:BOOL=OFF \
    -DINSTALL_CREATE_DISTRIB=ON \
    -DBUILD_DOCS:BOOL=OFF -DBUILD_EXAMPLES:BOOL=ON -DWITH_IPP=OFF -DWITH_MSMF=OFF \
../opencv

ninja
1 Like