Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

c++ - OpenCV Undefined symbols for architecture x86_64: error

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:

brew tap homebrew/science
brew install opencv

I am using the following simple file I found online to test my set up:

// Example showing how to read and write images
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

int main(int argc, char** argv)
{   
    // Load an image from file - change this based on your image name
    Mat img = imread("my_image.jpg", CV_LOAD_IMAGE_UNCHANGED);

    if(img.empty())
    {
        fprintf(stderr, "failed to load input image
");
        return -1;
    }

    // this is just to show, that you won't have to pre-alloc
    // result-images with c++ any more..
    Mat gray;
    cvtColor(img,gray,CV_BGR2GRAY);

    // Write the image to a file with a different name,
    // using a different image format -- .png instead of .jpg
    if( ! imwrite("my_image_copy.png", img) )
    {
        fprintf(stderr, "failed to write image file
");
    }

    // no need to release anything with c++ !   
    return 0;
}

This is the error I am getting after trying to compile and run suing g++ test.cpp -o test.

Undefined symbols for architecture x86_64:
  "cv::_InputArray::_InputArray(cv::Mat const&)", referenced from:
      _main in test-41a30e.o
  "cv::namedWindow(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
      _main in test-41a30e.o
  "cv::destroyWindow(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
      _main in test-41a30e.o
  "cv::Mat::deallocate()", referenced from:
      cv::Mat::release() in test-41a30e.o
  "cv::imread(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
      _main in test-41a30e.o
  "cv::imshow(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, cv::_InputArray const&)", referenced from:
      _main in test-41a30e.o
  "cv::waitKey(int)", referenced from:
      _main in test-41a30e.o
  "cv::fastFree(void*)", referenced from:
      cv::Mat::~Mat() in test-41a30e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I did find similar issues on stack overflow but none using homebrew and I'm not sure how time- sensitive (with regards to updates and changes to OpenCV, my OS etc) the problem is. I'm using a mac with the 10.9.4 OS. Also those answers weren't really clear to me what should be changed. Many just suggested one line of code without saying where the line should go. I'm extremely novice with these technologies. Any help would be great thanks!

Edit: Mark Setchell helped with the first batch of errors. But I am now getting: g++ $(pkg-config --cflags --libs opencv) test.cpp -o Test & ./Test

[3] 7187 dyld: Library not loaded: lib/libopencv_calib3d.2.4.dylib Referenced from: /usr/local/Cellar/opencv/2.4.9/include/./Test Reason: image not found [2] Done g++ $(pkg-config --cflags --libs opencv) test.cpp -o Test Trace/BPT trap: 5

brewdoctor gave me this warning: Warning: You have unlinked kegs in your Cellar Leaving kegs unlinked can lead to build-trouble and cause brews that depend on those kegs to fail to run properly once built. Run brew link on these:

opencv

So I ran brew link --overwrite opencv but got this:

Linking /usr/local/Cellar/opencv/2.4.9... 
Error: Could not symlink include/opencv/cv.h

needed the overwrite because brew link opencv gave

Linking /usr/local/Cellar/opencv/2.4.9... 
Error: Could not symlink include/opencv/cv.h
Target /usr/local/include/opencv/cv.h
already exists. You may want to remove it:
  rm /usr/local/include/opencv/cv.h
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are getting linker errors. I think that is because you are not linking with the correct libraries that opencv requires.

The easiest way to get the correct libraries is with pkg-config, so I would recommend:

brew install pkg-config

Then you may have to reinstall opencv

brew reinstall opencv

or maybe

brew uninstall opencv
brew install opencv

Then you should be able to do:

g++ $(pkg-config --cflags --libs opencv) test.cpp -o Test 

You can run

pkg-config --cflags --libs opencv4

to see what it outputs for the g++ compiler if you are interested - it tells the compiler where the libraries and header files are. On my system, it outputs this:

pkg-config --cflags --libs opencv4

-I/usr/local/Cellar/opencv/2.4.12_2/include/opencv 
-I/usr/local/Cellar/opencv/2.4.12_2/include 
-L/usr/local/Cellar/opencv/2.4.12_2/lib 
-lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab

If you are unfamiliar with pkgconfig, you can ask it to tell you all the packages it knows about like this:

pkg-config --list-all

My system gives output like this:

libzmq                              libzmq - 0MQ c++ library
gio-unix-2.0                        GIO unix specific APIs - unix specific headers for glib I/O library
glibmm-2.4                          glibmm - C++ wrapper for GLib
libpostproc                         libpostproc - FFmpeg postprocessing library
libgsf-1                            libgsf-1 - A library for reading and writing structured files (eg MS OLE and Zip)
gobject-introspection-no-export-1.0 gobject-introspection - GObject Introspection
libtasn1                            libtasn1 - Library for ASN.1 and DER manipulation
libusb-1.0                          libusb-1.0 - C API for USB device access from Linux, Mac OS X, Windows and OpenBSD/NetBSD userspace
gio-2.0                             GIO - glib I/O library
libusb                              libusb - USB access library (libusb-1.0 compat wrapper)
libecpg_compat                      libecpg_compat - PostgreSQL libecpg_compat library
QtNetwork                           Qtnetwork - Qtnetwork Library
opencv4                              OpenCV - Open Source Computer Vision Library

Basically, the first word in each line of the list above tells you the names of the packages pkgconfig knows about and that is the name you should specify when running commands such as

pkg-config --cflags --libs opencv4

If your system is a bit messed up, you can find the .pc file that pkgconfig uses like this:

find /usr/local/Cellar -name *.pc

or

find /usr/local/Cellar -name *.pc | grep -i opencv

/usr/local/Cellar/opencv3/3.1.0_3/lib/pkgconfig/opencv.pc

And then use the config file directly, like this:

pkg-config --cflags --libs /usr/local/Cellar/opencv3/3.1.0_3/lib/pkgconfig/opencv.pc 

If it all compiles correctly, you can run it with

./test

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...