title: OpenCV - undefined reference: SurfFeatureDetector and BruteForceMatcher
body: <p>I'm making a program in C++ that uses 2 images to detect SURF Features, compute the matches with a bruteforcematcher and draws it.</p>
  
  <p>Here's the code</p>
  
  <pre><code>#include &lt;cstdio&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include "opencv/cv.h"
  #include "opencv/highgui.h"
  #include "opencv2/features2d/features2d.hpp"
  
  
  using namespace cv;
  using namespace std;
  
  int main(int argc, char **argv){
          if (argc &lt;3) {
              cout &lt;&lt; "Usage: " &lt;&lt; argv[0] &lt;&lt; " imageLocation1 imageLocation2" &lt;&lt; endl;
  
              return -1;
          }
  
          Mat source1 = imread(argv[1],CV_LOAD_IMAGE_GRAYSCALE);
          Mat source2 = imread(argv[2],CV_LOAD_IMAGE_GRAYSCALE);
          if(source1.empty() || source2.empty()){
          printf("Can't load all the images!");
          return -1;
          }   
  
  //Initialise the Wrapping Class for Surf()
      SurfFeatureDetector detector(400);
  
  //detect : first param: Image, second param: vector (output)
  
      vector&lt;KeyPoint&gt; keypoints1,keypoints2;
  
      detector.detect(source1,keypoints1);
      detector.detect(source2,keypoints2);
  
  //Initialise wrapping class for descriptors computing using SURF() class.
      SurfDescriptorExtractor extractor;
  
  //Compute: Input:image, keypoints Output:descriptors
      Mat descriptors1,descriptors2;
  
      extractor.compute(source1,keypoints1,descriptors1);
      extractor.compute(source2,keypoints2,descriptors2);
  
  //Initialise BruteForceMatcher: For each descriptor in the first set, this matcher finds the closest descriptor in the second set by trying each on (=brute)
      BruteForceMatcher&lt; L2&lt;float&gt; &gt; matcher;
      vector&lt; DMatch &gt; matches;
  
  //match: execute the matcher!
      matcher.match(descriptors1,descriptors2, matches);
  
  //Draw the matches with drawMatches
      Mat target;
      drawMatches(source1,keypoints1,source2,keypoints2,matches,target); 
  
      imshow("Matches", target);
  
      waitKey(0);
  
      return 0;
  }
  </code></pre>
  
  <p>Building isn't a problem, but when linking, I get this very nasty errors:</p>
  
  <pre><code>CMakeFiles/opg13.dir/src/lennart_martens_opgave13.o: In function `cv::BruteForceMatcher&lt;cv::L2&lt;float&gt; &gt;::~BruteForceMatcher()':
  lennart_martens_opgave13.cpp:(.text._ZN2cv17BruteForceMatcherINS_2L2IfEEED2Ev[_ZN2cv17BruteForceMatcherINS_2L2IfEEED5Ev]+0xb): undefined reference to `cv::DescriptorMatcher::~DescriptorMatcher()'
  CMakeFiles/opg13.dir/src/lennart_martens_opgave13.o: In function `cv::BruteForceMatcher&lt;cv::L2&lt;float&gt; &gt;::~BruteForceMatcher()':
  lennart_martens_opgave13.cpp:(.text._ZN2cv17BruteForceMatcherINS_2L2IfEEED0Ev[_ZN2cv17BruteForceMatcherINS_2L2IfEEED5Ev]+0x12): undefined reference to `cv::DescriptorMatcher::~DescriptorMatcher()'
  CMakeFiles/opg13.dir/src/lennart_martens_opgave13.o: In function `main':
  lennart_martens_opgave13.cpp:(.text.startup+0x172): undefined reference to `cv::SurfFeatureDetector::SurfFeatureDetector(double, int, int, bool)'
  lennart_martens_opgave13.cpp:(.text.startup+0x24f): undefined reference to `cv::FeatureDetector::detect(cv::Mat const&amp;, std::vector&lt;cv::KeyPoint, std::allocator&lt;cv::KeyPoint&gt; &gt;&amp;, cv::Mat const&amp;) const'
  lennart_martens_opgave13.cpp:(.text.startup+0x30a): undefined reference to `cv::FeatureDetector::detect(cv::Mat const&amp;, std::vector&lt;cv::KeyPoint, std::allocator&lt;cv::KeyPoint&gt; &gt;&amp;, cv::Mat const&amp;) const'
  lennart_martens_opgave13.cpp:(.text.startup+0x346): undefined reference to `cv::SurfDescriptorExtractor::SurfDescriptorExtractor(int, int, bool, bool)'
  lennart_martens_opgave13.cpp:(.text.startup+0x495): undefined reference to `cv::DescriptorExtractor::compute(cv::Mat const&amp;, std::vector&lt;cv::KeyPoint, std::allocator&lt;cv::KeyPoint&gt; &gt;&amp;, cv::Mat&amp;) const'
  lennart_martens_opgave13.cpp:(.text.startup+0x4bb): undefined reference to `cv::DescriptorExtractor::compute(cv::Mat const&amp;, std::vector&lt;cv::KeyPoint, std::allocator&lt;cv::KeyPoint&gt; &gt;&amp;, cv::Mat&amp;) const'
  lennart_martens_opgave13.cpp:(.text.startup+0x5ac): undefined reference to `cv::DescriptorMatcher::match(cv::Mat const&amp;, cv::Mat const&amp;, std::vector&lt;cv::DMatch, std::allocator&lt;cv::DMatch&gt; &gt;&amp;, cv::Mat const&amp;) const'
  lennart_martens_opgave13.cpp:(.text.startup+0x6de): undefined reference to `cv::drawMatches(cv::Mat const&amp;, std::vector&lt;cv::KeyPoint, std::allocator&lt;cv::KeyPoint&gt; &gt; const&amp;, cv::Mat const&amp;, std::vector&lt;cv::KeyPoint, std::allocator&lt;cv::KeyPoint&gt; &gt; const&amp;, std::vector&lt;cv::DMatch, std::allocator&lt;cv::DMatch&gt; &gt; const&amp;, cv::Mat&amp;, cv::Scalar_&lt;double&gt; const&amp;, cv::Scalar_&lt;double&gt; const&amp;, std::vector&lt;char, std::allocator&lt;char&gt; &gt; const&amp;, int)'
  lennart_martens_opgave13.cpp:(.text.startup+0x781): undefined reference to `cv::DescriptorMatcher::~DescriptorMatcher()'
  lennart_martens_opgave13.cpp:(.text.startup+0x7ad): undefined reference to `vtable for cv::SurfDescriptorExtractor'
  lennart_martens_opgave13.cpp:(.text.startup+0x7b5): undefined reference to `cv::DescriptorExtractor::~DescriptorExtractor()'
  lennart_martens_opgave13.cpp:(.text.startup+0x7d8): undefined reference to `vtable for cv::SurfFeatureDetector'
  lennart_martens_opgave13.cpp:(.text.startup+0x7e0): undefined reference to `cv::FeatureDetector::~FeatureDetector()'
  lennart_martens_opgave13.cpp:(.text.startup+0x8c8): undefined reference to `vtable for cv::SurfFeatureDetector'
  lennart_martens_opgave13.cpp:(.text.startup+0x8d0): undefined reference to `cv::FeatureDetector::~FeatureDetector()'
  lennart_martens_opgave13.cpp:(.text.startup+0x942): undefined reference to `vtable for cv::SurfDescriptorExtractor'
  lennart_martens_opgave13.cpp:(.text.startup+0x94a): undefined reference to `cv::DescriptorExtractor::~DescriptorExtractor()'
  lennart_martens_opgave13.cpp:(.text.startup+0x9a2): undefined reference to `cv::DescriptorMatcher::~DescriptorMatcher()'
  CMakeFiles/opg13.dir/src/lennart_martens_opgave13.o:(.rodata._ZTVN2cv17BruteForceMatcherINS_2L2IfEEEE[vtable for cv::BruteForceMatcher&lt;cv::L2&lt;float&gt; &gt;]+0x10): undefined reference to `cv::DescriptorMatcher::add(std::vector&lt;cv::Mat, std::allocator&lt;cv::Mat&gt; &gt; const&amp;)'
  CMakeFiles/opg13.dir/src/lennart_martens_opgave13.o:(.rodata._ZTVN2cv17BruteForceMatcherINS_2L2IfEEEE[vtable for cv::BruteForceMatcher&lt;cv::L2&lt;float&gt; &gt;]+0x14): undefined reference to `cv::DescriptorMatcher::clear()'
  CMakeFiles/opg13.dir/src/lennart_martens_opgave13.o:(.rodata._ZTVN2cv17BruteForceMatcherINS_2L2IfEEEE[vtable for cv::BruteForceMatcher&lt;cv::L2&lt;float&gt; &gt;]+0x18): undefined reference to `cv::DescriptorMatcher::empty() const'
  CMakeFiles/opg13.dir/src/lennart_martens_opgave13.o:(.rodata._ZTVN2cv17BruteForceMatcherINS_2L2IfEEEE[vtable for cv::BruteForceMatcher&lt;cv::L2&lt;float&gt; &gt;]+0x20): undefined reference to `cv::DescriptorMatcher::train()'
  CMakeFiles/opg13.dir/src/lennart_martens_opgave13.o:(.rodata._ZTVN2cv17BruteForceMatcherINS_2L2IfEEEE[vtable for cv::BruteForceMatcher&lt;cv::L2&lt;float&gt; &gt;]+0x24): undefined reference to `cv::DescriptorMatcher::read(cv::FileNode const&amp;)'
  CMakeFiles/opg13.dir/src/lennart_martens_opgave13.o:(.rodata._ZTVN2cv17BruteForceMatcherINS_2L2IfEEEE[vtable for cv::BruteForceMatcher&lt;cv::L2&lt;float&gt; &gt;]+0x28): undefined reference to `cv::DescriptorMatcher::write(cv::FileStorage&amp;) const'
  CMakeFiles/opg13.dir/src/lennart_martens_opgave13.o:(.rodata._ZTVN2cv17BruteForceMatcherINS_2L2IfEEEE[vtable for cv::BruteForceMatcher&lt;cv::L2&lt;float&gt; &gt;]+0x30): undefined reference to `cv::BruteForceMatcher&lt;cv::L2&lt;float&gt; &gt;::knnMatchImpl(cv::Mat const&amp;, std::vector&lt;std::vector&lt;cv::DMatch, std::allocator&lt;cv::DMatch&gt; &gt;, std::allocator&lt;std::vector&lt;cv::DMatch, std::allocator&lt;cv::DMatch&gt; &gt; &gt; &gt;&amp;, int, std::vector&lt;cv::Mat, std::allocator&lt;cv::Mat&gt; &gt; const&amp;, bool)'
  CMakeFiles/opg13.dir/src/lennart_martens_opgave13.o:(.rodata._ZTVN2cv17BruteForceMatcherINS_2L2IfEEEE[vtable for cv::BruteForceMatcher&lt;cv::L2&lt;float&gt; &gt;]+0x34): undefined reference to `cv::BruteForceMatcher&lt;cv::L2&lt;float&gt; &gt;::radiusMatchImpl(cv::Mat const&amp;, std::vector&lt;std::vector&lt;cv::DMatch, std::allocator&lt;cv::DMatch&gt; &gt;, std::allocator&lt;std::vector&lt;cv::DMatch, std::allocator&lt;cv::DMatch&gt; &gt; &gt; &gt;&amp;, float, std::vector&lt;cv::Mat, std::allocator&lt;cv::Mat&gt; &gt; const&amp;, bool)'
  CMakeFiles/opg13.dir/src/lennart_martens_opgave13.o:(.rodata._ZTIN2cv17BruteForceMatcherINS_2L2IfEEEE[typeinfo for cv::BruteForceMatcher&lt;cv::L2&lt;float&gt; &gt;]+0x8): undefined reference to `typeinfo for cv::DescriptorMatcher'
  collect2: ld gaf exit-status 1 terug
  make[2]: *** [bin/opg13] Fout 1
  make[1]: *** [CMakeFiles/opg13.dir/all] Fout 2
  make: *** [all] Fout 2
  </code></pre>
  
  <p>I really don't know what the problem is. I didn't find a thing at the Internet. Hope someone can help!</p>
  
  <p>Edit: This is my CMakeLists.txt:</p>
  
  <pre><code>cmake_minimum_required(VERSION 2.4)
  
  
  PROJECT(LABO5)
  
  # paths 
  INCLUDE_DIRECTORIES(src)
  INCLUDE_DIRECTORIES(/usr/local/include)
  LINK_DIRECTORIES(/usr/local/lib)
  LINK_DIRECTORIES(/usr/lib)
  SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)
  SET(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)
  SET(CMAKE_CXX_FLAGS "-o3 -w")
  SET(CMAKE_CXX_LINK_FLAGS "-pg")
  SET(OpenCV_LIBRARIES opencv_core opencv_highgui opencv_imgproc )
  
  
  ADD_EXECUTABLE(opg13 src/lennart_martens_opgave13.cpp)
  TARGET_LINK_LIBRARIES(opg13 ${OpenCV_LIBRARIES})
  SET(CMAKE_BUILD_TYPE Release)
  </code></pre>