OpenCV C++ give architecture arm64 error in Macbook M1 chip

Viewed 1945

I built OpenCV-4.5.2 in Macbook M1 followed this tutorial: https://sayak.dev/install-opencv-m1. It works fine in Python but when I use in C++

#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>

int main()
{
    cv::Mat img = cv::imread("avatar.jpeg");
    return 0;
}

It give an error in cv::Mat

Undefined symbols for architecture arm64:
  "cv::Mat::~Mat()", referenced from:
      _main in main.cpp.o
  "cv::imread(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [imgproc] Error 1
make[2]: *** [CMakeFiles/imgproc.dir/all] Error 2
make[1]: *** [CMakeFiles/imgproc.dir/rule] Error 2
make: *** [imgproc] Error 2

After hours, I can't find what's wrong with it. Can anybody help me? Thank you!

P/S: as additional, this is my CMakeLists.txt

cmake_minimum_required(VERSION 3.19)
project(imgproc)

set(CMAKE_CXX_STANDARD 14)

# Set the location of the OpenCV directory
set(OpenCV_DIR "/usr/local/include/opencv4")
# Find OpenCV library
find_package( OpenCV 4 REQUIRED )
# Add header file
include_directories(include ${OpenCV_INCLUDE_DIRS} )


add_executable(imgproc main.cpp)
3 Answers

I found that replace these include:

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>

with:

#include <opencv2/opencv.hpp>

Then everything worked!

After some exploration I finally solved the problem. To user the openCV library on M1 Mac, you need to include -I/opt/homebrew/Cellar/opencv/4.5.5/include/opencv4/ -lopencv_core -lopencv_imgcodecs -lopencv_highgui -L/opt/homebrew/Cellar/opencv/4.5.5/lib/ as your g++ compile options.

Related