OpenCV C++ 在 Macbook M1 芯片中给出架构 arm64 错误

时间:2021-07-01 10:52:11

标签: c++ macos opencv arm64 apple-m1

我按照本教程在 Macbook M1 中构建了 OpenCV-4.5.2: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)

1 个答案:

答案 0 :(得分:0)

我发现替换这些包括:

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

与:

#include <opencv2/opencv.hpp>

然后一切正常!