MacBook M2 uses Xcode to configure the OpenCV-C++ environment

To install OpenCV implemented in C++ on MacBook, you can choose between Homebrew and source code installation. Here I chose source code installation. Choose source code installation. You must use other methods to install cmake, because I still Use anaconda to create an independent environment and configure the Python-OpenCV environment, so I used conda install cmake to install cmake. You can also choose to install Homebrew to install cmake using brew install cmake.
Let’s start configuring the OpenCV-C++ environment together:

1. Install OpenCV

1. Obtain OpenCV source code

In your working directory (choose your own, I cloned it in the “Download” folder) use git clone to obtain the source code:
If it shows that cloning failed, this is normal. Repeat the operation, or repeat the operation after changing the network. Mobile hotspot is also a good choice.
You can also download directly from my Baidu network disk sharing link:
Link: https://pan.baidu.com/s/1Z9_Ap7Jm-rpBxVKV_1dEOw?pwd=emv4 Extraction code: emv4

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


The first clone above failed halfway, it doesn’t matter, try it again:


After you successfully clone the project source code, you can install it according to the instructions on the OpenCV official website (https://docs.opencv.org/4.x/d0/db2/tutorial_macos_install.html). In fact, I installed it according to the operation on the official website, and the installation steps I wrote below are also the installation process according to the operation on the official website.

2. Create a new compilation folder

After the code is cloned, an opencv folder will appear under the path you selected. The source code is in this folder. It is compiled in a folder at the same level as the opencv folder. What does it mean? For example, if the code path I cloned is ./Downloads/opencv, then I will create a compilation folder under the Downloads folder. This is to keep the source code clean, which is very important!
First use the cd command to enter the Downloads folder, then use mkdir to create a compilation folder and use cd to enter this folder, as shown in the screenshot below:

mkdir build
cd build


At this time, enter the Downloads folder and you will see the following two folders:

3. Use cmake to build opencv from source

The build type -DCMAKE_BUILD_TYPE can also be set to the Release model, and whether to build the routine -DBUILD_EXAMPLES can be set to ON, If you want to set more parameters, you can refer to the official website link above.

cmake -DCMAKE_BUILD_TYPE=Debug -DBUILD_EXAMPLES=OFF ../opencv

cmake is successful as shown in the screenshot below:

Special reminder: “Install to:” in the screenshot indicates the installation path.

4. Use make to generate opencv

make -j7

The -j7 here refers to 7 threads running.
make is successful as shown in the screenshot below:

5. Install opencv

sudo make install

Start the installation after entering the power-on password. If no error is reported, opencv is successfully installed in the /usr/local/ folder.


If you see the following four folders in the /usr/local/ folder, the installation is successful. The first three folders are all opencv related files, and the fourth share folder is the original folder of the system.

2. Build OpenCV project in Xcode

1. Create a project:

2. Click Next, then name the project, select C++ as the language, and customize the organizer identity:

3. Click Next again and select the project storage location. I chose to store it in the CPP folder:

4. Click Create:

5. The project creation is completed, and the next step is the core work: configuring the OpenCV environment

According to the prompts in the screenshot below, enter “/usr/local/include/” in the “Debug” mode of “Header Search Paths” and “/usr/local/include/opencv4/“.

Similarly, enter “/usr/" in "Debug" mode of "Library Search Paths" under "Header Search Paths" local/lib/” screenshot is as follows:
Then, under “Linking - General“, click “Runpath" Enter "/usr/local/lib/" in Search Paths“, the screenshot is as follows:

It’s not over yet, there is one more step: right-click on the project name and select “Add Files to "Opencv_test"...


After clicking, the following window appears:

Then, press the “/” key on the keyboard and enter “/usr/local/lib/”:

Then press Enter to go to the following page:
Select the lib folder and click Add


Then a lib folder will appear in the project directory, which will contain the linked library:

6. Test project

Write opencv related code in your main.cpp file and test whether the environment is built successfully. You can choose the code below, butbe sure to change the path to the image file!

#include<iostream>
#include<opencv2/opencv.hpp>

int main()
{<!-- -->
    std::string image_path = "/Users/freespirit/Pictures/Nayeon.png";
    cv::Mat gray_img = cv::imread(image_path, 1);
    if (gray_img.empty())
    {<!-- -->
        std::cout << "no file" << std::endl;
        return 0;
    }
    cv::imshow("image", gray_img);

    cv::waitKey(0);
    return 0;
}

The screenshot of the successful test is as follows:

7. Eliminate warnings

You noticed that although the test program ran successfully, 40 warnings appeared in the warning bar on the left (Empty paragraph passed to '@param' command)! This is caused by the @ comments in these source codes being inappropriate in Xcode or MacOS (because there is no such problem in Visual Studio C++ under Windows), as shown in the screenshot below Input “ -Wno-documentation“.


After doing this, not all warnings are eliminated, there are still seven warnings (Arithmetic between different enumeration types ('...' and '...') is deprecated ), this is a warning caused by Xcode deprecating arithmetic operations between different enumeration types and such operations still exist in the OpenCV source code. The screenshot of the warning is as follows:


The solution is the same as the previous one. Follow the prompts in the screenshot below to go to “Build Settings“-“Apple Clang - Custom Compiler Flags“-“Other Warning Flags ” enter “-Wno-deprecated“.


Running the test program again, no warnings:

If it is useful to you, please click three times >_<