OpenCV在macOS上的编译问题

注意这个问题针对的是macOS上的C++版本的OpenCV。

遇到的问题

一个简单的程序

#include 
#include 
using namespace cv;
using namespace std;

int main()
{
    Mat image = imread("assets/test_images/tomcat.jpg", CV_LOAD_IMAGE_UNCHANGED);
    if (image.empty())
    {
        cout << "Error: can't read the file" << endl;
    }
    namedWindow("tomcat.jpg", WINDOW_AUTOSIZE);
    imshow("tomcat.jpg", image);
    cout << "Press any key to exit..." << endl;
    waitKey(); // Wait for key press
    return 0;
}

使用g++进行编译

g++ -o test test.cpp

报错信息

Undefined symbols for architecture x86_64:
  "cv::Mat::deallocate()", referenced from:
      cv::Mat::release() in test-ce0d6e.o
  "cv::String::deallocate()", referenced from:
      cv::String::~String() in test-ce0d6e.o
      cv::String::operator=(cv::String const&) in test-ce0d6e.o
  "cv::String::allocate(unsigned long)", referenced from:
      cv::String::String(char const*) in test-ce0d6e.o
  "cv::imread(cv::String const&, int)", referenced from:
      _main in test-ce0d6e.o
  "cv::fastFree(void*)", referenced from:
      cv::Mat::~Mat() in test-ce0d6e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

解决方案

在终端中

export LD_LIBRARY_PATH=$LD_LIBARARY_PATH:/usr/local/lib

然后使用这样编译就OK了

g++ -o sample test.cpp `pkg-config --cflags --libs opencv`

你可能感兴趣的:(OpenCV在macOS上的编译问题)