簡體   English   中英

無法用 OpenCV 編譯 c++ 程序。 OpenCVConfig.cmake 文件丟失

[英]Can't compile c++ program with OpenCV. OpenCVConfig.cmake file missing

試圖從 CLion 和終端運行我的簡單 cpp 程序,但到處都有同樣的問題:

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. 

嘗試了一些解決方案,但沒有任何幫助。 OpenCV 安裝在 macOS 上的用戶目錄中。 有人可以幫助我嗎?

當我嘗試運行在其他系統上編寫的程序時,Windows中的CLion存在類似的問題。 為了解決該問題,我不得不“從源代碼導入項目”,以便CLion生成Make所需的所有文件。

我通過導入后彈出的對話框導入了現有程序。

CLion啟動窗口

這是我以CLion要求的方式導入存儲庫之前的簡單GCD c ++終端程序:

GCD項目文件

選擇導入后,CLion生成了在本地編譯所需的所有文件:

我的新項目文件架構

我希望這對我有幫助,一旦導入程序,CLion便會執行其他所有操作。 我修改了c ++ 11的CMakeList.txt文件,因為我需要知道我的程序可以按預期在11上編譯。CLion對我而言默認為c ++ 14。

該錯誤消息清楚地指出了解決該問題的方法:首先,找到上述文件之一( OpenCVConfig.cmakeopencv-config.cmake )在您系統上的位置。 該路徑將是/path/to/configfile 然后相應地設置OpenCV_DIR變量。

可以通過兩種方式設置變量:

  • 命令行: cmake -DOpenCV_DIR=</path/to/configfile> <any additional arguments>
  • CLion設置:請參見下面的圖像(取自上一篇文章 )。 但是, -DENABLE_TESTS=ON可以將-DOpenCV_DIR=</path/to/configfile>到CMake選項:

在此處輸入圖片說明

在此處輸入圖片說明

正如錯誤消息所說:

  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.

因此,查找文件OpenCVConfig.cmake並在您的CMakeLists.txt文件中將變量OpenCV_DIR設置為包含OpenCVConfig.cmake文件的目錄。

就我而言,我從源代碼安裝了 opencv。 我安裝opencv的目錄結構如下

opencv-installation-directory/
  - bin/
  - include/
  - lib/
  - share/
    - OpenCV/
      - OpenCVConfig.cmake

所以我CMakeLists.txt下行添加到CMakeLists.txt

set(OpenCV_DIR opencv-installation-directory/share/OpenCV)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM