Skip to content

Instantly share code, notes, and snippets.

@nucliweb
Last active May 31, 2024 10:22
Show Gist options
  • Save nucliweb/b2a234c673221af5ec24508da7d8b854 to your computer and use it in GitHub Desktop.
Save nucliweb/b2a234c673221af5ec24508da7d8b854 to your computer and use it in GitHub Desktop.
OpenCV C++ Mac M1 Installation Steps

📺 OpenCV C++ Mac M1 Installation Tutorial Video

Commands you need

We need homebrew installed in our system

brew install cmake

mkdir Open_CV && cd Open_CV

git clone https://github.com/opencv/opencv.git

mkdir build && cd build

cmake ../opencv/ .

arch -arm64 cmake ../opencv/ -DWITH_QT=OFF -DWITH_OPENGL=OFF -DFORCE_VTK=OFF -DWITH_TBB=OFF -DWITH_GDAL=OFF -DWITH_XINE=OFF -DBUILD_EXAMPLES=OFF -DBUILD_ZLIB=OFF -DBUILD_TESTS=OFF .

arch -arm64 sudo make -j 4

arch -arm64 sudo make install
@DigitalGabriele
Copy link

I think you meant:

mkdir build && cd build

@nucliweb
Copy link
Author

I think you meant:

mkdir build && cd build

Fixed!

Thanks @DigitalGabriele 😊

@east10
Copy link

east10 commented Oct 26, 2022

When I run 'arch -arm64 cmake ....' I get the error:

arch: posix_spawnp: cmake: Bad CPU type in executable

How do I install a version of make that has the arm64 architecture?

file which cmake show:

/usr/local/bin/cmake: Mach-O 64-bit executable x86_64

Where as file which make shows:

/usr/bin/make (for architecture x86_64): Mach-O 64-bit executable x86_64
/usr/bin/make (for architecture arm64e): Mach-O 64-bit executable arm64e

@stephenswetonic
Copy link

@east10 I'm getting the same problem. It appears to detect x86_64 in the terminal regardless..

@azhangvo
Copy link

azhangvo commented Jan 6, 2023

I was dealing with this issue last night and I found that I only had cmake installed for x86_64, though which cmake did not specify that. Instead, it was that my Homebrew version was installed on the x86_64 path rather than the arm64 path.

One of Homebrew's pages specifies that Intel files are installed under /usr/local, while Apple Silicon files are installed under /opt/homebrew, and is chosen automatically on install.

Since I had previously checked the box "Run with rosetta" in my terminal settings, I believe Homebrew installed as if it were an Intel machine rather than an Apple Silicon machine. I reran the Homebrew install with the box unchecked, added Homebrew to my path, and reinstalled cmake, which then allowed me to run the arch command without the above error.

@suryamanideep
Copy link

suryamanideep commented Feb 15, 2023

how do we include files. for xcode if this way of installation is followed?

@tahashieenavaz
Copy link

tahashieenavaz commented Mar 2, 2023

How about adding the below flag which enables you to use pkg-config .

-DOPENCV_GENERATE_PKGCONFIG=ON

pkg-config --cflags --libs opencv4

@Victor3699
Copy link

excuse me, but when I use the second line, he return me this : "zsh: command not found: pkg-config"

@tahashieenavaz
Copy link

You can simply run. brew install pkg-config

@henrydrp10
Copy link

Hello, thank you for this. I tried to run an OpenCV example code but I get this:

opencv_test.cpp:2:10: fatal error: 'opencv2/imgproc.hpp' file not found #include "opencv2/imgproc.hpp"

How can I do to include the different header files in the compilation?

Thank you.

@nucliweb
Copy link
Author

Hi @henrydrp10

Can you share the system and hardware that you are using?

@henrydrp10
Copy link

Hi @nucliweb

Sorry for bothering. I went into the video tutorial and I managed to understand how compilation is done (using CMakeLists and makefiles). I had just compiled using the g++ command and therefore the libraries had not been included properly.

Thank you.

@nucliweb
Copy link
Author

Great! 🚀

@mcserni
Copy link

mcserni commented Dec 8, 2023

Great! thank you!

@khachaturyan-1991
Copy link

@henrydrp10 may you please share a link to the video tutorial? I faced the same issue

@kowyo
Copy link

kowyo commented May 27, 2024

I ran the command: arch -arm64 sudo make -j$(nproc) and got the following output:

/Users/xxx/OpenCV/build_opencv/opencv_lapack.h:25:28: note: expanded from macro 'OCV_LAPACK_FUNC'
#define OCV_LAPACK_FUNC(f) f##_
                           ^~~~
<scratch space>:4:1: note: expanded from here
dgeev_
^~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX14.4.sdk/System/Library/Frameworks/vecLib.framework/Headers/lapack.h:23405:1: note: candidate function not viable: no known conversion from 'int *' to 'const __LAPACK_int * _Nonnull' (aka 'const long *') for 3rd argument
dgeev_(
^
1 error generated.
make[2]: *** [modules/calib3d/CMakeFiles/opencv_calib3d.dir/src/usac/dls_solver.cpp.o] Error 1
make[1]: *** [modules/calib3d/CMakeFiles/opencv_calib3d.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
[ 70%] Linking CXX shared library ../../lib/libopencv_highgui.dylib
[ 70%] Built target opencv_highgui
make: *** [all] Error 2

Anyone has idea about how to fix this?

@thewoz
Copy link

thewoz commented May 31, 2024

@kowyo
Copy link

kowyo commented May 31, 2024

you can look here:

opencv/opencv#25625 opencv/opencv#25675

Thank you for the reply! Yes I am using the latest macOS (Sonoma 14.5 arm64) with a clean install from 2 weeks ago. So as for as I know from partial discussion in opencv/opencv#25625 and opencv/opencv#25625, an update is needed for the opencv repo, and it's not an issue of my environment, isn't it? Anyway, thank you for remaindering me here. I could offer any help if action is needed.

p.s. I used brew install opencv to solve this days ago

@thewoz
Copy link

thewoz commented May 31, 2024

yes a pull was made a few days ago that fixes your error.
I don't know how brew works and how often they update the code.
What you should do is download and install OpenCV by hand.
To do that it seems to me that the script here (and the small changes suggested) should be fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment