Android的OpenCV自编译库比官方构建的库大10倍



我尝试为Android构建OpenCV 4.5.1 SDK,因为我需要带有contrib模块的SDK,而官方发布的[1]只有标准模块。但我构建的库几乎大了10倍,例如:libopencv_core.a==>47.6 MB自行编译,5.3 MB来自官方存储库(均用于arm64-v8a(

这是我的cmake命令,后面是用于编译的ninja。

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是arm64-v8a,ANDROID_PLATFORM是26,NDK是v21e。编译是在一个基于ubuntu groovy的docker镜像中完成的。

cmake的输出为

-- 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
-- -----------------------------------------------------------------

你知道为什么我的图书馆这么大吗?

[1]https://sourceforge.net/projects/opencvlibrary/files/4.5.1/

我发现了罪魁祸首:

-DINSTALL_CREATE_DISTRIB=ON

文献中如此巧妙地表述";调整多种内容以生成Windows和Android发行版"将库大小从45MB减少到5MB。

不带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

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

:轻微_微笑:

最新更新