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)限。