title: VScode配置task和launch
date: 2022-12-12 20:57:26
cover:
categories: VisualCode
tags:
- debug
VScode配置task和launch支持C++11
刚开始使用VScode一般都是使用默认的task和lunch配置去执行代码或者debug,一旦修改了相关目录或者改动一些参数,就会不停的提醒XXX文件不存在或者一些奇怪错误。今天就专门花时间研究了一下task和lunch怎么用
因为task和launch都是使用json编写,并且是用来启动编译器,所以需要一些 预备知识
- 编译器相关参数
- json语法
Task的配置
task就是你当前要执行的任务,如果不手动配置,vs会自动配置当前环境的编译任务
初次创建文件或者目录后,按下 F1键盘 会自动生成launch和task文件
默认task如下
{"tasks": [{"type": "cppbuild","label": "C/C++: g++ build active file","command": "/usr/bin/g++","args": ["-fdiagnostics-color=always","-g","${file}","-o","${fileDirname}/${fileBasenameNoExtension}"],"options": {"cwd": "${fileDirname}"},"problemMatcher": ["$gcc"],"group": "build","detail": "Task generated by Debugger."}],"version": "2.0.0"
}
以下是VS官方文档参数说明
- label: The task’s label used in the user interface.
- type: The task’s type. For a custom task, this can either be
shell
orprocess
. Ifshell
is specified, the command is interpreted as a shell command (for example: bash, cmd, or PowerShell). Ifprocess
is specified, the command is interpreted as a process to execute. - command: The actual command to execute.
- windows: Any Windows specific properties. Will be used instead of the default properties when the command is executed on the Windows operating system.
- group: Defines to which group the task belongs. In the example, it belongs to the
test
group. Tasks that belong to the test group can be executed by running Run Test Task from the Command Palette. - presentation: Defines how the task output is handled in the user interface. In this example, the Integrated Terminal showing the output is
always
revealed and anew
terminal is created on every task run. - options: Override the defaults for
cwd
(current working directory),env
(environment variables), orshell
(default shell). Options can be set per task but also globally or per platform. Environment variables configured here can only be referenced from within your task script or process and will not be resolved if they are part of your args, command, or other task attributes. - runOptions: Defines when and how a task is run.
在配置文件下使用 CTRL+空格键 触发候选项。
我们修改的主要由这几项
- label
- command
- args
label
图中显示的名字就是定义的 label
command
command为执行任务所使用的命令,在linux下如果用gcc或者clang可以使用
which gcc
which clang
查看当前使用编译器的文件路径。比如我要使用brew安装的clang,可以修改为
"command": "/opt/homebrew/opt/llvm/bin/clang++"
更换为你要使用的编译器即可
args
args为当前编译器的参数,配合vs相关变量使用,这里列出一些常用的变量
- ${userHome} - the path of the user’s home folder
- ${workspaceFolder} - the path of the folder opened in VS Code
- ${workspaceFolderBasename} - the name of the folder opened in VS Code without any slashes (/)
- ${file} - the current opened file
- ${fileWorkspaceFolder} - the current opened file’s workspace folder
- ${relativeFile} - the current opened file relative to
workspaceFolder
- ${relativeFileDirname} - the current opened file’s dirname relative to
workspaceFolder
- ${fileBasename} - the current opened file’s basename
- ${fileBasenameNoExtension} - the current opened file’s basename with no file extension
- ${fileDirname} - the current opened file’s dirname
- ${fileExtname} - the current opened file’s extension
- ${cwd} - the task runner’s current working directory upon the startup of VS Code
- ${lineNumber} - the current selected line number in the active file
- ${selectedText} - the current selected text in the active file
- ${execPath} - the path to the running VS Code executable
- ${defaultBuildTask} - the name of the default build task
- ${pathSeparator} - the character used by the operating system to separate components in file paths
编译器的参数可以参考https://www.runoob.com/w3cnote/gcc-parameter-detail.html
"args": ["-g","${file}","-o","${fileDirname}/temp/${fileBasenameNoExtension}", //生成的文件放在temp目录下"-std=c++17" //支持c++17
],
如果要使用c++11特性,可以在这里添加相关参数
其它参数
其他参数参照VS官方文档按需配置,基本C/C++的配置太不需要改动其他操作。
Launch
根据上文自动生成的task.json launch文件已经生成了,如果没有生成的话在debug界面点击生成launch
默认生成的文件如下
{"configurations": [{"name": "C/C++: g++ build and debug active file","type": "cppdbg","request": "launch","program": "${fileDirname}/${fileBasenameNoExtension}","args": [],"stopAtEntry": false,"cwd": "${fileDirname}","environment": [],"externalConsole": false,"MIMode": "lldb","preLaunchTask": "C/C++: g++ build active file"}],"version": "2.0.0"
}
VS官方参考文档:https://code.visualstudio.com/docs/editor/debugging
这里我们要注意几个参数
- program
- MIMode
- preLaunchTask
program
program参数表示可执行文件的位置,如果找不刀可执行文件,debug会报错。
这里一定注意 要和task中build任务生成目录一致
因为我的task中,输出的二进制文件在temp文件下,所以要修改program的位置
"program": "${fileDirname}/temp/${fileBasenameNoExtension}"
MIMode
这里指定要使用的调试程序
- gdb
- lldb
我使用的是llvm所以直接就写lldb即可
如果是gcc的话就是用gdb
preLaunchTask
这里指定的是launch前置任务,一般用于生成可调式的二进制文件
因为我们已经生成了一个task,我们直接调用前一个task即可。preLaunchTask的值与task的label的值相匹配
"preLaunchTask": "C/C++: g++ build active file"
现在开始调试你的程序吧