windows下使用Clion進行OpenCV開發環境搭建及排錯指南

1. 前言

鑑於VS自己體積的龐大和項目依賴管理方式的不便,因此本文采用Clion經過CMake進行項目結構管理。php

2. 準備工做

  • Windows 7 + (本文環境爲 win7 x64)
  • Clion
  • MinGW(本文環境 tdm-gcc-5.1.0-3)
  • CMake (本文環境 cmake-3.7.2)
  • OpenCV(本文環境 opencv-3.4.1)

安裝以上軟件,基本都是一路 next ,最後作一下系統環境變量的配置(右擊我點電腦->屬性->高級系統設置->環境變量->系統變量->編輯Path,添加對應軟件bin所在目錄,中間分號分隔)。最後打開Clion,依次進入Setting->Build,Execution,Deployment->Toolchains,設置編譯用工具鏈:html

工具鏈設置

ps:編譯好的OpenCV MSVC版及 MinGW版ios

  • MSVC版c++

    下載的 OpenCV 文件夾會有:git

    • build (已編譯好的庫)
    • sources (源碼)
使用 MSVC 的話,直接在

    build/x64/vc14

裏面就有了,配置好路徑便可使用
  • MinGW版

    OpenCV 沒有爲咱們編譯好 MinGW 版,因此只能用戶自行編譯,下面就是介紹 MinGW 版的編譯流程以及中間可能遇到的錯誤的排除github

    這裏有須要的話能夠直接下載使用我編譯好了的 OpenCV-3.4.1 x64windows

3. 編譯

  1. 解壓OpenCV,而後在解壓後source同級目錄下建立一個新的mingw-build文件夾用於存放編譯後的內容。

    (ps:這裏若是你以前安裝了Anaconda或者Python,請把這兩個軟件暫時從環境變量中刪除,只是刪除系統Path中的存在便可,不是卸載軟件。緣由是後面編譯會有衝突)bash

  2. 而後這裏選擇用cmake圖形化界面操做,比較直觀一些,採用命令行注意下命令參數同理。source和binary目錄按以下圖選擇:

    cmake

    其中:source目錄對應你的opencv/sources目錄,binary目錄就是以前新建的那個mingw-build存放目錄多線程

    此處須要複製opencv->build->bin下的兩個文件:opencv_ffmpegxxx.dll、opencv_ffmpegxxx_64.dll到opencv/sources/3rdparty/ffmpeg/目錄下ide

  3. 選好目錄,點擊configure選擇選擇MinGW Makefiles,如上圖所示。

注意:碰見紅色以後再次點擊Configure(等因而要點兩次),等到全部列表變白,沒有紅色一片的時候才表示成功

  1. configure成功後點擊generate
  2. 進入剛纔建立的mingw-build目錄,直接右鍵Git bash here(若是沒裝git windows,直接在cmd下進行),而後使用make命令進行編譯:

    mingw32-make -j2 # 以2線程進行編譯,這個數字根據本身硬件合理選擇

    make_install

  3. 漫長的make到達100%後進行install:

    mingw32-make install

  4. 添加環境變量

    • 爲系統變量 Path 添加 C:opencvmingw-buildinstallx64mingwbin
    • 添加 OpenCV_DIR (注意大小寫,部分狀況這個能夠不加,不過我這裏不加Cmake會報找不到OpenCV的錯誤),變量值爲以前編譯的build路徑:C:opencvmingw-build (實質是告知Cmake 包含OpenCV庫文件和include文件配置文件的OpenCVConfig.cmake所在)

opencv_dir

4. 調用

至此,OpenCV編譯和相關設置已經完成,只須要正常調用便可使用。下面給一個簡單攝像頭調用示例:

CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
project(detect)

set(CMAKE_CXX_STANDARD 11)

add_executable(detect main.cpp)

FIND_PACKAGE(OpenCV REQUIRED)
IF (OpenCV_FOUND)
    INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})
    TARGET_LINK_LIBRARIES(detect ${OpenCV_LIBS})
ELSE (OpenCV_FOUND)
    MESSAGE(FATAL_ERROR "OpenCV library not found")
ENDIF (OpenCV_FOUND)

main.cpp

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

int main() {
cv::VideoCapture capture(0);

if (!capture.isOpened()) {
    std::cout << "open camera error!" << std::endl;
    return -1;
}

cv::Mat frame;
while (1) {
    capture >> frame;
    if (frame.empty()) {
        std::cout << "capture empty frame" << std::endl;
        continue;
    }

    cv::Mat shrink_frame;
    cv::resize(frame, shrink_frame,
               cv::Size(frame.cols / 2, frame.rows / 2),
               0, 0, 3);

    cv::imshow("detect", shrink_frame);

    int key = cv::waitKey(1);
    if (key == 'q') {
        break;
    }
}
return 0;
}

detect

5. 編譯常見錯誤排除

  • 多線程編譯錯誤信息不明確

    表現:

    若是使用了多線程編譯,致使錯誤,可是錯誤信息不明確,如:

    Makefile:161: recipe for target 'all' failed
     mingw32-make: *** [all] Error 2

    多線程錯誤

    解決:

    使用單線程make,以查看詳細的錯誤提示,再根據具體狀況解決

    mingw32-make
  • RC 錯誤

    表現:

    ... windres.exe: unknown option -- W ...

    或者:

    FORMAT is one of rc, res, or coff, and is deduced from the file name
     extension if not specified.  A single file name is an input file.
     No input-file is stdin, default rc.  No output-file is stdout, default rc.

    RC錯誤

    解決:

    在 cmake-gui 編譯配置中:不勾選 ENABLE PRECOMPILED HEADERS,而後從新Configure-Generate-mingw32-make

  • sprintf instead use StringCbPrintfA or StringCchPrintfA 錯誤

    表現:

    ...opencv/sources/modules/videoio/src/cap_dshow.cpp...
     ... 'sprintf_instead_use_StringCbPrintfA_or_StringCchPrintfA' was not declared in this scope ...

    sprintf錯誤

    解決:

    修改E:OpenCV_3.3.1opencvsourcesmodulesvideoiosrccap_dshow.cpp文件,在#include "DShow.h"這行的上面加一行#define NO_DSHOW_STRSAFE,如:

    #define NO_DSHOW_STRSAFE
     #include "DShow.h"

    而後從新Configure-Generate-mingw32-make

  • identifier ‘nullptr’ is a keyword in C++11 錯誤

    表現:

    D:\opencv-3.4.1\opencv-3.4.1\3rdparty\protobuf\src\google\protobuf\stubs\io_win32.cc:94:3: warning: identifier 'nullptr' is a keyword in C++11 [-Wc++0x-compat]
     return s == nullptr || *s == 0;
    
     D:\opencv-3.4.1\opencv-3.4.1\3rdparty\protobuf\src\google\protobuf\stubs\io_win32.cc: In function 'bool google::protobuf::internal::win32::{anonymous}::null_or_empty(const char_type*)':
     D:\opencv-3.4.1\opencv-3.4.1\3rdparty\protobuf\src\google\protobuf\stubs\io_win32.cc:94:15: error: 'nullptr' was not declared in this scope
        return s == nullptr || *s == 0;
            ^
     3rdparty\protobuf\CMakeFiles\libprotobuf.dir\build.make:412: recipe for target '3rdparty/protobuf/CMakeFiles/libprotobuf.dir/src/google/protobuf/stubs/io_win32.cc.obj' failed
     mingw32-make[2]: *** [3rdparty/protobuf/CMakeFiles/libprotobuf.dir/src/google/protobuf/stubs/io_win32.cc.obj] Error 1
     CMakeFiles\Makefile2:710: recipe for target '3rdparty/protobuf/CMakeFiles/libprotobuf.dir/all' failed
     mingw32-make[1]: *** [3rdparty/protobuf/CMakeFiles/libprotobuf.dir/all] Error 2
     Makefile:161: recipe for target 'all' failed
     mingw32-make: *** [all] Error 2

    KEY錯誤

    解決:

    在 cmake-gui 編譯配置中:

    勾選 ENABLE_CXX11

    而後從新Configure-Generate-mingw32-make

  • dnn build error (Assembler messages)

    表現:

    C:\Users\ADMINI~1\AppData\Local\Temp\ccHIQxbw.s: Assembler messages:
     C:\Users\ADMINI~1\AppData\Local\Temp\ccHIQxbw.s:21800: Error: invalid register for .seh_savexmm
     ...
     modules\dnn\CMakeFiles\opencv_dnn.dir\build.make:1741: recipe for target 'modules/dnn/CMakeFiles/opencv_dnn.dir/layers/layers_common.avx512_skx.cpp.obj' failed
     mingw32-make[2]: *** [modules/dnn/CMakeFiles/opencv_dnn.dir/layers/layers_common.avx512_skx.cpp.obj] Error 1
     CMakeFiles\Makefile2:4124: recipe for target 'modules/dnn/CMakeFiles/opencv_dnn.dir/all' failed
     mingw32-make[1]: *** [modules/dnn/CMakeFiles/opencv_dnn.dir/all] Error 2
     Makefile:159: recipe for target 'all' failed
     mingw32-make: *** [all] Error 2

    dnn error

    解決:

    在 cmake-gui 編譯配置中:

    DCPU_DISPATCH 選空白

    而後從新Configure-Generate-mingw32-make

    (ps:這個錯誤在最新的OpenCV master分支已修復)

ref:

dnn build error via Mingw64

OpenCV使用CMake和MinGW的編譯安裝

相關文章
相關標籤/搜索