vscode使用了mingw64,在使用官网下载的opencv后,发现有函数提示功能,但是在调试时出现了各种opencv库中的各种函数未定义的情况(如下图代码所示的类型),然后突然明白过来,既然使用了mingw,那么相对应的opencv也应该使用mingw版本,但是官网不提供该版本的下载,那就只能自己编译一个该版本出来。
C:\Eulerian Real-Time OpenCV build\Realtime-Video-Magnification-master\build-rvm-Desktop_Qt_5_9_0_MinGW_32bit-Debug\debug\main.o:-1: In function `ZN2cv6StringaSERKS0_':
E:\opencv\build\include\opencv2\core\cvstd.hpp:672: error: undefined reference to `cv::String::deallocate()'
C:\Eulerian Real-Time OpenCV build\Realtime-Video-Magnification-master\src\main\threads\CaptureThread.h:42: error: undefined reference to `cv::VideoCapture::~VideoCapture()'
注意:使用的工具版本如下,一定要注意版本问题,如果使用的mingw64版本和opencv版本不对应,那简直是一个天坑,将会在mingw64-make 的时候无限报错,这简直能折磨死人。
opencv-mingw
mingw64-4.8.1
opencv-3.4.1
cmake-3.10,0
ERROR: During startup program exited with code 0xc0000135.
{
"configurations": [
{
"name": "Win32",
"includePath": [
"D:/opencv-3.4.1/mingw-x64-install/install/include",
"${workspaceFolder}/**",
"D:/opencv-3.4.1/mingw-x64-install/install/include/opencv",
"D:/opencv-3.4.1/mingw-x64-install/install/include/opencv2"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "D:\\mingw64\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
请注意:关键代码为 "includePath"里的内容,这里需要将opencv里的头文件包含进来。
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
"type": "cppdbg", // 配置类型,这里只能为cppdbg
"request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
"program": "${workspaceRoot}/build/${fileBasenameNoExtension}.exe",// 将要进行调试的程序的路径
"args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
"stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false
"cwd": "${workspaceRoot}", // 调试程序时的工作目录,一般为${workspaceRoot}即代码所在目录
"environment": [],
"externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台
"MIMode": "gdb",
//"miDebuggerPath": "D:\\Mingw\\bin\\gdb.exe",
"miDebuggerPath": "D:\\mingw64\\bin\\gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应
"preLaunchTask": "g++", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
请注意:此处的关键代码为"miDebuggerPath"里的语句,里面包含gdb.exe的目录地址; "program"里为调试程序的路径,之所以放到build里,是因为如果包含了动态链接库,那么动态链接库需要拷贝到和编译的.exe文件相同目录下,和tasks.json文件里的args参数相对应。
{
"version": "2.0.0",
"command": "g++",
"args": ["-g","${file}","-o","build/${fileBasenameNoExtension}.exe",
"-I", "D:/opencv-3.4.1/mingw-x64-install/install/include",
"-I", "D:/opencv-3.4.1/mingw-x64-install/install/include/opencv",
"-I", "D:/opencv-3.4.1/mingw-x64-install/install/include/opencv2",
"-L", "D:/opencv-3.4.1/mingw-x64-install/install/x64/mingw/bin/lib*",
],
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceRoot}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
注意:这里的关键代码是args里的参数,这里“-g”定义了编译出的.exe文件的存放目录(和c_cpp_properties文件相对应),“-I”里的参数和c_cpp_properties文件相对应,“-L”里定义了所编译出的opencv里的库文件的包含目录(需要拷贝到和.exe相同目录)。
workspace
.vscode
c_cpp_properties.json
launch.json
tasks.json
include
(所包含的头文件等,建议,也可以直接将头文件放在src)
build
(各种库文件等依赖的内容)
src
(源码)