building for macOS-x86_64 but attempting to link with file built for macOS-arm64

对着背影说爱祢 提交于 2021-02-10 18:00:25

问题


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;
}

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

but I get this error:

...
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)

回答1:


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.

Way to compile OpenCV whith g++ via terminal as you provide in the example is described here

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} )

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.



来源:https://stackoverflow.com/questions/65798262/building-for-macos-x86-64-but-attempting-to-link-with-file-built-for-macos-arm64

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!