OpenCV C++ give architecture arm64 error in Macbook M1 chip
Asked Answered
K

4

6

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)
Kea answered 1/7, 2021 at 10:52 Comment(3)
Have you added the link arguments to the compiler command? You'll need something like g++ main.cpp $(pkg-config --libs opencv)Arsenal
It seems no use in this case, the error still appearsKea
In my case, I use also M1 Mac. The OpenCV path was /opt/homebrew/Cellar/opencv/4.5.2_1/include/opencv4/. Therefore, I utilized a command: g++ $(pkg-config --cflags --libs opencv4) -std=c++11 main.cpp -o main.o. However, not using cv::mat, main function has just return 0 so that the compile was successful. By the way, if the main function include cv::mat, it throw me the same compile error. So, I've concluded that it is because M1 Mac Architecture doesn't support OpenCV codes yet. I'm also finding the solution still.Reaganreagen
K
2

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!

Kea answered 2/7, 2021 at 10:48 Comment(0)
V
2

After some exploration I finally solved the problem. To use 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.

Voile answered 3/4, 2022 at 0:45 Comment(1)
Worked for me, with minor update for opencv version. I did -I$(brew --prefix opencv)/include/opencv4 -lopencv_core -lopencv_imgcodecs -lopencv_highgui -L$(brew --prefix opencv)/lib and this will adapt as the version changes (presumably until opencv5 ..!)Luhey
O
0

I have tested OpenCV in macOS successfully, refer to: https://medium.com/@mfkhao2009/set-up-opencv-development-enrioment-875aa69bd403

You should link the library to the target imgproc by adding this code to CMakeLists.txt

add_executable(imgproc main.cpp)
target_link_libraries(imgproc PUBLIC ${OpenCV_LIBS} )

// or in old cmake version: 
add_executable(imgproc main.cpp)
target_link_libraries(imgproc ${OpenCV_LIBS} )
Outcry answered 30/8, 2021 at 2:0 Comment(0)
H
-1

I've been dealing with the same issue. I kept getting the linker error (Undefined symbols for architecture arm64...). FYI I installed via homebrew on my M1 mac, and developing with CLion.

What solved it was adding this to specifiy X86_64 in cmake:

set(CMAKE_OSX_ARCHITECTURES x86_64)

My full CMakeLists.txt:

cmake_minimum_required(VERSION 3.9)
set(CMAKE_OSX_ARCHITECTURES x86_64)
project(opencvtest)

set(CMAKE_CXX_STANDARD 23)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(opencvtest main.cpp)

target_link_libraries(opencvtest ${OpenCV_LIBS})
Hawkeyed answered 22/11, 2022 at 22:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.