Win10 VS Code 项目开发opencv C++ 初学踩坑
程序员文章站
2024-02-24 12:49:28
...
VS Code环境中的C/C++ && OpenCV开发
背景:
用VS打开《计算机视觉算法 -基于OpenCV的计算机应用开发》附带C/C++源文件时,会对头文件进行内容提示:大意是无法找到头文件:includePath。
#include // 标准C++头文件
#include <opencv2/opencv.hpp> //OpenCV头文件
安装VScode搭配c/c++环境出现“#include errors detected. Please update your includePath…”问题的解决
解决上述问题方法:
- 安装C/C++工具包,配置VS Code;可以参考网页:添加链接描述
这里面有很多需要添加的扩展,对于编辑十分方便。其主要目的是可以在工程目录中建立“.vscode/c_cpp_properties.json”然后对相关内容进行补充。
特别说明,不需要在C/C++ intelligence (大概名字吧)的配置采用WinGW作为编译器,联网后VS自动扫描VS studio。 - 创建并建立相关文件,参考添加链接描述
task.json, launch.json, c_cpp_properties.json,这三个文件都需要存在。 - 实例
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
cout << cv::getBuildInformation();
return 0;
}
- task.json文件
{
"version": "2.0.0",
"presentation": {
"reveal": "always",
"panel": "new",
"echo": true
},
"options": {
"env": {
"LINUX_SRC_HOME": "/home/user/system/packages/services/Car/evs",
"LOCAL_SRC_HOME": "${workspaceRoot}"
}
},
"type": "shell",
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": ".*(app/.*|project/.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
},
"tasks": [
{
"label": "g++",
"type": "shell",
"command": "g++",
"args": ["-g", "${file}", "-o", "${file}.o",
"-I","D:/opencv/build/include",
"-I","D:/opencv/build/include/opencv2",
"-L", "D:/opencv/build/x64/v15/lib",
"-l", "opencv_world411d"
],
"identifier": "CodeSync",
"taskClassify": "同步代码"
}
]
}
- 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": "(Windows) 启动",
"type": "cppvsdbg",
"request": "launch",
"program": "输入程序名称,例如 ${workspaceFolder}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true
}
]
}
- c_cpp_properties.json文件
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"D:/opencv/build/include",
"D:/opencv/build/include/opencv2",
"D:/opencv/build/x64/vc15/lib" // 采用WinPack 安装OpenCV 4.1 在D:盘目录下
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"intelliSenseMode": "${default}"
}
],
"version": 4
}
希望以上内容对初学者有所帮助。交流意见