【OpenCV--报错】Undefined symbols for architecture x86_64

Context

报这个错,我一直当是library找不到。没想到最后是没写 头文件选项 -I

报错信息

Undefined symbols for architecture x86_64:
  "cv::namedWindow(cv::String const&, int)", referenced from:
      _main in HelloWorld-f6034d.o
  "cv::String::deallocate()", referenced from:
      cv::String::~String() in HelloWorld-f6034d.o
      cv::String::operator=(cv::String const&) in HelloWorld-f6034d.o
  "cv::String::allocate(unsigned long)", referenced from:
      cv::String::String(char const*) in HelloWorld-f6034d.o
  "cv::imread(cv::String const&, int)", referenced from:
      _main in HelloWorld-f6034d.o
  "cv::imshow(cv::String const&, cv::_InputArray const&)", referenced from:
      _main in HelloWorld-f6034d.o

Source Code

#include 
#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

    Mat image;
    image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file

    if(! image.data )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", image );                   // Show our image inside it.

    waitKey(0);                                          // Wait for a keystroke in the window
    return 0;
}

终结解决方案pkg-config

pkg-config命令。用于在compile source code时 插入正确的 头文件、library选项的工具。

g++ HelloWorld.cpp -o hello `pkg-config --cflags --libs opencv`

单独运行下,可以看到输出的 头文件、library的选项

$ pkg-config --cflags --libs opencv
-I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core

扩展阅读

这类的原因StackOverFlow

你可能感兴趣的:(opencv)