macOS使用VSCode調(diào)試C八酒、Cpp(c++)

1.先看上一篇文章

如果你還沒有配置VSCode相關(guān)C++的環(huán)境和擴(kuò)展譬挚,先看上一篇文章macOS使用VSCode編輯Cpp(c++)

2.tasks.json

這個(gè)是無注釋的文件

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "g++ compile",
            "type": "shell",
            "command": "g++",
            "args": [
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.out",
                "-W",
                "-Wall",
                "-g",
                "-std=c++17"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "echo": true,
                "reveal": "silent",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": true,
                "clear": false
            },
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": "absolute",
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        },
        {
            "label": "Open Terminal.app",
            "type": "shell",
            "command": "osascript -e 'tell application \"Terminal\"\ndo script \"echo now VS Code is able to open Terminal.app\"\nend tell'",
            "problemMatcher": [],
            "group": "none"
        }
    ]
}

這個(gè)是帶注釋的文件青伤,但是可能由于編輯原因,直接復(fù)制這個(gè)可能會(huì)報(bào)錯(cuò)殴瘦,需要自己手動(dòng)更改,這里僅作注釋參考

{
  // Tasks in VS Code can be configured to run scripts and start processes
  // so that many of these existing tools can be used from within VS Code 
  // without having to enter a command line or write new code.
  // Workspace or folder specific tasks are configured from the tasks.json file in the .vscode folder for a workspace.
  "version": "2.0.0",
  "tasks": [
    {
      // The task's label used in the user interface.
      // Terminal -> Run Task... 看到的名字
      "label": "g++ compile",
      // The task's type. For a custom task, this can either be shell or process.
      // If shell is specified, the command is interpreted as a shell command (for example: bash, cmd, or PowerShell).
      // If process is specified, the command is interpreted as a process to execute.
      "type": "shell",// shell: 輸入命令
      // The actual command to execute.
      // 因?yàn)間++已經(jīng)在環(huán)境變量中了号杠,所以我們這里寫命令就行不用寫g++的絕對(duì)路徑
      "command": "g++",
      "args": [
        "${file}", // 表示當(dāng)前文件(絕對(duì)路徑)
        // 在這里添加你還需要鏈接的.cpp文件
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}.out",
        "-W",
        "-Wall",
        "-g",
        "-std=c++17",
      ],
      // Defines to which execution group this task belongs to.
      // It supports "build" to add it to the build group and "test" to add it to the test group.
      // Tasks that belong to the build/test group can be executed by running Run Build/Test Task from the Command Palette (sft cmd P).
      // Valid values:
      //   "build",
      //   {"kind":"build","isDefault":true}, 
      //   "test",
      //   {"kind":"test","isDefault":true}, 
      //   "none".
      "group": {
        "kind": "build",
        "isDefault": true, // Defines if this task is the default task in the group.
      },
      // Configures the panel that is used to present the task's output and reads its input.
      "presentation": {
        // Controls whether the executed command is echoed to the panel. Default is true.
        "echo": true, // 打開可以看到編譯的命令蚪腋,把命令本身輸出一次
        // Controls whether the terminal running the task is revealed or not. Default is "always".
        //   always: Always reveals the terminal when this task is executed.
        //   silent: Only reveals the terminal if the task exits with an error or the problem matcher finds an error.(會(huì)顯示錯(cuò)誤,但不會(huì)顯示警告)
        //   never: Never reveals the terminal when this task is executed.
        "reveal": "silent", // 控制在集成終端中是否顯示姨蟋。如果沒問題那我不希望終端被切換屉凯、如果有問題我希望能看到編譯過程哪里出錯(cuò),所以選silent(可能always會(huì)好一些)
        // Controls whether the panel takes focus. Default is false.
        "focus": false, // 我的理解是:是否將鼠標(biāo)移過去眼溶。因?yàn)檫@個(gè)是編譯任務(wù)悠砚,我們不需要輸入什么東西,所以選false
        // Controls if the panel is shared between tasks, dedicated to this task or a new one is created on every run.
        "panel": "shared", // shared:不同任務(wù)的輸出使用同一個(gè)終端panel(為了少生成幾個(gè)panel我們選shared)
        // Controls whether to show the `Terminal will be reused by tasks, press any key to close it` message.
        "showReuseMessage": true, // 就一句話堂飞,你想看就true灌旧,不想看就false
        // Controls whether the terminal is cleared before executing the task.
        "clear": false, // 還是保留之前的task輸出信息比較好绑咱。所以不清理
      },
      // Other two choices: options & runOptions (cmd I to use IntelliSense)
      "options": {
        // The current working directory of the executed program or script. If omitted Code's current workspace root is used.
        "cwd": "${workspaceFolder}",// 默認(rèn)就是這個(gè),刪掉也沒問題
      },
      // problemMatcher: 用正則表達(dá)式提取g++的輸出中的錯(cuò)誤信息并將其顯示到VS Code下方的Problems窗口
      // check: https://code.visualstudio.com/docs/editor/tasks#_defining-a-problem-matcher
      "problemMatcher": {
        "owner": "cpp",
        "fileLocation": "absolute",
        "pattern": {
          "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
          "file": 1,
          "line": 2,
          "column": 3,
          "severity": 4,
          "message": 5,
        },
      },
      // 官網(wǎng)教程 https://code.visualstudio.com/docs/cpp/config-clang-mac#_build-helloworldcpp 
      // 提到了另一種problemMatcher枢泰,但試了之后好像不起作用描融,甚至還把我原本的電腦搞出了一些問題……
    },
    {
      "label": "Open Terminal.app",
      "type": "shell",
      "command": "osascript -e 'tell application \"Terminal\"\ndo script \"echo now VS Code is able to open Terminal.app\"\nend tell'",
      "problemMatcher": [],
      "group": "none",
    }
  ]
}

3.launch.json

這個(gè)是無注釋的文件

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "cppdbg",
            "request": "launch",
            "name": "C++ Debug",
            "preLaunchTask": "g++ compile",
            "program": "${fileDirname}/${fileBasenameNoExtension}.out", 
            "args": [],
            "environment": [],
            "cwd": "${workspaceFolder}",
            "stopAtEntry": false,
            "externalConsole": true,
            "MIMode": "lldb"
        }
    ]
}

這個(gè)是帶注釋的文件,但是可能由于編輯原因衡蚂,直接復(fù)制這個(gè)可能會(huì)報(bào)錯(cuò)窿克,需要自己手動(dòng)更改,這里僅作注釋參考

{
  // One of the key features of Visual Studio Code is its great debugging support.
  // VS Code's built-in debugger helps accelerate your edit, compile and debug loop.
  // VS Code keeps debugging configuration information in a launch.json file
  // located in a .vscode folder in your workspace (project root folder).
  "version": "0.2.0",
  "configurations": [
    {
      /* ------ these three options are mandatory ------ */
      // The type of debugger to use for this launch configuration.
      "type": "cppdbg", // C++ debug
      // The request type of this launch configuration. Currently, launch and attach are supported.
      //   If you come from a server or desktop background, 
      //   it's quite normal to have your editor launch your process for you, 
      //   and your editor automatically attaches its debugger to the newly launched process.
      //   A launch configuration starts your app in debug mode before VS Code attaches to it.
      // 大概意思是說毛甲,如果你開始debug的時(shí)候你的項(xiàng)目已經(jīng)啟起來了年叮,那就attach(把debug的工具附加上去)
      // 如果你開始debug的時(shí)機(jī)和你啟動(dòng)項(xiàng)目的時(shí)機(jī)是相同的,那就launch
      "request": "launch", // debug的類型玻募,launch表示啟動(dòng)只损,attach表示附加
      // The reader-friendly name to appear in the Debug launch configuration drop-down.
      "name": "C++ Debug", // 在VSCode側(cè)邊欄Run那里看到的名字(可以隨便起)
      /* ------ some optional attributes available to all launch configurations ------ */
      // To launch a task before the start of a debug session, set this attribute to the label of a task specified in tasks.json.
      "preLaunchTask": "g++ compile", //在調(diào)試之前要進(jìn)行的工作 compile是在 tasks.json 的編譯任務(wù)里面的label
      /* ------ Many debuggers support some of the following attributes: ------ */
      // executable or file to run when launching the debugger
      // !补箍!不要在程序和代碼的路徑及文件名中出現(xiàn)空格8闹础!否則無法調(diào)試(我嘗試解決這個(gè)問題坑雅,但真的無法解決)
      "program": "${fileDirname}/${fileBasenameNoExtension}.out", // debug的對(duì)象(-g編譯出來的二進(jìn)制文件)辈挂,需要和.vscode/tasks.json中生成的可執(zhí)行文件一致
      // arguments passed to the program to debug
      "args": [], // 比如運(yùn)行你的程序添加輸入?yún)?shù)(argc/argv),需要在這里添加
      // Environment variables to add to the environment for the program
      "environment": [], // 放置環(huán)境變量
      // current working directory for finding dependencies and other files
      "cwd": "${workspaceFolder}",
      // break immediately when the program launches
      "stopAtEntry": false,
      // If true, a console is launched for the debuggee.
      // If false, on Linux and Windows, it will appear in the Integrated Console.
      "externalConsole": true,
      // 為true則會(huì)打開系統(tǒng)終端在其中進(jìn)行交互
      // 如果為 true裹粤,則為調(diào)試對(duì)象啟動(dòng)控制臺(tái)终蒂。如果為 false,它在 Linux 和 Windows 上會(huì)顯示在集成控制臺(tái)中
      // macOS不適用:https://code.visualstudio.com/docs/cpp/launch-json-reference#_externalconsole
      /* ------ Customizing GDB or LLDB ------ */
      // Indicates the debugger that VS Code will connect to. Must be set to gdb or lldb. 
      // 但是macOS只安裝了llbd(有可能是安裝命令行工具的時(shí)候安裝的)遥诉,那就用lldb吧
      "MIMode": "lldb",
    }
  ]
}

直接F5開始調(diào)試(如果你設(shè)置了多個(gè)launch可能需要在邊欄Run哪里勾選相應(yīng)的)

這里可能遇到一個(gè)問題拇泣,終端沒有任何輸出。這是什么原因呢矮锈?霉翔??

GitHub issue|VS Code因權(quán)限無法打開系統(tǒng)終端(大概1/2的地方)
回到tasks.json苞笨,快捷鍵??P調(diào)出VSCode的命令窗口债朵,搜索task,點(diǎn)擊Tasks: Run Task瀑凝,選擇Open Terminal.app序芦;給VS Code打開終端的權(quán)限。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末粤咪,一起剝皮案震驚了整個(gè)濱河市谚中,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖宪塔,帶你破解...
    沈念sama閱讀 221,695評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件磁奖,死亡現(xiàn)場離奇詭異,居然都是意外死亡蝌麸,警方通過查閱死者的電腦和手機(jī)点寥,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來来吩,“玉大人敢辩,你說我怎么就攤上這事〉芙” “怎么了戚长?”我有些...
    開封第一講書人閱讀 168,130評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長怠苔。 經(jīng)常有香客問我同廉,道長,這世上最難降的妖魔是什么柑司? 我笑而不...
    開封第一講書人閱讀 59,648評(píng)論 1 297
  • 正文 為了忘掉前任迫肖,我火速辦了婚禮,結(jié)果婚禮上攒驰,老公的妹妹穿的比我還像新娘蟆湖。我一直安慰自己,他們只是感情好玻粪,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評(píng)論 6 397
  • 文/花漫 我一把揭開白布隅津。 她就那樣靜靜地躺著,像睡著了一般劲室。 火紅的嫁衣襯著肌膚如雪伦仍。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,268評(píng)論 1 309
  • 那天很洋,我揣著相機(jī)與錄音充蓝,去河邊找鬼。 笑死喉磁,一個(gè)胖子當(dāng)著我的面吹牛棺克,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播线定,決...
    沈念sama閱讀 40,835評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼确买!你這毒婦竟也來了斤讥?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,740評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎芭商,沒想到半個(gè)月后派草,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,286評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡铛楣,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評(píng)論 3 340
  • 正文 我和宋清朗相戀三年近迁,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片簸州。...
    茶點(diǎn)故事閱讀 40,505評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡鉴竭,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出岸浑,到底是詐尸還是另有隱情搏存,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布矢洲,位于F島的核電站璧眠,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏读虏。R本人自食惡果不足惜责静,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望盖桥。 院中可真熱鬧灾螃,春花似錦、人聲如沸葱轩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽靴拱。三九已至垃喊,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間袜炕,已是汗流浹背本谜。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評(píng)論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留偎窘,地道東北人乌助。 一個(gè)月前我還...
    沈念sama閱讀 48,921評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像陌知,于是被迫代替她去往敵國和親他托。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容