Visual Studio Code 设置编译任务和调试配置
tasks.json
tasks.json 用于设置构建指令。
Python
{
"version": "2.0.0",
"tasks": [
{
"label": "main",
"type": "shell",
"command": "python",
"args": [
"main.py",
"${input:arg1}",
"${input:arg2}"
],
"options": {
"cwd": "${workspaceFolder}",
"env": {
"PYTHONPATH": "${workspaceFolder}"
}
},
"group": {
"kind": "build",
"isDefault": false
},
"problemMatcher": [],
"detail": "Run main.py"
}
],
"inputs": [
{
"id": "arg1",
"type": "pickString",
"description": "Select the option",
"default": "opt1",
"options": ["opt1", "opt2"]
},
{
"id": "arg2",
"type": "promptString",
"description": "Enter the value",
"default": "placeholder..."
}
]
}
C++
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}
-
启动任务的快捷键是
Shift + Cmd + B。 -
可以在控制面板(
Shift + Cmd + P)中输入Tasks: Open User Tasks来设置用户任务。 -
可以使用 Task Manager 插件管理任务。
参考:
Problem Matcher
{
"tasks": [
{
"problemMatcher": {
"owner": "cpp",
"fileLocation": "autoDetect",
"source": "gcc",
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
]
}
- 关于编写 Problem Matcher 可以参见 Defining a problem matcher | Visual Studio Code Docs
- 测试正则表达式:RegEx101 playground
Compound Tasks
{
"tasks": [
{
"label": "build",
"type": "shell",
"command": "echo build finished",
"dependsOrder": "sequence",
"dependsOn": ["step1", "step2"]
}
]
}
参考:Compound tasks | Visual Studio Code Docs
launch.json
launch.json 用于管理调试配置。
Python
program
{
"version": "0.2.0",
"configurations": [
{
"name": "main",
"type": "debugpy",
"request": "launch",
"cwd": "${workspaceFolder}",
"python": ".venv/bin/python",
"program": "main.py",
"args": [
"${input:arg1}",
"${input:arg2}",
"${command:pickArgs}"
],
"env": {
"PYTHONPATH": "${workspaceFolder}"
},
"console": "integratedTerminal",
"justMyCode": true
}
],
"inputs": [
{
"id": "arg1",
"type": "pickString",
"description": "Select the option",
"default": "opt1",
"options": ["opt1", "opt2"]
},
{
"id": "arg2",
"type": "promptString",
"description": "Enter the value",
"default": "placeholder..."
}
]
}
module
{
"version": "0.2.0",
"configurations": [
{
"name": "train",
"type": "debugpy",
"request": "launch",
"cwd": "${workspaceFolder}",
"module": "torch.distributed.run",
"args": [
"--nnodes=1",
"--nproc_per_node=8",
"train.py"
],
"env": {
"PYTHONPATH": "${workspaceFolder}"
},
"console": "integratedTerminal",
"justMyCode": true
}
]
}
常用 module:
torch.distributed.run: torchrunaccelerate.commands.launch: accelerate launch
Node.js
{
"version": "0.2.0",
"compounds": [
{
"name": "Main + renderer",
"configurations": ["Main", "Renderer"],
"stopAll": true
}
],
"configurations": [
{
"name": "Renderer",
"port": 9222,
"request": "attach",
"type": "chrome",
"webRoot": "${workspaceFolder}"
},
{
"name": "Main",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron",
"windows": {
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron.cmd"
},
"args": [".", "--remote-debugging-port=9222"],
"outputCapture": "std",
"console": "integratedTerminal"
}
]
}
C++
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++: g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"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": "C/C++: g++ build active file"
}
]
}
可以在 Visual Studio Code 的 launch 设置项中设置用户 launch 配置。
参考:
- Visual Studio Code debug configuration | Visual Studio Code Docs
- Python debugging in VS Code | Visual Studio Code Docs
- Python environments in VS Code | Visual Studio Code Docs
- Using C++ on Linux in VS Code | Visual Studio Code Docs
参见:
- Running and debugging Java | Visual Studio Code Docs
- Configure C/C++ debugging | Visual Studio Code Docs
- Debugging TypeScript | Visual Studio Code Docs
常用变量
${command:pickArgs}:手动输入参数${workspaceFolder}:工作区目录路径${workspaceFolderBasename}:工作区目录名${userHome}:用户主目录${file}:当前文件${/}:在类 UNIX 系统下是/,在 Windows 下是\${env:PATH}:系统环境变量

浙公网安备 33010602011771号