Skip to main content

Zig+VsCode的windows开发环境

· 3分钟阅读

环境

  • Windows 10
  • VsCode 1.80.1
  • zig 0.11.0-dev.4229+f1bd59876

安装

zig

scoop install zig-dev
zig version
# 0.11.0-dev.4229+f1bd59876

VsCode

scoop install vscode
code -v
# 1.80.1
# 74f6148eb9ea00507ec113ec51c489d6ffb4b771
# x64

VsCode插件

zls

理论上应该使用跟zig版本相符的zls,这里偷懒就直接使用Zig Language插件提示安装的版本了。

调试

建立测试项目

mkdir hello
cd hello
zig init-exe
code .

调试

VsCode中按下Ctrl+Shift+D打开调试页面,随意新建一个配置,会新建一个文件.vscode/launch.json,替换文件内容为:

{
  "version": "0.2.0",
  "configurations": [
      {
          "name": "Zig Launch (Windows)",
          "type": "cppvsdbg",
          "request": "launch",
          "program": "${workspaceFolder}/zig-out/bin/hello.exe",
          "args": [],
          "stopAtEntry": false,
          "cwd": "${workspaceFolder}",
          "environment": []
      }
  ]
}

打开main.zig,如果不能下断点,就勾选上VsCode的配置项Debug: Allow Breakpoints Everywhere

手动编译一下项目:

zig build

随意下个断点,F5启动,应该可以了。

前置编译

每次调试前都要手动编译是挺呆的,作如下调整:

Ctrl+Shift+P -> Task: Configure Task -> Create task.json fromtemplate -> Other,然后修改.vscode/tasks.json的内容为:

{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "zig build",
"group": "build",
"problemMatcher": [
"$gcc"
]
}
]
}

再修改.vscode/launch.json为:

{
"version": "0.2.0",
"configurations": [
{
"name": "Zig Launch (Windows)",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/zig-out/bin/hello.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"preLaunchTask": "build" // 前置编译
}
]
}

再次F5启动,就会先行编译了。

参考