OpenCV中有很多模块,模块间保持最小的依赖关系,用户可以根据自己的实际需要链接相关的库,而不需链接所有的库,这样在最终交付应用程序时可以减少总库的大小。但如果需要依赖OpenCV的库太多,有时会带来不方便,此时可以使用OpenCV的world模块。
OpenCV中的world模块,也称为超级模块(super-module),它结合了用户选择的所有其它模块。它是一个一体化(all-in-one)模块,具有所有库的功能。使用world模块可能会使应用程序的编译时间略有增加。
目前OpenCV最新发布版本为4.8.1,编译不带world模块的动态库,在windows下会有58个动态库。在windows上使用vs2022及在linux上使用g++ 9的脚本build.sh如下所示:
#! /bin/bashif [ $# != 2 ]; thenecho "Error: requires two parameters: 1: windows or linux; 2: relese or debug"echo "For example: $0 windows debug"exit -1
fiif [ $1 != "windows" ] && [ $1 != "linux" ]; thenecho "Error: the first parameter can only be windows or linux"exit -1
fiif [ $2 != "release" ] && [ $2 != "debug" ]; thenecho "Error: the second parameter can only be release or debug"exit -1
fiif [[ ! -d "build" ]]; thenmkdir buildcd build
elsecd build
fiif [ $1 == "windows" ] && [ $2 == "release" ]; thencmake \-G"Visual Studio 17 2022" -A x64 \-DCMAKE_BUILD_TYPE=Release \-DCMAKE_CONFIGURATION_TYPES=Release \-DBUILD_SHARED_LIBS=ON \-DBUILD_opencv_world=ON \-DBUILD_PERF_TESTS=OFF \-DBUILD_TESTS=OFF \-DCMAKE_INSTALL_PREFIX=../install_4.8.1/ \-DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \..cmake --build . --target install --config release
fiif [ $1 == "windows" ] && [ $2 == "debug" ]; thencmake \-G"Visual Studio 17 2022" -A x64 \-DCMAKE_BUILD_TYPE=Debug \-DCMAKE_CONFIGURATION_TYPES=Debug \-DBUILD_SHARED_LIBS=ON \-DBUILD_opencv_world=ON \-DBUILD_PERF_TESTS=OFF \-DBUILD_TESTS=OFF \-DCMAKE_INSTALL_PREFIX=../install_4.8.1/ \-DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \..cmake --build . --target install --config debug
fiif [ $1 == "linux" ] && [ $2 == "release" ]; thencmake \-DCMAKE_C_COMPILER=/usr/bin/gcc-9 \-DCMAKE_CXX_COMPILER=/usr/bin/g++-9 \-DCMAKE_BUILD_TYPE=Release \-DBUILD_SHARED_LIBS=ON \-DBUILD_opencv_world=ON \-DBUILD_PERF_TESTS=OFF \-DBUILD_TESTS=OFF \-DCMAKE_INSTALL_PREFIX=../install_4.8.1/release/ \-DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \..make -j4make install
fiif [ $1 == "linux" ] && [ $2 == "debug" ]; thencmake \-DCMAKE_C_COMPILER=/usr/bin/gcc-9 \-DCMAKE_CXX_COMPILER=/usr/bin/g++-9 \-DCMAKE_BUILD_TYPE=Debug \-DCMAKE_CONFIGURATION_TYPES=Debug \-DBUILD_SHARED_LIBS=ON \-DBUILD_opencv_world=ON \-DBUILD_PERF_TESTS=OFF \-DBUILD_TESTS=OFF \-DCMAKE_INSTALL_PREFIX=../install_4.8.1/debug/ \-DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \..make -j4make install
firc=$?
if [[ ${rc} != 0 ]]; thenecho "Error: please check: ${rc}"exit ${rc}
fi
在windows上编译结果如下所示:
在Linux上编译结果如下所示:
GitHub:https://github.com/fengbingchun/OpenCV_Test