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

标签 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”文件,如您所见here。但是,我在github上找到的最旧版本的OpenCV拥有此文件,如您所见here

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

提前致谢。

最佳答案

CMake会告诉您该怎么做:

Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set "OpenCV_DIR" to a directory containing one of the above files.



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

他们说:

you may need to set OpenCV_DIR variable to the absolute path to the directory containing 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

关于c++ - CMake在编译使用OpenCV的项目时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58023262/

相关文章:

qt - 如何指示 cmake/automoc 查找外部 header

cmake - 为 cmake 指定库以从命令行链接到

c++ - 使用抽象类的子类专门化模板

python - OpenCV 中的图像扭曲

c++ - 我想删除背景(就像黑环)

c++ - OpenCV 从 Mat 访问元素

c++ - QtCreator 找不到cmake

c++ - C++中的Qsort困境

c++ - 带碰撞的 N 体模拟

c++ - 将 Flycapture2 图像转换为 float *