Self-compiled libraries almost 10x larger than official build

I try to build the OpenCV 4.5.1 SDK for Android because I need the SDK with contrib modules and the official release [1] only has the standard modules. But the libraries I build are almost 10x larger, for example:
libopencv_core.a ==> 47.6 MB self compiled, 5.3 MB from the official repository (both for arm64-v8a)

Here is my cmake command, followed by ninja for compilation.

cmake \
    -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
    -DANDROID_ABI=$ABI  \
    -DANDROID_NATIVE_API_LEVEL=$ANDROID_PLATFORM \
    -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 \
    -DCMAKE_CXX_FLAGS_RELEASE:STRING=-Os \
    -DCMAKE_C_FLAGS_RELEASE:STRING=-Os \
    -DBUILD_opencv_highgui:BOOL=OFF \
    -DBUILD_opencv_js_bindings_generator:BOOL=OFF \
    -DBUILD_opencv_objc_bindings_generator:BOOL=OFF \
../opencv

ABI is arm64-v8a, ANDROID_PLATFORM is 26, NDK is v21e. Compilation is done in a docker image based on ubuntu groovy.

Output of cmake is

-- General configuration for OpenCV 4.5.1 =====================================
--   Version control:               4.5.1
-- 
--   Extra modules:
--     Location (extra):            /root/opencv_contrib/modules
--     Version control (extra):     4.5.1
-- 
--   Platform:
--     Timestamp:                   2021-03-22T20:16:30Z
--     Host:                        Linux 5.4.0-66-generic x86_64
--     Target:                      Android 1 aarch64
--     CMake:                       3.16.3
--     CMake generator:             Ninja
--     CMake build tool:            /usr/bin/ninja
--     Configuration:               Release
-- 
--   CPU/HW features:
--     Baseline:                    NEON FP16
-- 
--   C/C++:
--     Built as dynamic libs?:      NO
--     C++ standard:                11
--     C++ Compiler:                /opt/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++  (ver 9.0)
--     C++ flags (Release):         -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 -Os  -DNDEBUG
--     C++ flags (Debug):           -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 -O0 -fno-limit-debug-info   -DDEBUG -D_DEBUG
--     C Compiler:                  /opt/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
--     C flags (Release):           -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 -Os  -DNDEBUG
--     C flags (Debug):             -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 -O0 -fno-limit-debug-info   -DDEBUG -D_DEBUG
--     Linker flags (Release):      -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,--as-needed  
--     Linker flags (Debug):        -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,--as-needed  
--     ccache:                      NO
--     Precompiled headers:         NO
--     Extra dependencies:          ade /opt/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/libz.a dl m log
--     3rdparty dependencies:       libcpufeatures ittnotify libprotobuf libjpeg-turbo libwebp libpng libtiff libopenjp2 IlmImf quirc tegra_hal
-- 
--   OpenCV modules:
--     To be built:                 aruco bgsegm bioinspired calib3d core datasets dnn dnn_objdetect dnn_superres dpm face features2d flann fuzzy gapi hfs img_hash imgcodecs imgproc intensity_transform java line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
--     Disabled:                    highgui js_bindings_generator objc_bindings_generator world
--     Disabled by dependency:      ccalib
--     Unavailable:                 alphamat cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv freetype hdf julia matlab ovis python2 python3 sfm ts viz
--     Applications:                -
--     Documentation:               NO
--     Non-free algorithms:         NO
-- 
--   Android NDK:                   /opt/android-ndk-r21e (ver 21.4.7075529)
--     Android ABI:                 arm64-v8a
--     NDK toolchain:               aarch64-linux-android-clang
--     STL type:                    c++_static
--     Native API level:            26
--   Android SDK:                   /opt/android_sdk (tools: 26.1.1 build tools: 30.0.3)
-- 
--   GUI: 
-- 
--   Media I/O: 
--     ZLib:                        /opt/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/libz.a (ver 1.2.7)
--     JPEG:                        build-libjpeg-turbo (ver 2.0.6-62)
--     WEBP:                        build (ver encoder: 0x020f)
--     PNG:                         build (ver 1.6.37)
--     TIFF:                        build (ver 42 - 4.0.10)
--     JPEG 2000:                   build (ver 2.3.1)
--     OpenEXR:                     build (ver 2.3.0)
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--     PFM:                         YES
-- 
--   Video I/O:
-- 
--   Parallel framework:            pthreads
-- 
--   Trace:                         YES (with Intel ITT)
-- 
--   Other third-party libraries:
--     Custom HAL:                  YES (carotene (ver 0.0.1))
--     Protobuf:                    build (3.5.1)
-- 
--   Python (for build):            /usr/bin/python3
-- 
--   Java:                          export all functions
--     ant:                         NO
--     Java wrappers:               YES
--     Java tests:                  NO
-- 
--   Install to:                    /root/build/install
-- -----------------------------------------------------------------

Any ideas why my libraries are so huge?

[1] OpenCV - Browse /4.5.1 at SourceForge.net

Hi,
No idea why libopencv_core.a is so large. In my configuration libopencv_core size is 5 508 394.

My build script is :

CMAKE_OPTIONS='-DBUILD_opencv_world:BOOL=OFF -DBUILD_PERF_TESTS:BOOL=OFF -DBUILD_TESTS:BOOL=OFF -DBUILD_DOCS:BOOL=OFF -DWITH_CUDA:BOOL=OFF -DBUILD_EXAMPLES:BOOL=ON -DENABLE_PRECOMPILED_HEADERS=OFF -DWITH_IPP=OFF -DWITH_MSMF=OFF -DCPU_DISPATCH='
cmake -DCMAKE_MAKE_PROGRAM=${ANDROID_NDK}/prebuilt/windows-x86_64/bin/make.exe  \
-DANDROID_ABI=arm64-v8a \
-DWITH_CAROTENE:BOOL=OFF \
-DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi \
-DJAVA_AWT_INCLUDE_PATH:PATH="C:/Program Files/Java/jdk1.8.0_152/include" \
-DJAVA_AWT_INCLUDE_PATH:PATH="C:/Program Files/Java/jdk1.8.0_152/include" \
-DJAVA_AWT_LIBRARY:FILEPATH="C:/Program Files/Java/jdk1.8.0_152/lib/jawt.lib" \
-DJAVA_INCLUDE_PATH:PATH="C:/Program Files/Java/jdk1.8.0_152/include" \
-DJAVA_INCLUDE_PATH2:PATH="C:/Program Files/Java/jdk1.8.0_152/include/win32" \
-DJAVA_JVM_LIBRARY:FILEPATH="C:/Program Files/Java/jdk1.8.0_152/lib/jvm.lib" \
-DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \
-DANDROID_SDK_ROOT:PATH=/F/Data_SDK_Android \
-DANDROID_NATIVE_API_LEVEL=25 \
-DANDROID_ARM_NEON=ON \
-DBUILD_ANDROID_PROJECTS:BOOL=ON \
-DANT_EXECUTABLE:FILEPATH=f:/apache-ant-1.10.5/bin/ant.bat \
-G"$CMAKE_CONFIG_GENERATOR" \
$CMAKE_OPTIONS -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules -DOPENCV_ENABLE_NONFREE:BOOL=OFF \
-DINSTALL_CREATE_DISTRIB=ON -DCMAKE_INSTALL_PREFIX=/g/lib/install/opencv  ../../"$RepoSource"
1 Like

so you are building static libs, which in fact are huge

Thanks! I found the culprit with the help of your makefile:

-DINSTALL_CREATE_DISTRIB=ON

which the documentation phrases so delicately “Tune multiple things to produce Windows and Android distributions.” reduces the library size from 45 MB to 5 MB.

Without INSTALL_CREATE_DISTRIB=ON:

root@42ce12b54820:~/build# ll -h install/sdk/native/staticlibs/arm64-v8a/libopencv_core.a 
-rw-r--r-- 1 root root 48M Mar 23 09:04 install/sdk/native/staticlibs/arm64-v8a/libopencv_core.a

with INSTALL_CREATE_DISTRIB=ON:

root@42ce12b54820:~/build# ll -h install/sdk/native/staticlibs/arm64-v8a/libopencv_core.a 
-rw-r--r-- 1 root root 5.6M Mar 23 09:12 install/sdk/native/staticlibs/arm64-v8a/libopencv_core.a

:slight_smile:

3 Likes