CMake在编译使用OpenCV的项目时遇到问题

时间:2019-09-20 06:58:02

标签: c++ opencv cmake

我正在使用Linux Mint 19.1 Tessa。

我按照here中所述的说明安装了OpenCV。现在,我将其安装在目录“ / home / dell / opencv”。

我试图通过运行命令“ cmake”来运行位于“ / home / dell / opencv / samples / cpp / example_cmake /”的示例项目。在终端上,出现以下错误:

CMake Error at CMakeLists.txt:14 (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 "/home/dell/opencv/samples/cpp/example_cmake/CMakeFiles/CMakeOutput.log".

“ CMakeLists.txt”文件包含以下内容:

# cmake needs this line
cmake_minimum_required(VERSION 3.1)

# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

# Define project name
project(opencv_example_project)

# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)

# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS "    config: ${OpenCV_DIR}")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

# Declare the executable target built from your sources
add_executable(opencv_example example.cpp)

# Link your application with OpenCV libraries
target_link_libraries(opencv_example LINK_PRIVATE ${OpenCV_LIBS})

我已经在互联网上进行了很多搜索,这似乎是一个常见问题。但是,我仍然无法按照我发现的指示来解决它。

我在浏览OpenCV文件夹时注意到的一件事是,我拥有的版本(我认为它是最新版本)确实不包含任何“ OpenCVConfig.cmake”文件,如您所见{{ 3}}。但是,我在github上发现的最旧版本的OpenCV拥有此文件,如您所见here

那么,也许某些配置设置为此最早的版本,并且正在引起冲突?如何更改它并使它适用于最新版本?我认为解决起来一定很简单,但是我是新手。

谢谢。

2 个答案:

答案 0 :(得分:0)

CMake会为您写信:

  

将“ OpenCV”的安装前缀添加到CMAKE_PREFIX_PATH或进行设置    “ OpenCV_DIR”到包含上述文件之一的目录。

另一个选择-请再次读取您粘贴的文件CMakeLists.txt中的注释。

他们说:

  

您可能需要设置OpenCV_DIR变量   到包含OpenCVConfig.cmake的目录的绝对路径

另一个问题-源代码构建,通过将点传递给cmake来执行。最好为构建文件创建单独的目录。

通过这种方式构建,可以轻松地将构建目录添加到.gitignore,而不会用已编译的二进制文件污染源代码存储库。另外,如果出现问题,您可以删除该构建目录,然后从头开始。这比一一弄乱文件要容易得多。

因此,您需要像

一样调用CMake。
mkdir build
cd build 
OpenCV_DIR=/home/dell/opencv cmake ..

mkdir build
cd build
cmake .. -DCMAKE_PREFIX_PATH=/home/dell/opencv

或以下内容也可以解决问题

mkdir build
cd build
cmake .. -DOpenCV_DIR=/home/dell/opencv

答案 1 :(得分:0)

如下更新您的OpenCV搜索目录路径,

find_package(OpenCV 3.4 REQUIRED PATHS "/usr/local/opencv/" NO_DEFAULT_PATH)