日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【转】VScode tasks.json和launch.json的设置

發布時間:2023/12/10 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【转】VScode tasks.json和launch.json的设置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉自:VScode tasks.json和launch.json的設置 - 知乎

目錄

  • C++(方法1:不使用VSCode插件,較繁瑣)
  • C++(方法2:使用Native Debug插件)
  • C++(方法3:使用C/C++ Compile Run插件)
  • Rust(使用Native Debug插件)
  • Typescript(僅做參考,這是用來寫vscode插件時的項目設置)
  • 一些常用的預定義變量
  • 在網上搜了幾篇文章,都寫的不是很詳細,而且好多都過時了。尤其很多是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是執行新文件

    // launch.json{"version": "0.2.0","configurations": [{"name": "(gdb) Launch", //這個應該是F1中出現的名字"preLaunchTask": "Build", //在launch之前運行的任務名,這個名字一定要跟tasks.json中的任務名字大小寫一致"type": "cppdbg","request": "launch","program": "${fileDirname}/${fileBasenameNoExtension}.exe", //需要運行的是當前打開文件的目錄中,名字和當前文件相同,但擴展名為exe的程序"args": [],"stopAtEntry": false, // 選為true則會在打開控制臺后停滯,暫時不執行程序"cwd": "${workspaceFolder}", // 當前工作路徑:當前文件所在的工作空間"environment": [],"externalConsole": true, // 是否使用外部控制臺,選false的話,我的vscode會出現錯誤"MIMode": "gdb","miDebuggerPath": "c:/MinGW/bin/gdb.exe","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}]}] }

    二、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)的名字

    預定義變量示例:

    假設你滿足以下的條件

  • 一個文件?/home/your-username/your-project/folder/file.ext?在你的編輯器中打開;
  • 一個目錄?/home/your-username/your-project?作為你的根目錄.
  • 下面的預定義變量則代表:

    • ${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的位置
    Tip: vscode的智能提示會在tasks.json和launch.json?提示所有支持的預定義變量.

    總結

    以上是生活随笔為你收集整理的【转】VScode tasks.json和launch.json的设置的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。