问题
on Mac OSX 10.8, XCode 4.6, in C++
I'm following the tutorial in opencv_tutorials.pdf
, located in /opt/local/share/OpenCV/doc
on my system. On page 311, we get this sample (slightly edited):
#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
int main( int argc, char** argv ) {
Mat img_1 = imread( argv[1], CV_LOAD_IMAGE_GRAYSCALE );
int minHessian = 400;
SurfFeatureDetector detector( minHessian );
std::vector<KeyPoint> keypoints_1;
detector.detect( img_1, keypoints_1 );
}
I had to modify the code above to #include "opencv2/nonFree/features2d.hpp"
, a file that I found by running
find /opt/local/include -name "*.hpp" -exec grep SurfFeatureDetector "{}" ';' -print
The code compiles, meaning that the symbol detector.detect
is found in the hpp
file and passes type-checking by the C++ compiler. Next is to try to find the libraries that contain the symbols.
Now, I have these libraries in my distribution, in /opt/local/lib
:
libopencv_calib3d.2.4.3.dylib
libopencv_contrib.2.4.3.dylib
libopencv_core.2.4.3.dylib
libopencv_features2d.2.4.3.dylib
libopencv_flann.2.4.3.dylib
libopencv_gpu.2.4.3.dylib
libopencv_highgui.2.4.3.dylib
libopencv_imgproc.2.4.3.dylib
libopencv_legacy.2.4.3.dylib
libopencv_ml.2.4.3.dylib
libopencv_nonfree.2.4.3.dylib
libopencv_objdetect.2.4.3.dylib
libopencv_photo.2.4.3.dylib
libopencv_stitching.2.4.3.dylib
libopencv_ts.2.4.3.dylib
libopencv_video.2.4.3.dylib
libopencv_videostab.2.4.3.dylib
I found it uninformative to nm
search these for the required symbols because name mangling is difficult to decompile in my head. I just brute-force included all the libraries, but still get linker errors:
Undefined symbols for architecture x86_64:
"cv::imread(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
_main in main.o
"cv::FeatureDetector::detect(cv::Mat const&, std::__1::vector<cv::KeyPoint, std::__1::allocator<cv::KeyPoint> >&, cv::Mat const&) const", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
I've done some spot-checking of the OpenCv reference documents and the wiki pages, starting from here
http://opencv.willowgarage.com/documentation/cpp/features2d__feature_detection_and_descriptor_extraction.html
but without success.
I'd be grateful for
- specific help in finding these OpenCv APIs
- general strategies for this kind of query: "what library contains the symbol I need to link against". This kind of problem makes me hate C++, since 5 minutes of coding always seems to lead to hours of hit-and-miss library searching, possibly because I just don't know the professional secret for doing this.
回答1:
My toolchain utilizes CMake to keep track of these sorts of things. For most new libraries I use, there are usually google searchable accompanying Find*.cmake files that are written by others. If you wanted more information about my specific set-up, feel free to ask. I have seen this specific problem (Undefined symbols for architecture x86_64) a few times for things in the nonfree libraries. I'm not sure why you are getting that error for the imread, though. The first place I would look is the matcher_simple.cpp example in the samples/cpp folder. If you can compile and run this (using this) and the other examples, your set-up should be ok. If you can't, can you please post the errors of that example? I'm not familiar with XCode but someone else suggested going to Build Settings>Build Options> Compiler for C/C++/Objective-C and change it from Apple LLVM Compiler to LLVM GCC 4.2. And there are a few other guesses here too.
回答2:
On Linux and using OpenCV 3.0.0 I had to link with the opencv_imgcodecs
shared lib.
回答3:
Try including -lopencv_features2d
and -lopencv_nonfree
in your list of Other Linker Flags in the Build Settings. I was getting the same problems and it worked for me
来源:https://stackoverflow.com/questions/14940126/opencv-2-4-3-cant-find-imread-and-surffeaturedetectordetect