vscode中配置c++的opencv库
时间: 2023-04-26 14:01:02 浏览: 137
1. 首先需要安装好opencv库,可以参考官网的安装教程。
2. 打开VSCode,新建一个C++文件,然后按下Ctrl+Shift+P,输入“C++:Edit Configurations”,选择“C++(GDB/LLDB)”,然后在弹出的窗口中输入以下内容:
{
"name": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file"
}
3. 然后在VSCode的左侧边栏中选择“任务”,然后选择“配置任务”,然后选择“C/C++: g++ build active file”,然后在弹出的窗口中输入以下内容:
{
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-I/usr/local/include/opencv4",
"-L/usr/local/lib",
"-lopencv_core",
"-lopencv_highgui",
"-lopencv_imgproc",
"-lopencv_imgcodecs"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
4. 然后就可以在C++代码中使用opencv库了,例如:
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat img = imread("test.jpg");
imshow("test", img);
waitKey();
return ;
}
5. 最后按下F5即可运行程序。
阅读全文