[英]OpenCV C++ give architecture arm64 error in Macbook M1 chip
I built OpenCV-4.5.2 in Macbook M1 followed this tutorial: https://sayak.dev/install-opencv-m1 .我按照本教程在 Macbook M1 中构建了 OpenCV-4.5.2: https://sayak.dev/install-opencv-m1 。 It works fine in Python but when I use in C++
它在 Python 中工作正常但是当我在 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它在 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 P/S: 另外,这是我的 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我已经在macOS中成功测试过OpenCV,参考: 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
您应该通过将此代码添加到
CMakeLists.txt
将库链接到目标 imgproc
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.要在 M1 Mac 上使用 openCV 库,您需要包含
-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/
作为您的 g++ 编译选项。
I've been dealing with the same issue.我一直在处理同样的问题。 I kept getting the linker error (Undefined symbols for architecture arm64...).
我不断收到 linker 错误(架构 arm64 的未定义符号...)。 FYI I installed via homebrew on my M1 mac, and developing with CLion.
仅供参考,我通过自制软件安装在我的 M1 mac 上,并使用 CLion 进行开发。
What solved it was adding this to specifiy X86_64 in cmake:解决它的方法是将其添加到 cmake 中的指定 X86_64:
set(CMAKE_OSX_ARCHITECTURES x86_64)
My full CMakeLists.txt
:我的完整
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})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.