Ubuntu系統(tǒng)下如何在VScode配置OpenCV(C++)環(huán)境(.json文件)
更新時間:2025年02月12日 08:42:03 作者:forever0007
這篇文章主要介紹了如何在VSCode中配置和運行C++程序,包括創(chuàng)建test.cpp文件、配置launch.json、tasks.json和c_cpp_properties.json文件,以及重啟VSCode以解決可能的報錯問題,需要的朋友可以參考下
創(chuàng)建一個test文件,用VScode打開

(1)按住ctrl+shift+P搜索launch.json,點擊打開即可
(2)按住ctrl+shift+P 打開第一個

(1)、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": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}", //程序文件路徑
"args": [], //程序運行需傳入的參數(shù)
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false, //運行時是否顯示控制臺窗口
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}(2)、tasks.json文件
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file", /* 與launch.json文件里的preLaunchTask的內(nèi)容保持一致 */
"command": "/usr/bin/g++",
"args": [
"-std=c++11",
"-g",
//"${file}", /* 編譯單個文件 */
"${fileDirname}/*.cpp", /* 編譯多個文件 */
"-o",
"${fileDirname}/${fileBasenameNoExtension}", /* 輸出文件路徑 */
/* 項目所需的頭文件路徑 */
"-I",
"${workspaceFolder}/",
"-I",
"/usr/local/include/",
"-I",
"/usr/local/include/opencv4/",
"-I",
"/usr/local/include/opencv4/opencv2",
/* 項目所需的庫文件路徑 */
"-L",
"/usr/local/lib",
/* OpenCV的lib庫 */
"/usr/local/lib/libopencv_*"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}(3)、c_cpp_properties.json文件
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/include/opencv4",
"/usr/include/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu11",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64",
"configurationProvider": "ms-vscode.makefile-tools"
}
],
"version": 4
}(4)、測試
創(chuàng)建一個test.cpp文件,輸入測試代碼
#include <opencv2/opencv.hpp>
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
string Path = "test.jpg";//此處為的圖片路徑
//從文件中讀入圖像
cv::Mat img = cv::imread(Path, 1);
//如果讀入圖像失敗
if (img.empty()) {
cout<< "Not Found image"<<endl;
return -1;
}
cv::imshow("image", img); //顯示圖像
cv::waitKey();
return 0;
}
圖片顯示成功,配置完成?。ㄈ舫晒\行仍有報錯顯示,重啟VScode即可)

總結(jié)
到此這篇關(guān)于Ubuntu系統(tǒng)下如何在VScode配置OpenCV(C++)環(huán)境(.json文件)的文章就介紹到這了,更多相關(guān)Ubuntu VScode配置OpenCV環(huán)境內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C/C++ Qt 數(shù)據(jù)庫與Chart歷史數(shù)據(jù)展示
這篇文章主要介紹了Qt利用Qchart組件展示數(shù)據(jù)庫中的歷史數(shù)據(jù)。文中的示例代碼講解清晰,具有一定的學(xué)習(xí)和工作價值,感興趣的小伙伴可以學(xué)習(xí)一下2021-12-12
Visual Studio 2022 上使用ffmpeg的詳細步驟
文章介紹了在開發(fā)項目中配置FFmpeg庫的步驟,添加包含目錄、庫目錄、依賴項及動態(tài)庫路徑,確保編譯器和鏈接器正確識別FFmpeg資源,最后通過測試驗證配置有效性,感興趣的朋友一起看看吧2025-07-07

