VSCode 使用Bazel編譯調(diào)試 C++

前言

由于最近開發(fā)上使用了Bazel來構(gòu)建,而且是用vscode進(jìn)行遠(yuǎn)程后臺(tái)開發(fā),使用Remote Development插件,以前大多都是CLion開發(fā)C++,對(duì)vscode還是不太熟悉,這里簡單記錄下vscode的相關(guān)配置

vscode 編譯流程

在工程目錄下有個(gè).vscode 文件夾,里面是所有 vscode 的配置文件,編譯運(yùn)行配置也在這里,涉及的配置文件有 launch.json和 tasks.json,不存在的話就自己創(chuàng)建即可

大致執(zhí)行流程是這樣的:

  1. vscode 會(huì)先遍歷 launch.json,找到configurations,也就是入口啟動(dòng)配置
  2. 在configurations里會(huì)先找preLaunchTask,也就是啟動(dòng)前的任務(wù)
  3. 拿到configurations.preLaunchTask去 tasks.json 里找對(duì)應(yīng)的具體任務(wù)
  4. tasks.json 里有個(gè)tasks列表,里面是多個(gè) task任務(wù),task名字是 label 字段,也就是和configurations.preLaunchTask對(duì)應(yīng)
  5. 找到 task 后先執(zhí)行configurations.preLaunchTask,然后執(zhí)行configurations.program

具體配置

搞清楚大致流程后,就可以參照樣例寫自己的配置文件了
我這里是測(cè)試邏輯,編譯后的執(zhí)行文件在${workspaceFolder}/bazel-bin/server/TestServer,可以根據(jù)自己實(shí)際情況修改

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",
            "preLaunchTask": "BuildDebug",
            "type": "cppdbg",
            "MIMode": "gdb",
            "request": "launch",
            "program": "${workspaceFolder}/bazel-bin/server/TestServer",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
        }
    ]
}

tasks.json:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
          "label": "BuildDebug",
          "type": "shell",
          "command": "echo ${workspaceFolder} && bazel build //server:TestServer -c dbg",
          "options": {
                "cwd": "${workspaceFolder}"
            },
          "group": {
            "kind": "build",
            "isDefault": true
          }
        }
      ]
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容