title: How to properly link Visual Studio project with OpenCV (superpack) using CMake
body: <p>I am currently digitizing old VHS cassettes. For post-processing, I would like to implement a custom algorithm with C++ &amp; OpenCV. I have already implemented a promising prototype in Matlab, but it can only process single images (reading / writing video files is not possible in my version (R2010a); also, Matlab is far too slow).</p>
  
  <p>Sadly, I am - over and over again - stuck with CMake. Though I wonder ... this can't be so difficult. I have <em>often</em> had problems with CMake, so I will go into a lot of detail here. I hope that you can not only point out to me what I am doing wrong here, but give general advices towards my usage of CMake as well. Maybe I am doing it all wrong, I don't know.</p>
  
  <p>Here is what I've done so far:</p>
  
  <ol>
  <li>I have downloaded the <strong>OpenCV 2.3.1 superpack</strong> from sourceforge. The superpack contains OpenCV source code, includes and - most importantly - the .lib and .dll files for all major platforms. For this reason, I need not build OpenCV myself. It is already done. I need only use/link it.</li>
  <li>I installed (i.e. extracted to) the superpack in <code>C:\dev\vs2010sp1_win32\opencv\2.3.1</code>.</li>
  <li>I have renamed <code>C:\dev\vs2010sp1_win32\opencv\2.3.1\OpenCVConfig.cmake.in</code> to <code>OpenCVConfig.cmake</code>.</li>
  <li>I have created a folder for my project <code>C:\dev\VhsDejitterizer</code> with the following structure:</li>
  </ol>
  
  <pre><code>
  VhsDejitterizer/
      CMakeLists.txt (A)
      src/
          CMakeLists.txt (B)
          libvhsdejitter/
              CMakeLists.txt (C)
              vhsdejitter/
                  util.h
                  util.cpp
          main/
              CMakeLists.txt (D)
              main.cpp
  </code></pre>
  
  <hr>
  
  <p>Here are the contents of the individual CMakeLists.txt files.</p>
  
  <p><strong>/CMakeLists.txt (A)</strong></p>
  
  <pre><code>CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
  
  PROJECT("VhsDejitterizer")
  
  CMAKE_POLICY(SET CMP0015 OLD)
  FIND_PACKAGE(OpenCV REQUIRED
              NO_MODULE
              PATHS "C:/dev/vs2010sp1_win32/opencv/2.3.1"
              NO_DEFAULT_PATH)
  
  ADD_SUBDIRECTORY("src")
  </code></pre>
  
  <p><strong>/src/CMakeLists.txt (B)</strong></p>
  
  <pre><code>ADD_SUBDIRECTORY("libvhsdejitter")
  ADD_SUBDIRECTORY("main")
  </code></pre>
  
  <p><strong>/src/libvhsdejitter/CMakeLists.txt (C)</strong></p>
  
  <pre><code>UNSET(source_files)
  FILE(GLOB_RECURSE source_files "*.h" "*.cpp")
  
  ADD_LIBRARY(libvhsdejitter STATIC ${source_files})
  TARGET_LINK_LIBRARIES(libvhsdejitter ${OpenCV_LIBS})
  
  UNSET(source_files)
  </code></pre>
  
  <p><strong>/src/main/CMakeLists.txt (D)</strong></p>
  
  <pre><code>UNSET(source_files)
  FILE(GLOB_RECURSE source_files "*.h" "*.cpp")
  
  ADD_EXECUTABLE(main ${source_files})
  TARGET_LINK_LIBRARIES(main libvhsdejitter ${OpenCV_LIBS})
  
  UNSET(source_files)
  </code></pre>
  
  <hr>
  
  <p>Configuring and generating the Visual Studio .sln (...) files works well. In fact, I am not getting a single warning or error:</p>
  
  <pre><code>Configuring done
  Generating done
  </code></pre>
  
  <p>However, my attempt to build the 'main' project in Visual Studio fails:</p>
  
  <pre><code>1&gt;------ Build started: Project: main, Configuration: Debug Win32 ------
  1&gt;Build started 04.04.2012 14:38:47.
  1&gt;InitializeBuildStatus:
  1&gt;  Touching "main.dir\Debug\main.unsuccessfulbuild".
  1&gt;CustomBuild:
  1&gt;  All outputs are up-to-date.
  1&gt;ClCompile:
  1&gt;  main.cpp
  1&gt;LINK : fatal error LNK1104: cannot open file '@CMAKE_LIB_DIRS_CONFIGCMAKE@/libopencv_gpu.so.@OPENCV_VERSION@@OPENCV_DLLVERSION@@OPENCV_DEBUG_POSTFIX@.lib'
  1&gt;
  1&gt;Build FAILED.
  1&gt;
  1&gt;Time Elapsed 00:00:00.59
  ========== Build: 0 succeeded, 1 failed, 2 up-to-date, 0 skipped ==========
  </code></pre>
  
  <hr>
  
  <p>Further details:</p>
  
  <ul>
  <li>Operating system: Windows 7 Pro 64-bit</li>
  <li>IDE: Visual Studio 2010 SP1</li>
  <li>CMake version: 2.8.4</li>
  <li>Target platform (i.e. what am I compiling/building for): Windows 32-bit</li>
  </ul>
  
  <hr>
  
  <p>My questions:</p>
  
  <ol>
  <li>How do I successfully build the 'main' project? I.e. how to fix that error?</li>
  <li>What are these <code>@VARIABLE_OR_SOMETHING@</code>? I have tried to find out where they come from, and they seem to be set up in <code>OpenCVConfig.cmake</code>. But how are they supposed to work? Are they supposed to be evaluated by Visual Studio at "build-time"? If so, how are they evaluated?</li>
  <li>You have probably noticed that I have set up quite a sophisticated folder structure. Do you have any advice on this? How do you organize your libraries and projects? Are there best-practices? Where are they documented?</li>
  </ol>
  
  <p>Thank you and best regards, Robert</p>