title: OpenCV - only working with g++. Not gcc or nvcc
body: <p>I have installed OpenCV.
  I have been able to compile some code but sometimes it does not work. The example below does not work.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "opencv2/opencv.hpp"
  #include "opencv2/gpu/gpu.hpp"
  
  int main (int argc, char* argv[])
  {
      try
      {
          cv::Mat src_host = cv::imread("building.jpg", CV_LOAD_IMAGE_GRAYSCALE);
          cv::gpu::GpuMat dst, src;
          src.upload(src_host);
  
          cv::gpu::threshold(src, dst, 128.0, 255.0, CV_THRESH_BINARY);
  
          cv::Mat result_host = dst;
          cv::imshow("Result", result_host);
          cv::waitKey();
      }
      catch(const cv::Exception&amp; ex)
      {
          std::cout &lt;&lt; "Error: " &lt;&lt; ex.what() &lt;&lt; std::endl;
      }
      return 0;
  }
  </code></pre>
  
  <p>I get the following errors when I compile with </p>
  
  <p>gcc Test.cpp $(pkg-config --cflags --libs opencv)</p>
  
  <pre><code>gcc Test.cpp $(pkg-config --cflags --libs opencv)
  Undefined symbols for architecture x86_64:
  "std::allocator&lt;char&gt;::allocator()", referenced from:
    _main in ccnzUIww.o
  "std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::basic_string(char const*, std::allocator&lt;char&gt; const&amp;)", referenced from:
    _main in ccnzUIww.o
  "std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::~basic_string()", referenced from:
    _main in ccnzUIww.o
  "std::terminate()", referenced from:
    _main in ccnzUIww.o
  "std::allocator&lt;char&gt;::~allocator()", referenced from:
    _main in ccnzUIww.o
  "cv::gpu::GpuMat::upload(cv::Mat const&amp;)", referenced from:
    _main in ccnzUIww.o
  "cv::gpu::Stream::Null()", referenced from:
    _main in ccnzUIww.o
  "cv::gpu::threshold(cv::gpu::GpuMat const&amp;, cv::gpu::GpuMat&amp;, double, double, int, cv::gpu::Stream&amp;)", referenced from:
    _main in ccnzUIww.o
  "cv::gpu::GpuMat::operator cv::Mat() const", referenced from:
    _main in ccnzUIww.o
  "___cxa_begin_catch", referenced from:
    _main in ccnzUIww.o
  "std::cout", referenced from:
    _main in ccnzUIww.o
  "std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::operator&lt;&lt; &lt;std::char_traits&lt;char&gt; &gt;(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, char const*)", referenced from:
    _main in ccnzUIww.o
  "std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::endl&lt;char, std::char_traits&lt;char&gt; &gt;(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;)", referenced from:
    _main in ccnzUIww.o
  "std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;::operator&lt;&lt;(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; (*)(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;))", referenced from:
    _main in ccnzUIww.o
  "___cxa_end_catch", referenced from:
    _main in ccnzUIww.o
  "std::ios_base::Init::Init()", referenced from:
    __static_initialization_and_destruction_0(int, int)in ccnzUIww.o
  "std::ios_base::Init::~Init()", referenced from:
    ___tcf_0 in ccnzUIww.o
  "cv::gpu::GpuMat::release()", referenced from:
    cv::gpu::GpuMat::~GpuMat()in ccnzUIww.o
  "___gxx_personality_v0", referenced from:
    Dwarf Exception Unwind Info (__eh_frame) in ccnzUIww.o
  ld: symbol(s) not found for architecture x86_64
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>I have also tried:</p>
  
  <ul>
  <li>gcc Test.cpp $(pkg-config --cflags --libs opencv) -m32</li>
  <li>gcc Test.cpp $(pkg-config --cflags --libs opencv) -m64</li>
  <li>nvcc Test.cpp $(pkg-config --cflags --libs opencv)</li>
  </ul>
  
  <p>But this also give errors. I have looked for answers on stackoverflow and found this. <a href="https://stackoverflow.com/questions/6842197/compiling-a-basic-opencv-cuda-program-on-linux">Compiling OpenCV CUDA program</a>
  In this answer the solution is to use this command: </p>
  
  <pre><code>g++ Test.cpp `pkg-config --cflags --libs opencv` -lopencv_gpu
  </code></pre>
  
  <p>And it works! I have tried giving the same argument to the gcc and nvcc compiler but then I get errors again. This is a problem since I have to use the nvcc compiler because I want to use OpenCV in a CUDA project.</p>
  
  <p>I have little experience with C and C++ so there is a possibility that it is something obvious :)</p>