OpenCV: undefined reference to `cv::imread when using cmake
<p>I have to use OpenCV on two systems, both Linux. My setup works on the first system, but fails on the second. For both systems I have installed OpenCV in a custom location using:</p>

<pre><code>cd myBuildDir
cmake -DWITH_VTK=OFF -DCMAKE_BUILD_TYPE=Release 
  -DCMAKE_INSTALL_PREFIX=~/Software/OpenCVLibs/opencv-3.1.0/release 
  ~/Software/opencv-3.1.0 
make &amp;&amp; make install
</code></pre>

<p>and similar for debug. The directories are different. One is with my user on my laptop, the other with a different user on a multiuser system.</p>

<p>I then used the example given in the OpenCV documentation to test my setup:
<a href="http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html" rel="nofollow">http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html</a></p>

<p>To build the example using my custom OpenCV location I used</p>

<pre><code>cmake -DCMAKE_BUILD_TYPE=Release -DOpenCV_DIR=~/Software/OpenCVLibs
  /opencv-3.1.0/release/share/OpenCV exampleSourceDir
</code></pre>

<p>This works on the first system (gcc version 5.3.0) but fails on the second (gcc version 4.6.3). Don't think the gcc version matter here, but stating them for completeness. I think it might be related to that on the first system there is no OpenCV in the system path installed, while in the second system there is. However the build fails with:</p>

<pre><code>/usr/bin/cmake -E cmake_link_script CMakeFiles/DisplayImage.dir/link.txt --verbose=1
/usr/bin/c++   -O3 -DNDEBUG    CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o  -o DisplayImage -rdynamic /home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_videostab.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_videoio.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_video.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_superres.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_stitching.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_shape.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_photo.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_objdetect.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_ml.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_imgproc.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_imgcodecs.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_highgui.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_flann.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_features2d.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_core.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_calib3d.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_features2d.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_ml.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_highgui.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_videoio.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_imgcodecs.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_flann.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_video.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_imgproc.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_core.so.3.1.0 -Wl,-rpath,
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib 
CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o: In function `main':
DisplayImage.cpp:(.text.startup+0xb8): undefined reference to `cv::imread(std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, int)'
DisplayImage.cpp:(.text.startup+0x1d7): undefined reference to `cv::namedWindow(std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, int)'
DisplayImage.cpp:(.text.startup+0x1f4): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&amp;)'
DisplayImage.cpp:(.text.startup+0x223): undefined reference to `cv::imshow(std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, cv::_InputArray const&amp;)'
collect2: ld returned 1 exit status
</code></pre>

<p>From this call, I figure that the libraries in the custom location should have been used. Why does it fail then? I ran opencv_test_imgcodecs on both machines, which works fine. 
Any ideas how to fix my problem? Is it because of the other OpenCV installation on the second system?
Thank in advance for any help!</p>