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)
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!
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 ${OpenCV_LIBS} )
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.
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})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.