WSL(Windows Subsystem for Linux)是windows下的linux环境。
本文将记录如何配置Visual Studio Code,使其能在WSL下的Ubuntu系统中编写C++程序。
一、前提条件1.安装WSL。参考WSL2安装_sy95122的博客-CSDN博客
2.window下安装VSCode
3.在VSCode扩展商店中安装Remote-WSL extension
二、WSL配置Linux环境1.打开WSL控制台
2.建立文件夹projects/helloworld
mkdir projects cd projects mkdir helloworld
3.安装GNU编译工具和GDB调试器
sudo apt-get install build-essential gdb三、在WSL下运行VSCode
cd $HOME/projects/helloworld code .
期间会提示“Installing VS Code Server”,安装linux端的vs code小型服务端,用以与window下vc code会话通信。然后windows下vs code会自启动,界面上会有以下显示
在底部状态栏也能看到wsl字样
注:wsl控制台,在相应目录下使用“code . ”命令,就会在当前目录下打开vs code,并以此目录做为工作区。
四、添加C++代码1.在vs code的文件浏览器上,选择添加新文件按钮,添加helloworld.cpp文件
2.安装c/c++ extension
如果vs code中未安装c/c++ extention,在添加完helloworld.cpp文件后,会提示安装
选择安装,并重新加载,然后在扩展面板(Ctrl+Shift+X)中,C/C++ extension项上点击“Install in WSL”按钮(如下图)
3.添加代码
#include#include #include using namespace std; int main() { vector msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"}; for (const string& word : msg) { cout << word << " "; } cout << endl; }
4.运行程序
选择”g++建立和调试活动文件(g++ build and debug active file)”
运行结果如下



