macOS Static OpenCV Build
I recently figured out how to build OpenCV statically on macOS. As someone with zero C/C++ background I ran into plenty of bumps along the way, but the full flow finally makes sense now.
Below is the process I followed. The resulting static OpenCV build is tailored for Auto Engine Core (used mainly for image matching).
1. Install libraries required by OpenCV
Because the OpenCV build needs image processing support, we must install the following libraries via Homebrew and use their lib*.a outputs:
brew install libjpeg libpng libtiff zlib openblas2. Download OpenCV
First set up the environment variables used by the build:
export OPENCV_VERSION=4.11.0
export DYLD_FALLBACK_LIBRARY_PATH=/Library/Developer/CommandLineTools/usr/lib/Download and extract OpenCV plus the contrib modules:
wget -O opencv.zip "https://github.com/opencv/opencv/archive/refs/tags/${OPENCV_VERSION}.zip"
wget -O opencv_contrib.zip "https://github.com/opencv/opencv_contrib/archive/refs/tags/${OPENCV_VERSION}.zip"
unzip opencv.zip && rm opencv.zip
unzip opencv_contrib.zip && rm opencv_contrib.zip3. Build a static OpenCV
Set the static libraries that were just installed through Homebrew (libjpeg, libpng, libtiff, zlib, openblas):
mkdir -p build && cd build && \
cmake -DCMAKE_BUILD_TYPE=Release \
-DBUILD_SHARED_LIBS=OFF \
-DWITH_OPENBLAS=ON \
-DBLAS_LIBRARIES=/opt/homebrew/opt/openblas/lib/libopenblas.a \
-DLAPACK_LIBRARIES=/opt/homebrew/opt/openblas/lib/libopenblas.a \
-DWITH_ACCELERATE=OFF \
-DWITH_TEGRA=OFF \
-DCMAKE_OSX_ARCHITECTURES=arm64 \
-DCMAKE_INSTALL_PREFIX=/usr/local \
-DBUILD_DOCS=OFF \
-DBUILD_EXAMPLES=OFF \
-DBUILD_TESTS=OFF \
-DBUILD_PERF_TESTS=OFF \
-DWITH_PNG=ON \
-DPNG_INCLUDE_DIR=/usr/local/include \
-DPNG_LIBRARY=/usr/local/lib/libpng16.a \
-DWITH_JPEG=ON \
-DJPEG_INCLUDE_DIR=/usr/local/include \
-DJPEG_LIBRARY=/usr/local/lib/libjpeg.a \
-DWITH_TIFF=ON \
-DTIFF_INCLUDE_DIR=/usr/local/include \
-DTIFF_LIBRARY=/usr/local/lib/libtiff.a \
-DWITH_WEBP=OFF \
-DWITH_OPENJPEG=OFF \
-DWITH_JASPER=OFF \
-DWITH_OPENEXR=OFF \
-DWITH_V4L=OFF \
-DWITH_FFMPEG=OFF \
-DWITH_IPP=OFF \
-DWITH_OPENCL=OFF \
-DWITH_CAROTENE=OFF \
-DBUILD_opencv_java=OFF \
-DBUILD_opencv_python=OFF \
-DOPENCV_EXTRA_MODULES_PATH="opencv/opencv_contrib-${OPENCV_VERSION}/modules" \
"opencv/opencv-${OPENCV_VERSION}"
cd opencv/build && sudo cmake --build . --target install --config Release --parallel 8
cd opencv/build && sudo cmake --install . --prefix /usr/local4. Build your own application
clang_dir="$(clang --print-search-dirs | awk -F= '/^libraries: =/ { print $2 }')"
export OPENCV_LINK_LIBS=opencv_core,opencv_imgproc,opencv_imgcodecs,libittnotify,libjpeg,libpng,libtiff,zlib
export OPENCV_LINK_PATHS=/usr/local/lib,/usr/local/lib/opencv4/3rdparty,$clang_dir/lib/darwin
export OPENCV_INCLUDE_PATHS=/usr/local/include,/usr/local/include/opencv4
cargo build --release5. Missing dependencies?
Most of the time you should now be able to build your application successfully. If the linker still complains about libpng, libjpeg, and similar libraries, inspect the files inside /usr/local/lib/opencv4/3rdparty.
Verify that the files are named lib*.a rather than liblib*.a. If you see the latter, rename them manually.
ls -l /usr/local/lib/opencv4/3rdparty
total 15176
-rw-r--r--@ 1 root wheel 347880 Nov 14 00:54 libade.a
-rw-r--r--@ 1 root wheel 87512 Nov 14 00:54 libittnotify.a
-rw-r--r--@ 1 root wheel 777952 Nov 14 00:54 libjpeg.a
-rw-r--r--@ 1 root wheel 2624272 Nov 14 00:54 liblibprotobuf.a
-rw-r--r--@ 1 root wheel 737872 Nov 13 23:12 liblibwebp.a
-rw-r--r--@ 1 root wheel 88096 Nov 14 00:54 libopencv.sfm.correspondence.a
-rw-r--r--@ 1 root wheel 1154496 Nov 14 00:54 libopencv.sfm.multiview.a
-rw-r--r--@ 1 root wheel 7536 Nov 14 00:54 libopencv.sfm.numeric.a
-rw-r--r--@ 1 root wheel 890464 Nov 14 00:54 libopencv.sfm.simple_pipeline.a
-rw-r--r--@ 1 root wheel 311976 Nov 14 00:54 libpng.a
-rw-r--r--@ 1 root wheel 614432 Nov 14 00:54 libtiff.a
-rw-r--r--@ 1 root wheel 104800 Nov 14 00:54 libzlib.a