【转】VScode tasks.json和launch.json的设置
轉自:VScode tasks.json和launch.json的設置 - 知乎
目錄
在網上搜了幾篇文章,都寫的不是很詳細,而且好多都過時了。尤其很多是task0.1.0的版本,vscode自動會屏蔽掉。
根據我的理解,tasks用于在launch前執行任務,launch用于讀取執行文件。
這兩者經常組合起來用于需要編譯語言的自動編譯+自動執行,下面以C++、Rust和Typescript為例。
一、 C++(方法1:不使用VSCode插件,較繁瑣)
在當前文件是C++的情況下,tasks可以被用來做編譯,而launch用來執行編譯好的文件。
// tasks.json {// https://code.visualstudio.com/docs/editor/tasks"version": "2.0.0","tasks": [{"label": "Build", // 任務的名字叫Build,注意是大小寫區分的,等會在launch中調用這個名字"type": "shell", // 任務執行的是shell命令,也可以是"command": "g++", // 命令是g++"args": ["'-Wall'","'-std=c++17'", //使用c++17標準編譯"'${file}'", //當前文件名"-o", //對象名,不進行編譯優化"'${fileBasenameNoExtension}.exe'", //當前文件名(去掉擴展名)],// 所以以上部分,就是在shell中執行(假設文件名為filename.cpp)// g++ filename.cpp -o filename.exe"group": { "kind": "build","isDefault": true // 任務分組,因為是tasks而不是task,意味著可以連著執行很多任務// 在build組的任務們,可以通過在Command Palette(F1) 輸入run build task來運行// 當然,如果任務分組是test,你就可以用run test task來運行 },"problemMatcher": ["$gcc" // 使用gcc捕獲錯誤],}] }下面是launch.json文件
里面主要是編譯器的參數,可以使用ctrl+space來查看有哪些可用參數
也可以在configurations中存在鼠標光標的情況下,點擊右下自動出現的Add Configurations按鈕
為什么選gdb不選 windows?因為這個不會執行預任務,也就沒法編譯文件了
為什么選 launch不選attach,是因為attach用來給正在執行的文件用的,比如網頁中的組件,而launch是執行新文件
二、C++(方法2:使用Native Debug插件)
VSCode里有一個Native Debug插件能大大簡化上述配置,請點擊下載。
點擊下載Native Debug?marketplace.visualstudio.com/items?itemName=webfreak.debug正在上傳…重新上傳取消?
下載安裝之后,我們再來試試。
使用Native Debug插件自動生成
三、C++(方法3:使用C/C++ Compile Run插件)
VSCode的這個插件更簡單,但只能用于單文件。
C/C++ Compile Run?marketplace.visualstudio.com/items?itemName=danielpinto8zz6.c-cpp-compile-run正在上傳…重新上傳取消?
這下根本無需配置task.json和launch.json,保存后直接按F6自動編譯運行,其他功能見插件下載頁的how to use。
Native Debug插件和C/C++ Compile Run插件的對比:前者更通用(可用于多種編譯語言),后者更簡單。
四、Rust(使用Native Debug插件)
適用于cargo new建立的項目。
具體操作
//tasks.json {// See https://go.microsoft.com/fwlink/?LinkId=733558// for the documentation about the tasks.json format"version": "2.0.0","tasks": [{"label": "Build","type": "shell","command": "cargo build"}] } // launch.json {// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{"name": "Debug","type": "gdb", // 使用Native Debug插件提供的gdb類型"request": "launch","target": "./target/debug/${workspaceFolderBasename}.exe", // 注意這里使用了第五部分講到的配置變量"preLaunchTask": "Build","cwd": "${workspaceRoot}","valuesFormatting": "parseText"}] }五、Typescript(僅做參考,這是用來寫vscode插件時的項目設置)
tasks.json
// See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format {"version": "2.0.0","tasks": [{"type": "npm","script": "watch","problemMatcher": "$tsc-watch","isBackground": true,"presentation": {"reveal": "never"},"group": {"kind": "build","isDefault": true}}] }launch.json
{"version": "0.2.0","configurations": [{"name": "Run Extension","type": "extensionHost","request": "launch","runtimeExecutable": "${execPath}","args": ["--extensionDevelopmentPath=${workspaceFolder}"],"outFiles": ["${workspaceFolder}/out/**/*.js"],"preLaunchTask": "${defaultBuildTask}"},{"name": "Extension Tests","type": "extensionHost","request": "launch","runtimeExecutable": "${execPath}","args": ["--extensionDevelopmentPath=${workspaceFolder}","--extensionTestsPath=${workspaceFolder}/out/test/suite/index"],"outFiles": ["${workspaceFolder}/out/test/**/*.js"],"preLaunchTask": "${defaultBuildTask}"}] }六、一些常用的預定義變量
Visual Studio Code Variables Reference?code.visualstudio.com/docs/editor/variables-reference#_predefined-variables正在上傳…重新上傳取消?
預定義變量#
支持下面的預定義變量:
- ${workspaceFolder}?- 當前工作目錄(根目錄)
- ${workspaceFolderBasename}?- 當前文件的父目錄
- ${file}?- 當前打開的文件名(完整路徑)
- ${relativeFile}?- 當前根目錄到當前打開文件的相對路徑(包括文件名)
- ${relativeFileDirname}?- 當前根目錄到當前打開文件的相對路徑(不包括文件名)
- ${fileBasename}?- 當前打開的文件名(包括擴展名)
- ${fileBasenameNoExtension}?- 當前打開的文件名(不包括擴展名)
- ${fileDirname}?- 當前打開文件的目錄
- ${fileExtname}?- 當前打開文件的擴展名
- ${cwd}?- 啟動時task工作的目錄
- ${lineNumber}?- 當前激活文件所選行
- ${selectedText}?- 當前激活文件中所選擇的文本
- ${execPath}?- vscode執行文件所在的目錄
- ${defaultBuildTask}?- 默認編譯任務(build task)的名字
預定義變量示例:
假設你滿足以下的條件
下面的預定義變量則代表:
- ${workspaceFolder}?-?/home/your-username/your-project
- ${workspaceFolderBasename}?-?your-project
- ${file}?-?/home/your-username/your-project/folder/file.ext
- ${relativeFile}?-?folder/file.ext
- ${relativeFileDirname}?-?folder
- ${fileBasename}?-?file.ext
- ${fileBasenameNoExtension}?-?file
- ${fileDirname}?-?/home/your-username/your-project/folder
- ${fileExtname}?-?.ext
- ${lineNumber}?- 光標所在行
- ${selectedText}?- 編輯器中所選擇的文本
- ${execPath}?- Code.exe的位置
總結
以上是生活随笔為你收集整理的【转】VScode tasks.json和launch.json的设置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 今天是神舟十号出征太空9周年:三人组曾完
- 下一篇: 【转】解密Qt安装目录的结构