win10 下 vscode 运行编译 rust 并连接子系统 ubuntu 终端
一、vscode 搭建 rust 环境
找到 vscode 的 extensions 加入 rust:
具体使用功能在上面有给出,自行阅读文档:
依据 Quick start 执行相关步骤。
rust 使用 cargo 在终端执行:
至此 rust 已可通过终端在 vscode 上编写,并有相关提示,但 cargo build 等操作需要在终端进行。
二、使用Debug
打开项目目录找到 .vscode 目录下的 launch.json 文件:
内容修改:
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceRoot}/target/debug/xxxx.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true
},
{
"name": "(OSX) Launch",
"type": "lldb",
"request": "launch",
"program": "${workspaceRoot}/target/debug/xxxx",
"args": [],
"cwd": "${workspaceRoot}",
}
]
}
注意 program 下的可执行文件需要使用 cargo build 建立之后才可执行,将 xxxx 替换为可执行文件名即可。
三、vscode 连接子系统 ubuntu 终端
同样在 extensions 里安装 Remote - wsl:
当然前提是你已经构建好了 win10 的子系统,如果没有可参看我的其他文档构建,继续下一步:
点击这个左下角的绿色方块:
打开子系统的任意一个 Folder 就可以了,此时 new 一个新的 终端就是子系统的终端环境。
三、BUG
第一次在 win10 下 cargo build 可能报错:
error: linker ink.exe not found
粗暴的安装一个 VS 2019 就可以了,选一个使用 c++ 的桌面开发即可,反正说不定哪天也用的上: