简体   繁体   English

在 Cygwin 上使用 CMake 编译 OpenCV 项目,并为 Windows 安装 OpenCV

[英]Compiling an OpenCV project using CMake on Cygwin, with OpenCV installed for Windows

Situation:情况:

I have a Windows 7 machine with OpenCV for Windows installed on it.我有一台装有OpenCV for Windows的 Windows 7 机器。 My OpenCV C++ projects work fine with Visual Studio 2010.我的 OpenCV C++ 项目在 Visual Studio 2010 中运行良好。

I want to run my existing OpenCV C++ projects to run on Raspberry Pi and on other Linux machines.我想运行我现有的 OpenCV C++ 项目以在 Raspberry Pi 和其他 Linux 机器上运行。

Trouble麻烦

As a first step, I am trying to compile my .cpp & .h files using GCC on Cygwin on my Windows Machine.作为第一步,我尝试在 Windows 机器上的Cygwin上使用GCC编译我的 .cpp 和 .h 文件。 For this purpose I am trying to use CMake package for Cygwin , but CMake gives error that it cannot find OpenCV package.为此,我尝试将CMake 包用于 Cygwin ,但CMake给出了找不到 OpenCV 包的错误。 (error is listed below). (错误在下面列出)。

Question:题:

  1. Do i need to install OpenCV package for Cygwin separately for CMake to work ?我是否需要单独为Cygwin安装OpenCV 包才能让 CMake 工作?
  2. Is there a way to using my existing OpenCV for Win to work with CMake ?有没有办法使用我现有的OpenCV for Win与 CMake 一起工作?
  3. What environment variables need to be set ?需要设置哪些环境变量?
    (Currently only a variable OPENCV_DIR is set to C:/opencv/build) (目前只有一个变量 OPENCV_DIR 设置为 C:/opencv/build)

CMakeLists.txt CMakeLists.txt

cmake_minimum_required(VERSION 2.8.4) 
PROJECT (testProj)
find_package(OpenCV REQUIRED )
set( NAME_SRC
    src/main.cpp    
    src/imgProcess.cpp    
)

set( NAME_HEADERS       
     include/imgProcess.h
)

INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/include )
link_directories( ${CMAKE_BINARY_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
add_executable( testProj ${NAME_SRC} ${NAME_HEADERS} )

target_link_libraries( test ${OpenCV_LIBS} )

cmake error错误

$ cmake .
-- The C compiler identification is GNU 4.9.3
-- The CXX compiler identification is GNU 4.9.3
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++.exe
-- Check for working CXX compiler: /usr/bin/c++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:3 (find_package):
  By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "OpenCV", but
  CMake did not find one.

  Could not find a package configuration file provided by "OpenCV" with any
  of the following names:

    OpenCVConfig.cmake
    opencv-config.cmake

  Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
  "OpenCV_DIR" to a directory containing one of the above files.  If "OpenCV"
  provides a separate development package or SDK, be sure it has been
  installed.
-- Configuring incomplete, errors occurred!
See also "/cygdrive/c/Work/Project/cmaketest/CMakeFiles/CMakeOutput.log".

该变量应称为OpenCV_DIR ,它必须与find_package(OpenCV REQUIRED)中的find_package(OpenCV REQUIRED)相同。

As @berak, stated in the comment, I installed OpenCV Package for Cygwin using Cygwin Ports.正如@berak 在评论中所述,我使用 Cygwin Ports 为 Cygwin 安装了 OpenCV 包。 (Without any src or requiring any build, simple install only). (没有任何 src 或需要任何构建,仅简单安装)。
Had to tweak my CMakeLists.txt a little but it compiled well.不得不稍微调整我的CMakeLists.txt但它编译得很好。

##### CMakeLists.txt ########
cmake_minimum_required(VERSION 2.8.4) 
PROJECT (test)
find_package(OpenCV REQUIRED )

set( NAME_SRC
    src/main.cpp    
    src/mytest.cpp    
)

set( NAME_HEADERS       
    include/mytest.hpp
)

set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
add_executable( test ${NAME_SRC} ${NAME_HEADERS} )
include_directories(/usr/include /usr/include/opencv2 /usr/include/opencv ${CMAKE_CURRENT_SOURCE_DIR}/include)
link_directories(/usr/lib ${CMAKE_BINARY_DIR}/bin)
target_link_libraries(test opencv_core opencv_highgui opencv_imgproc)

(Now, I have totally different trouble of runtime error : Gtk-WARNING **: cannot open display , but its another story of not having XServer on Cygwin) (现在,我遇到了完全不同的运行时错误问题: Gtk-WARNING **: cannot open display ,但这是另一个关于 Cygwin 上没有 XServer 的故事)

First take the following steps to build opencv using visual studio and cmake gui.首先按照以下步骤使用visual studio和cmake gui构建opencv。

  1. Clone into the opencv source code repo克隆到 opencv 源代码仓库

$ git clone https://github.com/Itseez/opencv.git

  1. Navigate inside the folder and checkout to the specified version在文件夹内导航并签出到指定的版本

$ git checkout 3.0.0

  1. Create a build directory and build opencv in there using CMake and visual studio.创建一个构建目录并使用 CMake 和 Visual Studio 在其中构建 opencv。 Note that by default opencv will build dynamic libraries unless you enable the option of building opencv static libraries.请注意,默认情况下 opencv 将构建动态库,除非您启用构建 opencv 静态库的选项。

  2. Once done you should handle the environment variables.完成后,您应该处理环境变量。 Create a variable named OpenCV_DIR and point it to the build directory of opencv.创建一个名为 OpenCV_DIR 的变量并将其指向 opencv 的构建目录。 Then update your PATH variable to have new opencv dll files added.然后更新您的 PATH 变量以添加新的 opencv dll文件。 Usually they can be found in the directory opencv\\build\\bin\\Release assuming you have built opencv in Release mode通常它们可以在目录opencv\\build\\bin\\Release假设您已经在Release模式下构建了 opencv

Then set up your project CMakeList.txt as follows然后如下设置你的项目CMakeList.txt

find_package(OpenCV  REQUIRED)  

include_directories(    
    ${OpenCV_INCLUDE_DIRS}     
)

target_link_libraries(test
    ${OpenCV_LIBS}
)

The command find_package(OpenCV REQUIRED) finds the opencv configuration files in the directory "opencv/build", aka the directory in which you have built opencv (That's why you should set the value of the variable ${OpenCV_DIR} to this directory so the command find_package can find the required files in there).命令find_package(OpenCV REQUIRED)在目录“opencv/build”中找到 opencv 配置文件,也就是你构建 opencv 的目录(这就是为什么你应该将变量${OpenCV_DIR}的值设置到这个目录,以便命令find_package可以在那里找到所需的文件)。

The two variables {OpenCV_INCLUDE_DIRS} and ${OpenCV_LIBS} have already been set in those configuration files.已经在这些配置文件中设置了两个变量{OpenCV_INCLUDE_DIRS}${OpenCV_LIBS} So you should be all set.所以你应该准备好了。

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

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