Visual Studio Code

Build & Debug C++

安裝 extension C/C++ (ms-vscode.cpptools)。

Build

ctrl + ship + p 輸入 tasks,選擇 Configure Task,選個 template 來改。或者直接在 .vscode/ 下新增 tasks.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
// 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": "make",
"args": [
],
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": [
"$gcc"
]
}
]
}

這是有 Makefile 的設定方式,command 也可以用 g++ 配合 args

Debug

左邊切到 Debug 按上面的齒輪可以設定 launch.json,也可以直接在 .vscode/ 下新增設定檔。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{
// 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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out", // 指定執行檔
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build" // debug 前要執行的 task, 對應 tasks.json 的 taskName
}
]
}

設定好之後按 F5 就可以 Debug 囉~

breakpoint 只要在 code line number 左邊按出紅點點就可以啦~(g++ 要記得 -g

Extension

workspace 推薦 extension

.vscode/ 底下加入 extensions.json,可以在裡面列推薦跟不推薦的 extension。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
// See http://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp

// List of extensions which should be recommended for users of this workspace.
"recommendations": [
"ms-vscode.cpptools",
"donjayamanne.githistory"
],
// List of extensions recommended by VS Code that should not be recommended for users of this workspace.
"unwantedRecommendations": [

]
}

Git History

ctrl + shift + p 後打 git h 可以看 git log、file 跟 line history。

選一個 commit 點下面的修改檔案還可以看 diff。

Sublime Text Keymap and Settings Importer

如果習慣 sublime 的 keymap 可以用這個 extension,其他還有 Visual Studio、Eclipse 等等的 keymap。

Troubleshooting

檔案太多 vscode 無法 watch changes

這裡說可以修改 /etc/sysctl.conf,加上:

1
fs.inotify.max_user_watches=524288

然後下 sudo sysctl -p load 進設定。

也可以設定 vscode 的 files.watcherExclude 來 exclude 一些不想 watch changes 的 folder。

Ref