一. 开启window的WSL(Windows Subsystem for Linux)
- 控制面板- 启用或关闭Windows功能, 需要重启系统
二. 安装WSL发行版:
- 下载
- 解压安装
下载之后是.appx, 改成.zip然后解压文件
运行ubuntu1804.exe
输入username, password进入
- 更新并安装环境
sudo apt update
sudo apt install build-essential
密码就是前面设置的密码
三. 对接VSCode
-
安装VSCode插件
-
创建代码目录
mkdir hub
- 同步目录
code hub
如果没有code命令, 通过
VSCode(Ctrl+Shift+P),输入"Remote-WSL: New Window using Distro"
- 配置调试文件
代码目录建立.VSCode文件夹,加入launch.json和tasks.json文件
// launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.out",
"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": "complie"
}
]
}
//tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "complie",
"type": "shell",
"command": "g++",
"args": [
"--std=c++11",
"-g",
"${fileDirname}/${fileBasename}",
"-o2",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.out"
],
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$gcc"
}
]
}
- 运行测试
Ctrl + Alt + N
参考并感谢:
优雅地使用VSCode与WSL在Windows 10下开发
手动安装WSL发行版