简体   繁体   English

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

[英]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.

相关问题 C++ 找不到 arm64 的 Boost 库符号 | M1 笔记本电脑 - C++ Boost Library Symbols not found for arm64 | M1 Macbook 架构 arm64 的未定义符号:m1 mac - Undefined symbols for architecture arm64: m1 mac 在 Mac arm64 架构 (M1) 上使用 ffmpeg 的 mpd 编译错误不清楚 - Unclear compile error for mpd with ffmpeg on Mac arm64 architecture (M1) 在 M1 Mac 上从 C++ 链接到 curl - arm64 的未定义符号 - Linking to curl from C++ on M1 Mac - undefined symbols for arm64 如何解决 M1 Mac 中的“未找到架构 arm64 的符号” - How to solve the "symbol(s) not found for architecture arm64" in M1 Mac “架构 arm64 的未定义符号”使用 g++-12 在 M1 mac 上构建基本 SFML 项目 - "Undefined symbols for architecture arm64" building basic SFML project on M1 mac with g++-12 无法在带有 M1 芯片的 Macbook Air 上编译带有 Boost 标头的 C++ - Can't compile C++ with Boost headers on a Macbook Air with M1 chip C++ 错误:ld:未找到体系结构 arm64 的符号 clang:错误:链接器命令失败,退出代码为 1 - C++ Error: ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 在 MacOS 上编译 c++ 和汇编代码时架构 arm64 的未定义符号 - Undefined symbols for architecture arm64 when compiling c++ and assembly code on MacOS 获取架构 arm64 错误的未定义符号 - Getting undefined symbols for architecture arm64 error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM