为macOS-x86_64构建,但尝试链接为macOS-arm64构建的文件 [英] building for macOS-x86_64 but attempting to link with file built for macOS-arm64
问题描述
我使用c ++和OpenCV编写了代码:
I wrote a code using c++ and OpenCV:
#include <iostream>
#include <time.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
using namespace std;
int main()
{
...
return 0;
}
然后我尝试在终端上运行我的代码并使用g ++进行构建:
then I trying to Running my code on the terminal and build using g++:
g++ $(pkg-config --cflags --libs opencv) -std=c++11 yourFile.cpp -o yourFileProgram
但我收到此错误:
...
ld: warning: ignoring file /opt/homebrew/Cellar/opencv/4.5.1_2/lib/libopencv_core.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /opt/homebrew/Cellar/opencv/4.5.1_2/lib/libopencv_photo.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
Undefined symbols for architecture x86_64:
"cv::Mat::Mat()", referenced from:
_main in cv_test-ff1014.o
"cv::Mat::~Mat()", referenced from:
_main in cv_test-ff1014.o
"cv::Mat::operator=(cv::Mat&&)", referenced from:
_main in cv_test-ff1014.o
"cv::imread(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
_main in cv_test-ff1014.o
"cv::imwrite(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, cv::_InputArray const&, std::__1::vector<int, std::__1::allocator<int> > const&)", referenced from:
_main in cv_test-ff1014.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
推荐答案
我遇到了同样的问题,简而言之,就是苹果改用了 libc ++
而不是 libstdc ++
.如果您要按照提及的方式进行编译(通过控制台g ++),则的答案是这里.
I had the same problem, and the short answer is that Apple switched to using libc++
instead of libstdc++
. If you want to compile it as you mention (via console g++) then the answer is here.
Also, you have mention g++
in tag, but be sure that you understand that gcc is alias of clang, and for using g++
compiler you have to print g++-10
.
在示例中提供的通过终端编译 OpenCV
和 g ++
的方法是
Way to compile OpenCV
whith g++
via terminal as you provide in the example is described here
在macOS上包含 OpenCV
lib的最佳方法是通过自制的opencv lib安装,然后仅生成 CMakeLists.txt
.可能的示例:
The best way to include OpenCV
lib on macOS is to install via homebrew opencv lib, and just generate CMakeLists.txt
. Possible example for it:
cmake_minimum_required(VERSION 3.17)
project(PROJECT_NAME)
find_package(OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
set(CMAKE_CXX_STANDARD 17)
#set(CMAKE_CXX_COMPILER "/usr/local/bin/g++-10" CACHE STRING "C++ compiler" FORCE)
#set(CMAKE_C_COMPILER "/usr/local/bin/gcc-10" CACHE STRING "C compiler" FORCE)
add_executable(PROJECT_NAME main.cpp)
target_link_libraries(PROJECT_NAME ${OpenCV_LIBS} )
请注意,如果您将强制使用 g ++-10
(取消注释集结构),则会出现
Note, that if you will force to use g++-10
(uncomment set structure) then there will be problem like described here.
I think there is no OpenCV
update for arm
, then just ignore the warning and try something like that.
这篇关于为macOS-x86_64构建,但尝试链接为macOS-arm64构建的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!