我建立了OpenCV-4.5.2在Macbook M1遵循这个教程:https://sayak.dev/install-opencv-m1。它在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;
}
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
几个小时后,我找不到它有什么问题。谁能帮帮我?谢谢!
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)
4条答案
按热度按时间kq4fsx7k1#
我发现替换这些包括:
与:
然后一切都正常了!
14ifxucb2#
经过一番探索,我终于解决了这个问题。要在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++编译选项。f3temu5u3#
我已经在macOS中成功测试了OpenCV,请参阅:https://medium.com/@mfkhao2009/set-up-opencv-development-enrioment-875aa69bd403
您应该通过将以下代码添加到
CMakeLists.txt
来将库链接到目标imgprocgab6jxml4#
我一直在处理同样的问题。我一直得到链接器错误(架构arm64的未定义符号...)。FYI我通过自制安装在我的M1 mac上,并与CLion开发。
解决此问题的方法是在cmake中添加以下内容以指定X86_64:
我的完整
CMakeLists.txt
: