Issues on OpenCV4 compiling from scratch on newer macOS (Intel chip, Monterey)

System information (version)
  • OpenCV => 4.5.5
  • Operating System / Platform => macOS Monterey 12.0.1, 64 Bit
  • Compiler => gcc-11, local
Detailed description

This is an issue I came across when I tried configuring OpenCV for C++ development after using it with Python for few weeks. I tried to install the “cutting-edge” version from GitHub directly and not use the Homebrew binaries. After cloning the repo, I created a build folder inside, and used CMake to configure build files, when this was finished, I used the ‘make all’ command to compile. This is when the issue happens.

Steps to reproduce

I followed the instructions in the book “Building Computer Vision Projects with OpenCV4 and C++” and I’ll describe here the steps that led to an error on my machine:

  • Install CMake
  • Download latest version of OpenCV from GitHub, unzip anywhere (I git cloned it in /usr/local/Cellar)
  • Go to OpenCV director (cd /full/path/to/opencv)
  • make new directory called ‘build’ && cd into ‘build’
  • Now we call CMake with this command: cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/full/path/to/opencv/build -D INSTALL_C_EXAMPLES=ON -D BUILD_EXAMPLES=ON ../
  • I tried both CMake via command line and using the actual CMake application, both led to the same result.
  • Now to install OpenCV, inside the build folder, I call: make -j4
  • The j4 flag indicates that we should use four cores to install (though I’ve tried calling make by itself and it still led to the same error, so this can’t be it.
  • This is where it starts to build targets, here is the error output after running make second time (because it’s cleaner than first, less to copy/paste).
sharbel@sharbel-MacBook-Pro build % make all
[  0%] Built target opencv_highgui_plugins
Consolidate compiler generated dependencies of target libopenjp2
[  1%] Built target libopenjp2
[  1%] Built target opencv_videoio_plugins
Consolidate compiler generated dependencies of target ittnotify
[  1%] Built target ittnotify
Consolidate compiler generated dependencies of target zlib
[  2%] Built target zlib
Consolidate compiler generated dependencies of target ippiw
[  3%] Built target ippiw
Consolidate compiler generated dependencies of target opencv_core
[  8%] Built target opencv_core
Consolidate compiler generated dependencies of target opencv_imgproc
[ 12%] Built target opencv_imgproc
Consolidate compiler generated dependencies of target libjpeg-turbo
[ 15%] Built target libjpeg-turbo
Consolidate compiler generated dependencies of target libpng
[ 16%] Built target libpng
Consolidate compiler generated dependencies of target libtiff
[ 18%] Built target libtiff
Consolidate compiler generated dependencies of target libwebp
[ 24%] Built target libwebp
Consolidate compiler generated dependencies of target opencv_imgcodecs
make[2]: *** No rule to make target `zlib', needed by `lib/libopencv_imgcodecs.4.5.5.dylib'.  Stop.
make[1]: *** [modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/all] Error 2
make: *** [all] Error 2

I’ve searched through all tutorials, they each have a flaw and most seem to be outdated or not specific. OpenCV4 works just fine when I use Xcode, because apparently Xcode is able to find the dynamic libraries it’s looking for because Apple changed the location of the libraries on newer OS’s and properly updated Xcode to handle this, but CMakeLists isn’t equipped with that ability, therefore builds are failing. (just a hypothesis, collab with scawful).

note: I’m aware I can use Homebrew alternatively, but since I’m on macOS Monterey, it’s not supported by that build yet. I’m seriously considering downgrading to Big Sur if I can’t resolve this issue.

Solved.
Some things I tried, not sure which one caused the fix:

  • downgraded to macOS Big Sur, so mac became fresh, no dependency clutter, no Xcode command line tools.
  • I uninstalled homebrew (it had it’s own zlib, this was likely the cause of the issue).