准备
- 下载Opencv源码
- 下载安装CMake
- 下载安装MinGW64
以上可参考上一篇
CMake Configure
- 在CMake的Configure时候会下载ffmepg,网络不好的话会超时。这时可以自己手动下载ffmeg放到OpenCV的编译目标目录TARGET\3rdparty\ffmpeg;下载的版本信息在OpenCV的SOURCE\3rdparty\ffmpeg\ffmpeg.cmake文件里;
- [上一篇]链接里的CMAKE设置项不需额外设置了,默认就行;
- Cmake第一次configure的时候选MinGw-Makefile, specify native compilers.
- 手动添加一项-DOPENCV_ALLOCATOR_STATS_COUNTER_TYPE=int64_t,否则会出现链接里一样的错误。添加方法:CMake的AddEntry,填OPENCV_ALLOCATOR_STATS_COUNTER_TYPE, 值填int64_t。
配置Vs Code
- 添加opencv install 的bin到环境变量;这里忘添加会编译出来无法运行;
- 修改vscode 的三个json文件, 添加include文件路径和库路径;
- c_cpp_properties.json
{
"configurations": [
{
"name": "MinGW",
"includePath": [
"${workspaceFolder}/**",
"C:/mingw64/x86_64-w64-mingw32/include/**",
"C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/**",
"D:/software/opencv-4.3.0-build/install/include/**" // 添加include路径
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"intelliSenseMode": "gcc-x64",
"cStandard": "c11",
"cppStandard": "c++11",
"compilerPath": "C:\\mingw64\\bin\\g++.exe",
"browse": {
"path": [
"${workspaceFolder}/**",
"C:/mingw64/x86_64-w64-mingw32/include/**",
"C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/**",
"D:/software/opencv-4.3.0-build/install/include" // 还有这里
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 4
}
- tasks.json
{
"tasks": [
{
"type": "shell",
"label": "g++", // 任务名称
"command": "C:\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o", "${fileDirname}\\${fileBasenameNoExtension}.exe", // .exe 还是.o 甚至.xxx都没影响。 但.exe可以双击直接运行,其他只能通过VSCode里运行(估计还是通过命令行运行)
"-I", "D:/software/opencv-4.3.0-build/install/include", // -I 是头文件路径
"-L", "D:/software/opencv-4.3.0-build/install/x64/mingw/lib", // -L 库文件路径。发现这里最后的文件夹是/lib还是/bin没区别。都可以运行。(/lib里面是xxx.dll.a, /bin里面是xxx.dll)
"-l", "opencv_core430", // -l 库文件名称,不要前缀lib和后缀.dll或.dll.a
"-l", "opencv_highgui430",
"-l", "opencv_imgproc430",
"-l", "opencv_imgcodecs430", // imread函数要用
],
"options": {
"cwd": "C:\\mingw64\\bin"
}
}
],
"version": "2.0.0"
}
- launch.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": "C++ Launch (GDB)",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe", // 后缀与上一个保持一致
"args": [],
"targetArchitecture": "x86_64",
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++" // 运行tasks.json里的g++任务
}
]
}
附
VS code中预定义路径相关变量
${workspaceFolder} - the path of the folder opened in VS Code
${workspaceFolderBasename} - the name of the folder opened in VS Code without any slashes (/)
${file} - the current opened file
${relativeFile} - the current opened file relative to workspaceFolder
${relativeFileDirname} - the current opened file’s dirname relative to workspaceFolder
${fileBasename} - the current opened file’s basename
${fileBasenameNoExtension} - the current opened file’s basename with no file extension
${fileDirname} - the current opened file’s dirname
${fileExtname} - the current opened file’s extension
${cwd} - the task runner’s current working directory on startup
${lineNumber} - the current selected line number in the active file
${selectedText} - the current selected text in the active file
${execPath} - the path to the running VS Code executable
更多预定义变量参考官方