简体   繁体   English

如何使用 CMake 正确链接 Visual Studio 项目与 OpenCV(超级包)

[英]How to properly link Visual Studio project with OpenCV (superpack) using CMake

I am currently digitizing old VHS cassettes.我目前正在对旧的 VHS 磁带进行数字化处理。 For post-processing, I would like to implement a custom algorithm with C++ & 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).对于后处理,我想用 C++ 和 OpenCV 实现自定义算法。我已经在 Matlab 中实现了一个有前途的原型,但它只能处理单个图像(在我的版本 (R2010a) 中无法读取/写入视频文件) ; 另外,Matlab 太慢了)。

Sadly, I am - over and over again - stuck with CMake. Though I wonder... this can't be so difficult.可悲的是,我 - 一遍又一遍 - 坚持使用 CMake。虽然我想知道......这不会那么困难。 I have often had problems with CMake, so I will go into a lot of detail here.经常遇到 CMake 的问题,所以我会在这里详细介绍 go。 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.我希望您不仅可以指出我在这里做错了什么,还可以对我对 CMake 的使用提出一般性建议。 Maybe I am doing it all wrong, I don't know.也许我做错了,我不知道。

Here is what I've done so far:这是我到目前为止所做的:

  1. I have downloaded the OpenCV 2.3.1 superpack from sourceforge.我已经从 sourceforge 下载了OpenCV 2.3.1 超级包 The superpack contains OpenCV source code, includes and - most importantly - the.lib and.dll files for all major platforms.超级包包含 OpenCV 源代码,包括 - 最重要的 - 所有主要平台的 .lib 和 .dll 文件。 For this reason, I need not build OpenCV myself.因此,我不需要自己构建 OpenCV。 It is already done.它已经完成了。 I need only use/link it.我只需要使用/链接它。
  2. I installed (ie extracted to) the superpack in C:\dev\vs2010sp1_win32\opencv\2.3.1 .我在C:\dev\vs2010sp1_win32\opencv\2.3.1中安装(即提取到)超级包。
  3. I have renamed C:\dev\vs2010sp1_win32\opencv\2.3.1\OpenCVConfig.cmake.in to OpenCVConfig.cmake .我已将C:\dev\vs2010sp1_win32\opencv\2.3.1\OpenCVConfig.cmake.inOpenCVConfig.cmake
  4. I have created a folder for my project C:\dev\VhsDejitterizer with the following structure:我为我的项目C:\dev\VhsDejitterizer创建了一个文件夹,其结构如下:

VhsDejitterizer/
    CMakeLists.txt (A)
    src/
        CMakeLists.txt (B)
        libvhsdejitter/
            CMakeLists.txt (C)
            vhsdejitter/
                util.h
                util.cpp
        main/
            CMakeLists.txt (D)
            main.cpp

Here are the contents of the individual CMakeLists.txt files.以下是各个 CMakeLists.txt 文件的内容。

/CMakeLists.txt (A) /CMakeLists.txt (一)

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")

/src/CMakeLists.txt (B) /src/CMakeLists.txt (B)

ADD_SUBDIRECTORY("libvhsdejitter")
ADD_SUBDIRECTORY("main")

/src/libvhsdejitter/CMakeLists.txt (C) /src/libvhsdejitter/CMakeLists.txt (C)

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)

/src/main/CMakeLists.txt (D) /src/main/CMakeLists.txt (D)

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)

Configuring and generating the Visual Studio.sln (...) files works well.配置和生成 Visual Studio.sln (...) 文件效果很好。 In fact, I am not getting a single warning or error:事实上,我没有收到任何警告或错误:

Configuring done
Generating done

However, my attempt to build the 'main' project in Visual Studio fails:但是,我在 Visual Studio 中构建“主”项目的尝试失败了:

1>------ Build started: Project: main, Configuration: Debug Win32 ------
1>Build started 04.04.2012 14:38:47.
1>InitializeBuildStatus:
1>  Touching "main.dir\Debug\main.unsuccessfulbuild".
1>CustomBuild:
1>  All outputs are up-to-date.
1>ClCompile:
1>  main.cpp
1>LINK : fatal error LNK1104: cannot open file '@CMAKE_LIB_DIRS_CONFIGCMAKE@/libopencv_gpu.so.@OPENCV_VERSION@@OPENCV_DLLVERSION@@OPENCV_DEBUG_POSTFIX@.lib'
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.59
========== Build: 0 succeeded, 1 failed, 2 up-to-date, 0 skipped ==========

Further details:更多详情:

  • Operating system: Windows 7 Pro 64-bit操作系统:Windows 7 Pro 64位
  • IDE: Visual Studio 2010 SP1 IDE:Visual Studio 2010 SP1
  • CMake version: 2.8.4 CMake 版本:2.8.4
  • Target platform (ie what am I compiling/building for): Windows 32-bit目标平台(即我正在编译/构建什么):Windows 32 位

My questions:我的问题:

  1. How do I successfully build the 'main' project?如何成功构建“主要”项目? Ie how to fix that error?即如何修复该错误?
  2. What are these @VARIABLE_OR_SOMETHING@ ?这些@VARIABLE_OR_SOMETHING@是什么? I have tried to find out where they come from, and they seem to be set up in OpenCVConfig.cmake .我试图找出它们的来源,它们似乎是在OpenCVConfig.cmake中设置的。 But how are they supposed to work?但它们应该如何工作? Are they supposed to be evaluated by Visual Studio at "build-time"?它们应该在“构建时”由 Visual Studio 评估吗? If so, how are they evaluated?如果有,他们是如何评价的?
  3. 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?他们在哪里记录?

Thank you and best regards, Robert谢谢你和最好的问候,罗伯特

These variables are probably related to CMake's configure_file command, which allows you to specify a parameterised template document (typically with the extension ending in .in ) and you can substitute CMake variables into the file to create it.这些变量可能与 CMake 的configure_file命令有关,它允许您指定参数化模板文档(通常扩展名以.in结尾),您可以将 CMake 变量替换到文件中以创建它。 They are evaluated at the time of the configure_file call, which happens when running CMake. I think what's happening is that there will be a parent CMake script to the one that you've taken which will configure that file with the contents of those variables and then use it in an add_subdirectory call.它们在运行 CMake 时发生的configure_file调用时被评估。我认为发生的事情是你所采用的脚本将有一个父 CMake 脚本,它将使用这些变量的内容配置该文件,并且然后在add_subdirectory调用中使用它。 I would suggest checking for any readme that describes the top level run process (or any file which defines those variables then substitute them manually).我建议检查任何描述顶级运行过程的自述文件(或任何定义这些变量然后手动替换它们的文件)。

I have fixed it now.我现在修好了。 I think I can safely say that the whole mess was not my fault.我想我可以肯定地说整个混乱不是我的错。 Sorry for answering my own question.很抱歉回答我自己的问题。


Here is what I tried first (of course, this did not work for me, but it might work for others):这是我首先尝试的(当然,这对我不起作用,但它可能对其他人有用):

As Martin Foot pointed out in his answer, the *.in files are templates which are supposed to be filled out with proper values during a CMake configuration.正如 Martin Foot 在他的回答中指出的那样,*.in 文件是模板,应该在 CMake 配置期间填写正确的值。 However, I am using the OpenCV superpack, which includes all the binaries.但是,我使用的是 OpenCV 超级包,其中包含所有二进制文件。 For this reason I have, at no point, performed such a configuration step, because I assumed this would only be necessary if you wanted to compile something.出于这个原因,我从来没有执行过这样的配置步骤,因为我认为只有在您想要编译某些东西时才需要这样做。

However, it seems that - even if you're using the superpack with prebuilt binaries - you have to configure the project in order to get your OpenCVConfig.cmake generated.但是,似乎 - 即使您使用带有预构建二进制文件的超级包 - 您也必须配置项目才能生成 OpenCVConfig.cmake。 Vadim Pisarevsky has stated that in the OpenCV bug tracker . Vadim Pisarevsky在 OpenCV bug tracker 中表示

For some reason, this didn't work for me.出于某种原因,这对我不起作用。 I started up the Cmake GUI as usual, pointed it to the OpenCV directory and hit "Configure".我像往常一样启动 Cmake GUI,将其指向 OpenCV 目录并点击“配置”。 I even hit "Generate" out of desperation.我什至绝望地点击了“生成”。 Yet, no OpenCVConfig.cmake appeared.然而,没有出现 OpenCVConfig.cmake。

So I had to go on further...所以我不得不进一步拨打 go...


This is what actually helped:这才是真正有用的:

In a recently filed bugreport related to OpenCVConfig.cmake, Sergiu Dotenco pointed out "that the currently provided OpenCVConfig.cmake is pretty fragile" etc. etc. Fortunately, Sergio has also provided a custom FindOpenCV.cmake script .在最近提交的与 OpenCVConfig.cmake 相关的错误报告中, Sergiu Dotenco 指出“当前提供的 OpenCVConfig.cmake 非常脆弱”等等。幸运的是,Sergio 还提供了一个自定义的 FindOpenCV.cmake 脚本 By using his script I have finally been able to generate a working Visual Studio solution.通过使用他的脚本,我终于能够生成一个有效的 Visual Studio 解决方案。

By the way, this is my current top-level CMakeLists.txt:顺便说一下,这是我当前的顶级 CMakeLists.txt:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

PROJECT("VhsDejitterizer")

SET(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake-modules" ${CMAKE_MODULE_PATH})

SET(OPENCV_ROOT_DIR "C:/dev/vs2010sp1_win32/opencv/2.3.1")
#SET(OPENCV_USE_GPU_LIBS ON)
FIND_PACKAGE(OpenCV REQUIRED)

ADD_SUBDIRECTORY("src")

I have installed Sergio's FindOpenCV.cmake script in a new cmake-modules/ subfolder of the project.我在项目的新cmake-modules/子文件夹中安装了 Sergio 的FindOpenCV.cmake脚本。 It is also important to note that I am using (as opposed to my original setup, where I used the "config mode" ) the minimal FIND_PACKAGE variant ( "module mode" ).同样重要的是要注意我正在使用(与我使用“配置模式”的原始设置相反)最小的 FIND_PACKAGE 变体( “模块模式” )。 Only if the minimal variant is used, CMake is looking for Find<package-name>.cmake scripts in the CMake module path.仅当使用最小变体时,CMake 才会在 CMake 模块路径中Find<package-name>.cmake脚本。 See the CMake documentation for FIND_PACKAGE .有关 FIND_PACKAGE 的信息,请参阅CMake 文档


I have also found this guide, which is about how to properly use CMake if you're a library developer: http://www.vtk.org/Wiki/CMake/Tutorials/How_to_create_a_ProjectConfig.cmake_file我还找到了本指南,如果您是图书馆开发人员,它是关于如何正确使用 CMake 的: http://www.vtk.org/Wiki/CMake/Tutorials/How_to_create_a_ProjectConfig.cmake_file

I have fixed it now.我现在修好了。 I think I can safely say that the whole mess was not my fault.我想我可以肯定地说整个混乱不是我的错。 Sorry for answering my own question.很抱歉回答我自己的问题。

You fixed it?你修好了? I am having the same problem, and I followed your solution but it did not work.我遇到了同样的问题,我按照你的解决方案做了,但是没有用。 When CMake executed the command FIND_PACKAGE( OpenCV REQUIRED )当 CMake 执行命令 FIND_PACKAGE( OpenCV REQUIRED )

It would output:它将是 output:

One or more OpenCV components were not found:
  calib3d
  contrib
  core
  features2d
  flann
  highgui
  imgproc
  legacy
  ml
  objdetect
  video
CMake Error at C:/Program Files/CMake 2.8/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:91 (MESSAGE):
  Could NOT find OpenCV (missing: OPENCV_CALIB3D_LIBRARY
  OPENCV_CONTRIB_LIBRARY OPENCV_CORE_LIBRARY OPENCV_FEATURES2D_LIBRARY
  OPENCV_FLANN_LIBRARY OPENCV_HIGHGUI_LIBRARY OPENCV_IMGPROC_LIBRARY
  OPENCV_LEGACY_LIBRARY OPENCV_ML_LIBRARY OPENCV_OBJDETECT_LIBRARY
  OPENCV_VIDEO_LIBRARY) (found version "2.3.1")

Finally, I used the commands include_directories TARGET_LINK_LIBRARIES to include all necessary directories or files, and it works, in a awkward way!最后,我使用命令 include_directories TARGET_LINK_LIBRARIES 来包含所有必要的目录或文件,它以一种笨拙的方式工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM