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

5n0oy7gb  于 2022-11-24  发布在  Mac
关注(0)|答案(4)|浏览(383)

我建立了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)
kq4fsx7k

kq4fsx7k1#

我发现替换这些包括:

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

与:

#include <opencv2/opencv.hpp>

然后一切都正常了!

14ifxucb

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++编译选项。

f3temu5u

f3temu5u3#

我已经在macOS中成功测试了OpenCV,请参阅:https://medium.com/@mfkhao2009/set-up-opencv-development-enrioment-875aa69bd403
您应该通过将以下代码添加到CMakeLists.txt来将库链接到目标imgproc

add_executable(imgproc main.cpp)
target_link_libraries(imgproc ${OpenCV_LIBS} )
gab6jxml

gab6jxml4#

我一直在处理同样的问题。我一直得到链接器错误(架构arm64的未定义符号...)。FYI我通过自制安装在我的M1 mac上,并与CLion开发。
解决此问题的方法是在cmake中添加以下内容以指定X86_64:

set(CMAKE_OSX_ARCHITECTURES x86_64)

我的完整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})

相关问题