title: Cmake undefined reference to 'cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
body: <p>I created a small program to test OpenCV C++ with Cmake and visual studio code but the build failed because of undefined functions and I don't know why Here is my code and my CmakeList.txt
  I have already set the variable paths like this :</p>
  <pre><code>C:\opencv\build\x64\vc15\bin
  C:\opencv\build\x64\vc15\lib
  </code></pre>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;opencv2/opencv.hpp&gt;
  #include &lt;opencv2/highgui/highgui.hpp&gt;
  
  using namespace cv;
  int main()
  {
  
      Mat img = imread(&quot;lena.jpg&quot;);
      imshow(&quot;IMAGE&quot;, img);
      waitKey(0);
      return 0;
  }
  </code></pre>
  <pre><code>cmake_minimum_required(VERSION 3.0.0)
  project(TP1 VERSION 0.1.0)
  
  include(CTest)
  enable_testing()
  
  find_package(OpenCV REQUIRED)
  
  include_directories(${OpenCV_INCLUDE_DIRS})
  add_executable(TP1 main.cpp)
  
  target_link_libraries(TP1 ${OpenCV_LIBS})
  
  set(CPACK_PROJECT_NAME ${PROJECT_NAME})
  set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
  include(CPack)
  
  </code></pre>
  <p>Build output using cmake tools in vscode</p>
  <pre><code>[main] Building folder: TP1 
  [build] Starting build
  [proc] Executing command: &quot;C:\Program Files\CMake\bin\cmake.EXE&quot; --build c:/Users/amine/Desktop/My_Studies/OpenCV/TP1/build --config Debug --target all -- -j 10
  [build] Consolidate compiler generated dependencies of target TP1
  [build] [ 50%] Building CXX object CMakeFiles/TP1.dir/main.cpp.obj
  [build] [100%] Linking CXX executable TP1.exe
  [build] CMakeFiles\TP1.dir/objects.a(main.cpp.obj): In function `main':
  [build] C:/Users/amine/Desktop/My_Studies/OpenCV/TP1/main.cpp:9: undefined reference to `cv::imread(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, int)'
  [build] C:/Users/amine/Desktop/My_Studies/OpenCV/TP1/main.cpp:10: undefined reference to `cv::imshow(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, cv::_InputArray const&amp;)'
  [build] C:/Users/amine/Desktop/My_Studies/OpenCV/TP1/main.cpp:11: undefined reference to `cv::waitKey(int)'
  [build] CMakeFiles\TP1.dir/objects.a(main.cpp.obj): In function `cv::Mat::~Mat()':
  [build] C:/opencv/build/include/opencv2/core/mat.inl.hpp:751: undefined reference to `cv::fastFree(void*)'
  [build] CMakeFiles\TP1.dir/objects.a(main.cpp.obj): In function `cv::Mat::release()':
  [build] C:/opencv/build/include/opencv2/core/mat.inl.hpp:863: undefined reference to `cv::Mat::deallocate()'
  [build] collect2.exe: error: ld returned 1 exit status
  [build] mingw32-make.exe[2]: *** [CMakeFiles\TP1.dir\build.make:115: TP1.exe] Error 1
  [build] mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:370: CMakeFiles/TP1.dir/all] Error 2
  [build] mingw32-make.exe: *** [Makefile:120: all] Error 2
  [build] Build finished with exit code 2
  
  </code></pre>
  <p>Location of opencv</p>
  <pre><code>C://opencv
  </code></pre>
  <p>Location of my code sample</p>
  <pre><code>Desktop//My_studies//OpenCV//TP1
  </code></pre>