Ubuntu 系统安装 VS Code 并配置 C++ 环境

devtools/2024/9/23 10:24:09/

Linux 系列教程:

  • VMware 安装配置 Ubuntu(最新版、超详细)
  • FinalShell 远程连接 Linux(Ubuntu)系统
  • Ubuntu 系统安装 VS Code 并配置 C++ 环境

➡️➡️ ➡️VS Code 官方教程:Using C++ on Linux in VS Code(仅供参考,具体步骤如下)

文章目录

  • 1. 配置 C++ 环境
  • 2. 安装 VS Code
  • 3. 项目运行
  • 4. 修改

1. 配置 C++ 环境

在 Linux(Ubuntu )中配置 C/C++ 环境非常之简单,只需按 Ctrl + Alt + T 调出终端,再按照以下步骤:

[Step 1]: 检查是否已经安装了 GCC,要验证是否正确,请输入以下命令

gcc -v

[Step 2]: 若未安装,在终端运行以下命令来更新 Ubuntu 包列表

sudo apt-get update

[Step 3]: 接下来,使用以下命令安装 GNU 编译器工具和 GDB 调试器:

sudo apt-get install build-essential gdb

[Step 4]: 再次查看各编译/调试工具是否成功安装

gcc --version
g++ --version
gdb --version

2. 安装 VS Code

[Step 1]: 由于是在 Linux(Ubuntu)中安装,即可以直接在 Ubuntu Software 搜索,再点击 Install


[Step 2]: 安装完成后,在终端按照如下命令所示新建项目文件夹并在 VS Code 中打开

mkdir C++ # 新建文件夹
ls # 查看所有文件夹
cd C++ # 进入 C++ 文件夹
code . # 用 VS Code打开当前 C++ 文件夹

[Step 3]: 点击左侧扩展按钮(或快捷键 Ctrl + Shift + X),搜索栏中输入 C++ 点击查看,再点击 Install

3. 项目运行

[Step 1]: 新建 main.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 string& word : msg){cout << word << " ";}cout << endl;cout << endl;
}

[Step 2]: 点击菜单栏中的 Run 选择 Run Without Debugging(或快捷键 Ctrl + F5),再选择 g++ 生成并调试活动文件

[Step 3]: 此时,在项目左侧会自动生成 .vscode/tasks.json 文件,并输出编译结果

4. 修改

注意:

  • 最后我还是按照了 VS Code 官方教程 Using C++ on Linux in VS Code 重新配置了一下;
  • 并参照 Ubuntu——VSCode运行C程序出现 [1] + Done “/usr/bin/gdb“ --interpreter=mi --tty=${DbgTerm} 去除了终端输出中的 [1] + Done ...

1、tasks.json(编译器生成设置):

{"version": "2.0.0","tasks": [{"type": "shell","label": "C/C++: g++ build active file","command": "/usr/bin/g++","args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"],"options": {"cwd": "/usr/bin"},"problemMatcher": ["$gcc"],"group": {"kind": "build","isDefault": true},"detail": "Task generated by Debugger."}]
}

2、launch.json(调试器设置)

{"version": "0.2.0","configurations": [{"name": "C/C++: 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、c_cpp_properties.json(编译器路径和IntelliSense设置)

{"configurations": [{"name": "Linux","includePath": ["${workspaceFolder}/**"],"defines": [],"compilerPath": "/usr/bin/gcc","cStandard": "c11","cppStandard": "c++17","intelliSenseMode": "clang-x64"}],"version": 4
}

最后,效果图如下所示:


http://www.ppmy.cn/devtools/6718.html

相关文章

idea项目启动异常:Command line is too long.

项目场景&#xff1a; 提示&#xff1a;这里简述项目相关背景&#xff1a; idea中启动项目报错&#xff1a; 解决方案 在idea 的运行配置中&#xff0c;修改enviroment下的shorten command line 为jar manifest 注&#xff1a; 有时shorten command line 可能不是默认存在的…

pytorch中unsqueeze用法说明

在指定的位置插入一个维度&#xff0c;有两个参数&#xff0c;input是输入的tensor&#xff0c;dim是要插到的维度 需要注意的是dim的范围是[-input.dim()-1, input.dim()1)&#xff0c;是一个左闭右开的区间&#xff0c;当dim为负值时&#xff0c;会自动转换为dim diminput.…

web server apache tomcat11-04-manager 如何管理?

前言 整理这个官方翻译的系列&#xff0c;原因是网上大部分的 tomcat 版本比较旧&#xff0c;此版本为 v11 最新的版本。 开源项目 从零手写实现 tomcat minicat 别称【嗅虎】心有猛虎&#xff0c;轻嗅蔷薇。 系列文章 web server apache tomcat11-01-官方文档入门介绍 web…

C语言 | Leetcode C语言题解之第29题两数相除

题目&#xff1a; 题解&#xff1a; int divide(int dividend, int divisor){int cnt 0;int sign 1;if ((dividend ^ divisor) < 0) { // 两数任意一个为负数sign -1;}if (divisor INT_MIN) { // 除数边界值特殊处理if (dividend INT_MIN) {return 1;} else {return 0…

elasticSearch从零整合springboot项目实操

type会被弃用 &#xff0c;就是说之后的elasticSearch中只会存在 索引&#xff08;indices&#xff09; 和 一行&#xff08;document&#xff09; 和字段&#xff08;fields&#xff09; elasticSearch 和solr的区别最大的就是 es对应的 是 json的格式 。 solr有xml和josn等…

【备战算法岗】—— 控制模块复习(持续更新!!!)

1 控制理论基础 1.1 控制模块概述 输入&#xff1a;轨迹线Reference、地图信息、定位信息、车辆反馈信息 输出&#xff1a;刹车、油门、转向 CANBUS&#xff1a;车辆底盘交互协议 参考博客&#xff1a;Apollo CANBUS模块解析 apollo&#xff1a;canbus模块&#xff08;1&…

selenium_定位输入框并输入值_id

定位id号 from time import sleepfrom selenium import webdriver# 获取浏览器对象 driver webdriver.Edge() # 打开 url url r"C:\Users\黄永生\Desktop\软件测试\tpshop\web自动化_day01_课件笔记资料代码\02_其他资料\注册A.html" driver.get(url) # 查找元素 用…

现在复习计算机考研408怎么复习?

考研是一场持久战&#xff0c;需要科学规划和备考&#xff0c;针对这个问题&#xff0c;小编在这里给大家总结一下老师建议的科学备考408思路&#xff1a; 01 先学C语言 在学408之前需要学一下C语言&#xff0c;特别是零基础跨考的同学&#xff0c;建议一定要学习C语言。 建议大…