配置opencv环境踩过的坑

众所周知,配环境是一个玄学问题,有问题的时候到处都是问题,在配置opencv 的时候再次感受到这感受,记录一下自己踩的坑,让大家少走些弯路(

首先给大家推荐一个教程,写得还是很清楚的,这个是网址,不过他也有一些需要注意的地方没有提到,所以这里把步骤简单列举,然后再把需要注意的地方提出来供大家参考。

不清楚版本号的具体影响,就尝试了3.9和3.12的cmake以及opencv的诸多版本,进行排列组合尝试,而且这个编译一次就要可能花费一个小时左右,并且还好多次给我卡在90%左右,弹出让人心肺骤停的进度条和报错信息···

步骤简介:

  1. 下载并安装MinGW_posix版本;
  2. 下载并安装cmake,注意要安装至C盘;
  3. 下载并安装opencv,注意不要安装至C盘;
  4. 添加上述三个文件对应的环境变量;
  5. cmake-gui程序中,配置路径并进行Configuration,然后进行Generate;
  6. opencv的安装路径下新建的opencv\build\x64\MinGW 文件夹下进入终端并且输入minGW32-make 进行编译,耗时约一个小时,完成后输入minGW32-make install 来装载;

以上步骤写得非常简略,开头的网址里更详细一些,下面来说下遇到的两个问题吧。

python2.7的调用问题

首先是第一步Finish之后就出现的问题

find_host_package(PythonInterp 2.7)' found wrong Python version,具体提示如下:

Found PythonInterp: C:/Users/LiuBing/AppData/Local/Programs/Python/Python38/python.exe (found suitable version "3.8.2", minimum required is "2.7")

CMake Warning at cmake/OpenCVDetectPython.cmake:81 (message):

  CMake's 'find_host_package(PythonInterp 2.7)' found wrong Python version:

 

  PYTHON_EXECUTABLE=C:/Users/LiuBing/AppData/Local/Programs/Python/Python38/python.exe

  PYTHON_VERSION_STRING=3.8.2

  Consider providing the 'PYTHON2_EXECUTABLE' variable via CMake command line

  or environment variables

Call Stack (most recent call first):

  cmake/OpenCVDetectPython.cmake:271 (find_python)

  CMakeLists.txt:598 (include)

并且这个问题我之前还没注意,因为还是可以之间按照步骤操作的就没管,后来发现了就装了python2.7,甚至之后还有python1.4,但是问题依然存在,殊不知在报错信息里就已经有了解决方法,所以说,多去阅读官方文档和第一手资料以及报错信息真的很有用,不能上来就搜索问题(虽然已经条件反射了)。

方法就是在环境变量里添加python2.7的路径,具体操作如下:

  1. 官方网址下载2.7版本的python;
  2. 打开设置的“系统”到“系统信息”到“高级系统设置”,选择“环境变量”,在系统变量里新建一个变量名为PYTHON2_EXECUTABLE,变量值为你下载的2.7版本的exe文件的位置,如E:\Python2.7\python.exe

之后编译的时候就可以找到文件了,并且生成make文件

MinGW的版本问题

然后在运行minGW32-make命令之后,出现这样一大堆的报错,进度卡在90%,排列组合很多次全都是一样的情况,心态很崩(

E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:175:14: error: 'thread' in namespace 'std' does not name a type
         std::thread second_workthread;
              ^~~~~~
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:175:9: note: 'std::thread' is defined in header '<thread>'; did you forget to '#include <thread>'?
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:82:1:
+#include <thread>

E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:175:9:
         std::thread second_workthread;
         ^~~
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:176:14: error: 'mutex' in namespace 'st
' does not name a type
         std::mutex mtx;
              ^~~~~
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:176:9: note: 'std::mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:82:1:
+#include <mutex>

E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:176:9:
         std::mutex mtx;
         ^~~
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:177:14: error: 'condition_variable' in namespace 'std' does not name a type
         std::condition_variable objectDetectorRun;
              ^~~~~~~~~~~~~~~~~~
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:177:9: note: 'std::condition_variable' is defined in header '<condition_variable>'; did you forget to '#include <condition_variable>'?
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:82:1:
+#include <condition_variable>

E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:177:9:
         std::condition_variable objectDetectorRun;
         ^~~
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:178:14: error: 'condition_variable' in namespace 'std' does not name a type
         std::condition_variable objectDetectorThreadStartStop;
              ^~~~~~~~~~~~~~~~~~
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:178:9: note: 'std::condition_variable' is defined in header '<condition_variable>'; did you forget to '#include <condition_variable>'?
         std::condition_variable objectDetectorThreadStartStop;
         ^~~
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp: In member function 'void cv::DetectionBasedTracker::SeparateDetectionWork::setParameters(const cv::DetectionBasedTracker::Parameters&)':
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:143:35: error: 'mutex' is not a member of 'std'
             std::unique_lock<std::mutex> mtx_lock(mtx);
                                   ^~~~~
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:143:35: note: 'std::mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:143:35: error: 'mutex' is not a member of 'std'
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:143:35: note: 'std::mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:143:40: error: template argument 1 is invalid
             std::unique_lock<std::mutex> mtx_lock(mtx);
                                        ^
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:143:51: error: 'mtx' was not declared in this scope
             std::unique_lock<std::mutex> mtx_lock(mtx);
                                                   ^~~
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:143:42: warning: unused variable 'mtx_lock' [-Wunused-variable]
             std::unique_lock<std::mutex> mtx_lock(mtx);
                                          ^~~~~~~~
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp: In member function 'void cv::DetectionBasedTracker::SeparateDetectionWork::init()':
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:156:35: error: 'mutex' is not a member of 'std'
             std::unique_lock<std::mutex> mtx_lock(mtx);
                                   ^~~~~
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:156:35: note: 'std::mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:156:35: error: 'mutex' is not a member of 'std'
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:156:35: note: 'std::mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:156:40: error: template argument 1 is invalid
             std::unique_lock<std::mutex> mtx_lock(mtx);
                                        ^
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:156:51: error: 'mtx' was not declared in this scope
             std::unique_lock<std::mutex> mtx_lock(mtx);
                                                   ^~~
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:164:13: error: 'objectDetectorThreadStartStop' was not declared in this scope
             objectDetectorThreadStartStop.notify_one();
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:164:13: note: suggested alternative: 'timeWhenDetectingThreadStartedWork'
             objectDetectorThreadStartStop.notify_one();
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
             timeWhenDetectingThreadStartedWork
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:156:42: warning: unused variable 'mtx_lock' [-Wunused-variable]
             std::unique_lock<std::mutex> mtx_lock(mtx);
                                          ^~~~~~~~
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp: In destructor 'virtual cv::DetectionBasedTracker::SeparateDetectionWork::~SeparateDetectionWork()':
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:254:5: error: 'second_workthread' was not declared in this scope
     second_workthread.join();
     ^~~~~~~~~~~~~~~~~
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp: In member function 'bool cv::DetectionBasedTracker::SeparateDetectionWork::run()':
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:261:27: error: 'mutex' is not a member of 'std'
     std::unique_lock<std::mutex> mtx_lock(mtx);
                           ^~~~~
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:261:27: note: 'std::mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:261:27: error: 'mutex' is not a member of 'std'
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:261:27: note: 'std::mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
E:\opencv3.4.5\opencv\sources\modules\objdetect\src\detection_based_tracker.cpp:261:32: error: template argument 1 is invalid
     std::unique_lock<std::mutex> mtx_lock(mtx);

可以看到报错里有很多关于某些头文件的问题,不清楚能否之间像里面说的那样添加头文件之后就可以执行,但在查找问题的时候发现出现这样error: ‘mutex’ in namespace ‘std’ does not name a type 的类似错误可能是mingw版本选择问题,之前装过了相关的版本,所以便没有在意这种事情,需要选择 -posix版本的mingw,而SJLJ 还是 SEH 的选择,似乎是说SJLJ 的兼容性会更好,但是SEH的速度会更快,所以保险起见选择SJLJ的版本。

不过如果你是按照开头提到的那个教程一步步来下载各个文件的话就不会踩到我这个坑了。

最后看着它终于冲破了90%的感觉就好像是获得了封号斗罗称号一样,然后install一泻千里的感觉非常爽(bushi

之后就是在vscode内部的配置了,需要有三个配置文件,分别列在下方

tasks.json

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "command": "g++",
  "args": [
      "-g", 
      "-std=c++11", 
      "${file}", 
      "-o", 
      "${fileBasenameNoExtension}.o",  
      "-I", "E:\\opencv3.4.5\\opencv\\build\\include",
      "-I", "E:\\opencv3.4.5\\opencv\\build\\include\\opencv2",
      "-I", "E:\\opencv3.4.5\\opencv\\build\\include\\opencv",
      "-L", "E:\\opencv3.4.5\\opencv\\build\\x64\\MinGW\\lib",
      "-l", "opencv_core345",
      "-l", "opencv_imgproc345",
      "-l", "opencv_imgcodecs345",
      "-l", "opencv_video345",
      "-l", "opencv_ml345",
      "-l", "opencv_highgui345",
      "-l", "opencv_objdetect345",
      "-l", "opencv_flann345",
      "-l", "opencv_imgcodecs345",
      "-l", "opencv_photo345",
      "-l", "opencv_videoio345"

  ],// 编译命令参数
  "problemMatcher":{
      "owner": "cpp",
      "fileLocation":[
          "relative",
          "${workspaceFolder}"
      ],
      "pattern":[
          {
              "regexp": "^([^\\\\s].*)\\\\((\\\\d+,\\\\d+)\\\\):\\\\s*(.*)$",
              "file": 1,
              "location": 2,
              "message": 3
          }
      ]
  },
  "group": {
      "kind": "build",
      "isDefault": true
  }
}

lauch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.o",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "E:/MinGW64/mingw64_posix/mingw64/bin/gdb.exe",
            "preLaunchTask": "g++",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        },
    ]
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "win",
            "includePath": [
                "${workspaceFolder}/**",
                "E:\\opencv3.4.5\\opencv\\build\\include",
                "E:\\opencv3.4.5\\opencv\\build\\include\\opencv2",
                "E:\\opencv3.4.5\\opencv\\build\\include\\opencv"                
            ],
            "defines": [],
            "compilerPath": "E:/MinGW64/mingw64_posix/mingw64/bin/gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

注意都需要将对应的路径更改为自己下载安装的相应路径!

MinGW版本顺序问题

这时候又遇到了一个问题,就是在debug的时候依然报编译器的错误,如ERROR: Unable to start debugging. GDB exited unexpectedly.

经过搜索排查之后发现是环境变量里的选择顺序问题,锅还是因为MinGW之前已经配置过了导致的问题,之前下载了不少编译器。有几种解决方法,可以参考这个链接里的。

我选择的解决方法就是直接在环境变量里把posiv 版本的MinGW移到最上面,然后重启之后就可以解决问题了。

还有其他的解决方法如将相对应编译器的libstdc++-6.dll 文件放到程序所在的文件夹当中,或者放到C:\\Windows\\System32 下。

其实总结一下,配置环境的时候需要很注意过程当中的编译信息,然后才能排查问题精确定位,在网上搜的情况太多了,而且出问题的地方很多,不仅需要检索查找的能力,还需要有一些自主排查的能力;

还有就是在过程当中有很多重复操作,总想着有没有可能下一次会有奇迹或者不一样的事情发生,但是电脑是呆子,做一万次都是一样

posted @   hhr346  阅读(1339)  评论(2编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示