Skip to content

Instantly share code, notes, and snippets.

@UplandsDynamic
Last active February 21, 2022 16:29
Show Gist options
  • Save UplandsDynamic/a8968d446c7267f65b45af3fdc349d65 to your computer and use it in GitHub Desktop.
Save UplandsDynamic/a8968d446c7267f65b45af3fdc349d65 to your computer and use it in GitHub Desktop.
Install OpenCV for C++ on MacBook Pro M1 (2020)
Here are the steps required to install OpenCV for C++ on the MacBook Pro M1.
1. If not already installed, install Homebrew, by following instructions here: https://brew.sh/.
2. If Xcode is not already installed, install it from the App Store (or other methods).
3. Open a terminal and run:
brew install opencv
4. In the terminal, run:
brew install pkg-config
5. Create a new Xcode project for MacOS, selecting 'Command Line Tool'.
6. Navigate to the new project's 'Build Settings' panel (click on the project's root folder, then select the tab).
7. Scroll to the 'Search Paths' section and add the following entry to the
'Header Search Paths' (double click to open the field's input panel):
/opt/homebrew/Cellar/opencv/4.5.4_4/include
8. Select 'recursive' from the drop down menu.
9. Go to the 'Library Search Paths' section (immediately below Header Search Paths) and add the following entry:
/opt/homebrew/Cellar/opencv/4.5.4_4/lib
10. Again, select 'recursive' from the drop down menu.
11. In the terminal, run the following:
pkg-config --cflags --libs opencv4
12. Copy the resulting output of the above command to the clipboard.
13. Back in the 'Build Settings' panel, scroll to the 'Linking' section, then to its 'Other Linker Flags' subsection.
Double click the input field to enter text (as before), then paste the contents of the clipboard into the field.
14. Select the 'Signing & Capabilities' tab (this may be found 2 tabs to the left of 'Build Settings').
15. Check the box marked 'Disable Library Validation'.
16. To include OpenCV in project files, just include the header, e.g.:
#include <opencv2/opencv.hpp>
17. Write your .cpp file in the usual way. Remember to use the cv namespace, for convenience, e.g.:
using namespace cv;
18. To build & run your code in one step, click the button with the 'Play' icon (above the left panel).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment