使用wsl搭建vscode调试环境

Windows Subsystem for Linux(简称WSL)是一个在Windows 10上能够运行原生Linux二进制可执行文件(ELF格式)的兼容层。它是由微软与Canonical公司合作开发,其目标是使纯正的Ubuntu、Debian等映像能下载和解压到用户的本地计算机,并且映像内的工具和实用工具能在此子系统上原生运行。

使用了wsl可以丢弃VMware。

安装wsl

参考:https://docs.microsoft.com/en-us/windows/wsl/install-manual

PowerShell执行下面命令安装wsl:

1
2
3
#安装wsl
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

重启机器完成WSL安装并更新到WSL2。

下载WSL2 Linux kernel update package for x64 machines,并安装。

PowerShell执行下面命令设置WSL2为默认版本:

1
2
#设置wsl
wsl --set-default-version 2

从Microsoft Store下载Windows Terminal。

设置wsl限制内存,在C:\Users\Administrator中创建文件.wslconfig,设置内容如下:

1
2
3
4
5
#设置wsl
[wsl2]
memory=5GB
swap=0
localhostForwarding=true

安装Cenos

https://github.com/wsldl-pg/CentWSL/releases下载centos,要根据你的windows10的版本下载对应的centos,您可以点击开始>输入winver回车查看到系统的具体版本信息。

下载后安装即可。

1
2
3
4
5
6
7
8
9
10
11
12
#环境准备
yum -y install gcc gcc-c++ kernel-devel make gdb zlib-devel
yum -y install zsh git
// 安装oh my zsh
git clone https://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
chsh -s /bin/zsh
// 设置zsh插件
git clone https://www.github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions
git clone https://www.github.com/zsh-users/zsh-syntax-highlighting.git $ZSH_CUSTOM/plugins/zsh-syntax-highlighting
plugins=(git zsh-autosuggestions) // vim ~/.zshrc
git config --add oh-my-zsh.hide-dirty 1

启动Windows Terminal,进入CentOS7,进入原理如下。

启动VScode

vscode安装Remote - WSL插件。

wsl的vscode中安装C++ Intellisense插件。

在centos中的/mnt中,cd到代码工程目录,执行code .即可在vscode中打开工程,按F5开启调试,launch.json中可以设置调试信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#launch设置
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "/mnt/e/SoftDev/scdnnode/bin/yfnode",
"args": ["-p", "/"],
"stopAtEntry": true,
"cwd": "/mnt/e/SoftDev/scdnnode/bin/",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

调试效果

nephen wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!