title: OpenCV Undefined symbols for architecture x86_64: error
body: <p>before this gets flagged as a repeat question please read the end. Thanks for looking.</p>
  
  <p>I set up openCV using home-brew. </p>
  
  <p>These were the commands I used:</p>
  
  <pre><code>brew tap homebrew/science
  brew install opencv
  </code></pre>
  
  <p>I am using the following simple file I found online to test my set up:</p>
  
  <pre><code>// Example showing how to read and write images
  #include &lt;opencv2/opencv.hpp&gt;
  #include &lt;opencv2/highgui/highgui.hpp&gt;
  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\n");
          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\n");
      }
  
      // no need to release anything with c++ !   
      return 0;
  }
  </code></pre>
  
  <p>This is the error I am getting after trying to compile and run suing <code>g++ test.cpp -o test</code>. </p>
  
  <pre><code>Undefined symbols for architecture x86_64:
    "cv::_InputArray::_InputArray(cv::Mat const&amp;)", referenced from:
        _main in test-41a30e.o
    "cv::namedWindow(std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt; const&amp;, int)", referenced from:
        _main in test-41a30e.o
    "cv::destroyWindow(std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt; const&amp;)", 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&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt; const&amp;, int)", referenced from:
        _main in test-41a30e.o
    "cv::imshow(std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt; const&amp;, cv::_InputArray const&amp;)", 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)
  </code></pre>
  
  <p>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!</p>
  
  <p>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 &amp; ./Test</p>
  
  <p>[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</p>
  
  <p>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 <code>brew link</code> on these:</p>
  
  <pre><code>opencv
  </code></pre>
  
  <p>So I ran  brew link --overwrite opencv but got this:</p>
  
  <pre><code>Linking /usr/local/Cellar/opencv/2.4.9... 
  Error: Could not symlink include/opencv/cv.h
  </code></pre>
  
  <p>needed the overwrite because brew link opencv gave</p>
  
  <pre><code>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
  </code></pre>