VSCode C++开发环境配置
01 ubuntu18.04 配置vscode
参考官网 https://code.visualstudio.com/docs/setup/linux
01.01 安装code包
为基于Debian / Ubuntu的发行版安装Visual Studio Code的最简单方法是通过图形软件中心(如果有)或通过命令行通过以下方式下载并安装.deb软件包(64位)
https://code.visualstudio.com/docs/setup/linux#_debian-and-ubuntu-based-distributions
这里下载的文件名称:code_1.41.1-1576681836_amd64.deb,运行
sudo apt install ./code_1.41.1-1576681836_amd64.deb
运行vscode,命令行输入:code,会启动vscode。
01.02 安装C/C++扩展
参考:https://code.visualstudio.com/docs/cpp/config-wsl,这个是针对windows上面的wsl的,对其它linux版本也适用。1
正常情况下,你应该已经更新了linux版本,并且按照了自己想要的gcc/g++/gdb等C/C++开发工具链。
sudo apt-get update sudo apt-get install build-essential gdb gcc g++可以查找自己安装的gcc/g++版本
whereis g++ whereis gdb
在vscode界面中,按(Ctrl + Shift + X)组合键,转到“扩展”视图。搜索感兴趣的扩展关键词,安装需要的扩展。
1.安装C / C ++语言扩展
输入C++,搜索ms-vscode.cpptools和austin.code-gnu-global。

安装了c/c++扩展后,就可以采用Makefile文件编译调试了。
注意:安装插件后,要重新启动vscode才能生效。
添加 hello.cpp 测试文件。
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
for (const auto& word : msg) {
cout << word << " ";
}
cout << '\n';
return 0;
}
C / C ++配置(c_cpp_properties.json)2
通过运行命令C / C ++来查看C / C ++配置UI :从命令面板(Ctrl + Shift + P)编辑配置(UI)。

{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
后面增加CMake的时候,会增加
"configurationProvider": "vector-of-bool.cmake-tools",
"compileCommands": "${workspaceFolder}/build/compile_commands.json"
配置 tasks.json用于编译3
从主菜单中,选择Terminal > Configure Default Build Task(主菜单->终端->配置任务)。选择自己适用的C++编译器。

{
// 有关 tasks.json 格式的文档,请参见
// https://go.microsoft.com/fwlink/?LinkId=733558
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "mkdir -p ./build ; /usr/bin/g++ -g *.cpp -o ./build/hello",
"args": [],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
"label": "g++ build active file",对应launch.json中的"preLaunchTask": "g++ build active file",。
需要重点关注:"command": "mkdir -p ./build ; /usr/bin/g++ -g *.cpp -o ./build/hello",以及"args": [],。
配置launch.json用于调试。4
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/hello",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "/usr/bin/gdb",
}
]
}
"type": "cppdbg", // 这里类型算则cppdbg
"program": "${workspaceFolder}/build/hello", // 编译后文件文件放在了build下面
"externalConsole": false, // 设为false
"preLaunchTask": "g++ build active file", // "g++ build active file"是tasks.json中g++编译的配置label名称,这个配置项直接采用g++命令行编译*.cpp文件。
到此,点击左边栏设置断点;即可按F5编译、调试。

2.安装中文扩展
输入Language,选择Chinese (Simplified) Language Pack for Visual Studio Code;安装中文扩展插件。

3.安装CMake扩展
VSCode支持集成CMake。
A. ubuntu系统安装CMake。
sudo apt install cmake
查看cmake版本:
cmake -version

B. 为VSCode安装CMake扩展
(Ctrl + Shift + X)转到“扩展”视图,安装CMake Tools。

C.编写CMakeLists.txt文件。
注意:需要指定为可调式模式。
使用set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
或者SET(CMAKE_BUILD_TYPE "DEBUG" )。
cmake_minimum_required(VERSION 3.10)
project( hello )
# 设置为调试模式
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
# SET(CMAKE_BUILD_TYPE "DEBUG" )
set(SOURCE hello.cpp)
add_executable(${PROJECT_NAME} ${SOURCE})
D.在tasks.json中添加对CMake的支持
在tasks.json中增加一个"label": "cmake build active file",用来编译CMakeLists.txt。
{
// 有关 tasks.json 格式的文档,请参见
// https://go.microsoft.com/fwlink/?LinkId=733558
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "mkdir -p ./build ; /usr/bin/g++ -g *.cpp -o ./build/hello",
"args": [],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"type": "shell",
"label": "cmake build active file",
"command": "mkdir -p ./build; cd ./build; cmake ../ ; make ",//编译命令
"args": [
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
E.修改launch.json的preLaunchTask为tasks.json中的cmake build active file
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/hello",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "cmake build active file",
"miDebuggerPath": "/usr/bin/gdb",
}
]
}
F. 完成A-E5步,F5编译。

4. VSCode 的 vim 扩展
传统linux开发着更喜欢vi或者vim。VSCode提供了 vim 扩展。

5. git扩展
git 扩展应该是必备的。首推 GitLens扩展。

本文详细介绍了如何在 Ubuntu 18.04 上配置 VSCode 以支持 C++ 开发,包括安装必要的软件包、扩展插件,如 C/C++、CMake、中文扩展、Vim 和 Git 扩展,以及配置编译和调试环境。
3023

被折叠的 条评论
为什么被折叠?



