ubuntu vscode配置c++ include路径
你可以通过在 VS Code 中打开 C 项目,然后在工作区文件夹中创建一个名为 ".vscode" 的文件夹,然后在该文件夹中创建一个名为 "c_cpp_properties.json" 的文件。在该文件中,你可以添加以下代码来配置 C 的 include 路径:
{ "configurations": [ { "name": "Linux", "includePath": [ "${workspaceFolder}/**", "/usr/include", "/usr/local/include" ], "defines": [], "compilerPath": "/usr/bin/gcc", "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "gcc-x64" } ], "version": 4 }
请注意,你需要根据你的实际情况修改 includePath 中的路径。
ubuntu vscode配置c++opencv环境
在Ubuntu中使用VScode配置C++和OpenCV环境的步骤如下:
首先,安装OpenCV。可以参考引用中提供的链接,按照最新最详细的教程进行安装。
安装VScode。在Ubuntu软件中心或者通过终端使用以下命令安装:
sudo snap install --classic code
打开VScode,并安装C++插件。点击左侧菜单栏的扩展图标,搜索并安装"C++"插件。
配置编译器路径和标准。创建一个名为
.vscode
的文件夹,并在其中创建一个名为c_cpp_properties.json
的文件。将以下内容复制到文件中,并保存:{ "configurations": [ { "name": "Linux", "includePath": [ "${workspaceFolder}/**", "/usr/include/x86_64-linux-gnu/sys", "/usr/local/include/opencv4" ], "defines": [], "compilerPath": "/usr/bin/gcc", "cStandard": "gnu17", "cppStandard": "gnu17", "intelliSenseMode": "linux-gcc-x64" } ], "version": 4 }
配置构建任务。创建一个名为
tasks.json
的文件,并将以下内容复制到文件中,并保存:{ "tasks": [ { "type": "cppbuild", "label": "C/C++: g++ build active file", "command": "/usr/bin/g++", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}.out", "`pkg-config","--libs","--cflags","opencv4`" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true } } ], "version": "2.0.0" }
现在,你可以在VScode中编写和运行C++代码,并使用OpenCV库。按下
Ctrl + Shift + B
编译并运行代码。
ubuntu vscode 配置c++ opencv环境
配置 Ubuntu 上 VSCode 的 C++ 和 OpenCV 开发环境
安装必要的依赖项
为了确保开发环境顺利搭建,在开始之前需确认已安装所有必需的软件包。可以通过以下命令来更新并安装这些工具:
sudo apt update && sudo apt upgrade -y
sudo apt install build-essential cmake git pkg-config libgtk-3-dev \
libavcodec-dev libavformat-dev libswscale-dev python3-dev python3-numpy \
libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev gfortran openexr \
libatlas-base-dev
此操作会准备系统以支持后续的编译工作[^1]。
下载并构建 OpenCV
获取指定版本的 OpenCV 源码,并按照如下方式完成其本地化部署:
cd ~
git clone https://github.com/opencv/opencv.git
cd opencv
mkdir build && cd build
cmake ..
make -j$(nproc)
sudo make install
上述过程将从 GitHub 获取最新的源代码,创建一个用于编译的临时文件夹 build
并通过 CMake 工具链进行配置,最后利用多线程加速编译流程以及安装最终产物到默认位置[^2]。
设置 VSCode 支持 C++
安装 Microsoft 提供的官方扩展插件 “C/C++”,它提供了 IntelliSense 功能以及其他特性帮助编写高效的 C++ 代码;同时还需要设置好 launch.json 文件以便调试程序:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/your_program_name",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "compile"
}
]
}
这段 JSON 片段定义了一个简单的启动配置,其中包含了指向可执行文件的位置和其他选项,使得开发者可以方便地测试自己的应用程序[^3]。
创建任务用于自动化编译
编辑 .vscode/tasks.json
来简化日常工作中频繁使用的编译指令集:
{
"tasks": [
{
"label": "compile",
"command": "/usr/bin/cmake",
"args": ["--build", "."],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
}
]
}
这里指定了一个名为 compile 的任务,当按下 Ctrl+Shift+B 组合键时自动触发该任务执行一次完整的重新编译动作。
测试环境有效性
编写一段简单的小例子验证当前环境是否能够正常运作,比如读取图像显示窗口等基本功能:
#include <iostream>
#include <opencv2/opencv.hpp>
int main(int argc, char **argv){
cv::Mat image;
image = cv::imread("/path/to/image.jpg");
if (image.empty()){
std::cout << "Could not read the image." << std::endl ;
return 1;
}
cv::imshow("Display window", image);
int k = cv::waitKey(0); // Wait infinitely until a keypress event occurs
return 0;
}
保存以上代码片段至合适路径下作为入口点,随后尝试运行看看能否成功加载图片资源并弹出预览界面。
相关推荐
















