OpenCV Undefined symbols for architecture x86_64: error

后端 未结 3 438
别跟我提以往
别跟我提以往 2020-12-04 14:09

before this gets flagged as a repeat question please read the end. Thanks for looking.

I set up openCV using home-brew.

These were the commands I used:

3条回答
  •  时光说笑
    2020-12-04 14:40

    You are getting these errors because you are using OpenCV methods in your code that are not linked to your project.

    So far you have only included:

    #include 
    #include 
    

    But to run your code you need far more then these two OpenCV libraries. For example let's take the following error when trying to Build your project in XCODE:

    Undefined symbols for architecture x86_64:
      "cv::imread(cv::String const&, int)", referenced from:
          _main in main.o
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    

    which corresponds to this part of your error:

    ...  "cv::imread(std::__1::basic_string, std::__1::allocator > const&, int)", referenced from:
          _main in test-41a30e.o  ...
    

    This occurs because XCODE is looking to execute the following function in your code:

    Mat img = imread("my_image.jpg", CV_LOAD_IMAGE_UNCHANGED);
    

    but the way you've implemented this, it doesn't know what imread() is, because the reference to the opencv library is missing! the Library that solves this particular problem is:

    libopencv_imgcodecs.3.1.0.dylib

    (Of course the version depends on whatever version of OpenCV you are using.)

    To resolve this particular error you have to do 2 steps:

    1. Add the library to your project: Right-click on your project file. Click on "Add files to blabla" and then browse to your OpenCV directory. The libraries should be located in

    /Users/YOURNAME/YOURFOLDER/opencv-3.1.0/build/lib

    Caution! This could depend on how you've installed OpenCV! If you have installed it using Homebrew.The directory shoulb be here:

    /usr/local/Cellar/opencv/2.4.9/lib
    

    Select the following library (libopencv_imgcodecs.3.1.0.dylib) and add it to your project:

    After you have added the Library to your Project it should appear in the list on the left. Double Check that you've picked the right library. Some of them have quite similar names!

    1. Include the Library in your C++ File

    As you've done it with:

    #include 
    #include 
    

    You should now go on and include the new Library as well:

    #include "opencv2/imgcodecs/imgcodecs.hpp"
    

    1. Run your program

    When you build your program again, you should have one error less. And XCODE shouldn't have a problem to run cv::imread.

    As you have plenty of errors, you should now go on by checking which libraries you need to successfully build your program.

    I would recommend to Link these libraries as they are very common:

    #include 
    #include "opencv2/highgui/highgui.hpp"
    #include "opencv2/imgproc/imgproc.hpp"
    #include "opencv2/core/core.hpp"
    #include "opencv2/video/video.hpp"
    #include "opencv2/imgcodecs/imgcodecs.hpp"
    

    Before going crazy over all the errors you could just link all the libraries and see if it builds correctly.

提交回复
热议问题