news 2026/10/21 13:39:05

在WSL中配置VS Code C++开发环境完整教程

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
在WSL中配置VS Code C++开发环境完整教程
设置Linux开发环境
  1. 启动WSL终端:在Windows搜索栏输入"Ubuntu"并打开

Ubuntu in Start Menu

  1. 首次启动会要求设置用户名和密码,请记住此密码,后续使用sudo命令时需要
  2. 更新系统包列表并安装GNU编译器工具和GDB调试器:

代码语言:bash

AI代码解释

sudo apt-get update sudo apt-get install build-essential gdb
  1. 验证安装是否成功:

代码语言:bash

AI代码解释

whereis g++ whereis gdb

如果安装成功,会显示g++和gdb的路径信息。

  1. 创建项目目录:

代码语言:bash

AI代码解释

mkdir projects cd projects mkdir helloworld cd helloworld

在WSL中启动VS Code

在WSL终端中,进入项目目录并输入以下命令启动VS Code:

代码语言:bash

AI代码解释

code .

首次启动时,VS Code会自动下载并安装WSL服务器组件。成功启动后,VS Code窗口标题栏会显示"WSL: Ubuntu",状态栏会显示远程连接状态。

Remote context in the Status bar

安装C/C++扩展

  1. 在VS Code中打开扩展面板(Ctrl+Shift+X)
  2. 搜索"C/C++"扩展(由Microsoft提供)
  3. 如果已在本地安装,点击"Install in WSL"按钮将其安装到WSL环境中

Install in WSL button

安装完成后需要重新加载VS Code以生效。

创建和编写C++代码

  1. 在VS Code文件资源管理器中,点击"新建文件"按钮,创建helloworld.cpp

New File title bar button

  1. 粘贴以下示例代码:

代码语言:cpp

AI代码解释

#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; }
  1. 保存文件(Ctrl+S)

探索IntelliSense功能

VS Code的C/C++扩展提供了强大的IntelliSense功能:

  • 悬停在vector或string上查看类型信息
  • 输入msg.时会自动显示成员函数列表
  • 自动补全功能可通过Tab键触发

Statement completion IntelliSense

运行C++程序

  1. 确保helloworld.cpp是当前活动文件
  2. 点击编辑器右上角的"播放"按钮
  3. 在弹出的编译器选择中,选择"g++ build and debug active file"

C++ debug configuration dropdown

VS Code会自动生成tasks.json文件,用于配置构建任务。成功运行后,在集成终端中会看到输出:

代码语言:txt

AI代码解释

Hello C++ World from VS Code and the C++ extension!
理解tasks.json

自动生成的tasks.json位于.vscode目录下,内容如下:

代码语言:json

AI代码解释

{ "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." } ] }

关键参数说明:

  • command: 指定编译器路径(g++)
  • args: 编译参数,-g表示生成调试信息
  • ${file}: 当前活动文件
  • ${fileDirname}/${fileBasenameNoExtension}: 输出可执行文件路径和名称

www.dongchedi.com/article/7588948864296714814
www.dongchedi.com/article/7588948673011728921
www.dongchedi.com/article/7588948986024198681
www.dongchedi.com/article/7588947393409090072
www.dongchedi.com/article/7588949176365646360
www.dongchedi.com/article/7588949135194440217
www.dongchedi.com/article/7588948142243070526
www.dongchedi.com/article/7588946058064544318
www.dongchedi.com/article/7588945397742551614
www.dongchedi.com/article/7588944515726492185
www.dongchedi.com/article/7588947344343859737
www.dongchedi.com/article/7588946689940537881
www.dongchedi.com/article/7588947009176863257
www.dongchedi.com/article/7588944406535750168
www.dongchedi.com/article/7588946073901629977
www.dongchedi.com/article/7588945847925637657
www.dongchedi.com/article/7588707381962424856
www.dongchedi.com/article/7588942138353123865
www.dongchedi.com/article/7588944252701442584
www.dongchedi.com/article/7588943488239485464
www.dongchedi.com/article/7588943863319151128
www.dongchedi.com/article/7588706162695389758
www.dongchedi.com/article/7588944665073009177
www.dongchedi.com/article/7588706119292289598
www.dongchedi.com/article/7588943021375291929
www.dongchedi.com/article/7588944306828837400
www.dongchedi.com/article/7588945091293872665
www.dongchedi.com/article/7588945474028438040
www.dongchedi.com/article/7588942163632161304
www.dongchedi.com/article/7588945704186413593


www.dongchedi.com/article/7588948673011663385

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/10/20 5:42:14

别再乱买电竞耳机了!职业选手都在用的“隐藏参数”曝光

花几百上千块买的电竞耳机&#xff0c;却总在决赛圈被敌人绕后偷袭&#xff1f;明明开着7.1环绕声&#xff0c;却连脚步声从左还是右来都分不清&#xff1f;团战沟通时&#xff0c;队友只听见你这边的键盘杂音&#xff0c;关键指令全错过&#xff1f;别再怪自己反应慢&#xff…

作者头像 李华
网站建设 2026/10/21 3:14:27

ModbusTCP报文格式说明:调试过程中典型问题汇总

深入理解 Modbus TCP 报文结构&#xff1a;从协议解析到调试实战在工业自动化现场&#xff0c;你是否遇到过这样的场景&#xff1f;PLC 和上位机明明连上了网&#xff0c;Ping 得通&#xff0c;端口也能 Telnet 通&#xff0c;但数据就是读不出来——要么返回异常码&#xff0c…

作者头像 李华
网站建设 2026/10/21 3:14:33

从零实现基于UDS诊断协议的诊断请求响应处理

手把手教你实现一个轻量级UDS诊断引擎 你有没有遇到过这样的场景&#xff1a;手握CAN分析仪&#xff0c;连上OBD接口&#xff0c;发了一串 22 F1 90 &#xff0c;却迟迟等不来VIN码的回应&#xff1f;或者在刷写ECU时卡在“进入编程会话”这一步&#xff0c;看着诊断仪反复超…

作者头像 李华
网站建设 2026/10/21 3:14:34

系统学习Multisim元器件图标布局与调用技巧

玩转Multisim元器件&#xff1a;从“找不着”到“秒调用”的实战指南 你有没有过这样的经历&#xff1f; 打开 Multisim 准备搭个放大电路&#xff0c;想找个 LM358 运放&#xff0c;结果在“Analog ICs”里翻来翻去&#xff0c;点开十几个子类也没找到&#xff1b;或者设计数…

作者头像 李华
网站建设 2026/10/21 3:29:21

深入浅出JS事件:从基础原理到实战进阶全解析

&#x1f4da; 前言&#xff1a; 在JavaScript交互开发中&#xff0c;事件是连接用户操作与程序逻辑的核心桥梁。无论是点击按钮、输入文本&#xff0c;还是页面加载完成&#xff0c;本质上都是事件驱动的结果。但很多开发者在使用事件时&#xff0c;往往只停留在“会用”的层面…

作者头像 李华
网站建设 2026/10/21 3:29:32

PyTorch-CUDA-v2.6镜像是否支持A100/H100?答案在这里

PyTorch-CUDA-v2.6镜像是否支持A100/H100&#xff1f;答案在这里 在当今大模型训练如火如荼的背景下&#xff0c;硬件选型与软件环境的匹配成了决定项目成败的关键一环。你有没有遇到过这样的情况&#xff1a;好不容易申请到了搭载 H100 的计算资源&#xff0c;兴冲冲地拉下 P…

作者头像 李华