【发布时间】:2020-07-26 13:30:30
【问题描述】:
我正在尝试在项目中使用 opencv,但在“安装”它时遇到了问题。我已经提取了 opencv 文件并创建了一个小测试程序:
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
int main(int argc, char **argv){
cv::Mat im=cv::imread((argc==2)? argv[1]: "testing.jpg",1);
if (im.empty()){
std::cout << "Cannot open image." << std::endl;
} else {
cv::imshow("image",im);
cv::waitKey(0);
}
return 0;
}
为了编译程序,我使用了以下命令:
g++ -I"../../PortableGit/opt/opencv/build/include/" -L"../../PortableGit/opt/opencv/build/x64/vc15/lib" main.cpp -lopencv_core -lopencv_highgui -o main
我收到以下错误:
In file included from ../../PortableGit/opt/opencv/build/include/opencv2/core.hpp:3293:0,
from ../../PortableGit/opt/opencv/build/include/opencv2/highgui.hpp:46,
from ../../PortableGit/opt/opencv/build/include/opencv2/highgui/highgui.hpp:48,
from main.cpp:1:
../../PortableGit/opt/opencv/build/include/opencv2/core/utility.hpp:714:14: error: 'recursive_mutex' in namespace 'std' does not name
a type
typedef std::recursive_mutex Mutex;
^~~~~~~~~~~~~~~
../../PortableGit/opt/opencv/build/include/opencv2/core/utility.hpp:715:25: error: 'Mutex' is not a member of 'cv'
typedef std::lock_guard<cv::Mutex> AutoLock;
^~
../../PortableGit/opt/opencv/build/include/opencv2/core/utility.hpp:715:25: error: 'Mutex' is not a member of 'cv'
../../PortableGit/opt/opencv/build/include/opencv2/core/utility.hpp:715:34: error: template argument 1 is invalid
typedef std::lock_guard<cv::Mutex> AutoLock;
我相信这与 mingw 二进制文件不再包含在 opencv 中有关。我缺少opencv/build/x86/mingw 目录。
我的问题是:
- 如何在不安装某种 IDE 和/或 CMake 的情况下“安装”opencv 并使用它? (我更喜欢使用 vim 和命令行。)
- 安装后,我使用什么命令来编译和链接程序与 opencv?
感谢任何帮助。
编辑:
这似乎是 GCC 在 Windows 上实现 threads 的问题。使用 mingw-w64 代替 mingw 修复了 std::recursive_mutex 问题,但现在链接器找不到正确的文件。
/i686-w64-mingw32/bin/ld.exe: cannot find -lopencv_core
/i686-w64-mingw32/bin/ld.exe: cannot find -lopencv_highgui
【问题讨论】:
-
看来OpenCV至少需要C++11。尝试使用
-std=c++11标志进行编译(或将编译器升级到至少 GCC 6.1,默认启用 C++14) -
我有 GCC 版本 6.3.0-1,我尝试使用
-std=c++11,但没有任何改变。 -
这很奇怪,
std::recursive_mutex在 C++11 中可用。在 OpenCV 包含之前添加#include <mutex>可能会有所帮助(除非它真的只是环境问题),但是包含依赖项远非好。 -
在没有opencv的情况下创建了一个小测试程序,即使使用
#include <mutex>,std::recursive_mutex也无法识别。 -
@Yksisarvinen 当你说环境问题时,我应该注意什么?
标签: c++ windows opencv command-line