安装opencv
brew install opencv
opencv示例代码 test.cpp
#include <iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; int main() { Mat srcImage = imread("../1.png"); imshow("Display Image window",srcImage); waitKey(0); return 0; }
vscode调试
vscode调试c++程序主要有三个文件:.vscode文件夹下的c_cpp_properties.json,launch.json,tasks.json
- c_cpp_properties.json最主要是指明 includePath,将opencv的路径放进去。
{ "configurations": [ { "name": "Mac", "includePath": [ "${workspaceFolder}/**", "/usr/local/include/opencv4", "/usr/local/Cellar/opencv/4.6.0/include/opencv4", "/usr/local/Cellar/opencv/4.6.0/lib/**" ], "defines": [], "macFrameworkPath": [ //"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks" ], "compilerPath": "/usr/bin/g++", "cStandard": "c11", "cppStandard": "c++11", "intelliSenseMode": "macos-clang-x64" } ], "version": 4 }
- launch.json就用默认生成的就可以。
{ "configurations": [ { "name": "C/C++: g++ 生成和调试活动文件", "type": "cppdbg", "request": "launch", "program": "${fileDirname}/${fileBasenameNoExtension}", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": false, "MIMode": "lldb", "preLaunchTask": "C/C++: g++ 生成活动文件" } ], "version": "2.0.0" }
- task.json主要是在args里声明c++的版本,以及pkg-config和要动态链接的库。
{ "tasks": [ { "type": "cppbuild", "label": "C/C++: g++ 生成活动文件", "command": "/usr/bin/g++", "args": [ "-fdiagnostics-color=always", "-std=c++11",//使用C++11 "-stdlib=libc++", "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}", "`pkg-config", "--libs", "--cflags", "opencv4`" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": "build", "detail": "调试器生成的任务。" }, ], "version": "2.0.0" }