Skip to content

Commit 2603e06

Browse files
committed
Added debug related materials.
1 parent df39da5 commit 2603e06

File tree

2 files changed

+129
-21
lines changed

2 files changed

+129
-21
lines changed

Section22-Bonus_Section-Using_Visual_Studio_Code/readme.md

+129-21
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,25 @@
77

88
## Installing Extensions
99

10-
1. C/C++
11-
p.s. This [link](https://code.visualstudio.com/docs/languages/cpp#_install-the-extension) could be more useful.
10+
1. C/C++ --- p.s. This [link](https://code.visualstudio.com/docs/languages/cpp#_install-the-extension) could be more useful.
1211
- Launch VSCode
1312
- Select the Extensions view icon, bottom, on the Activity bar, on the far left
14-
- Search for `c++`
15-
- Select the one supported by Microsoft (purple circle) and `install`
13+
- Search for ***c++***
14+
- Select the one supported by Microsoft (purple circle) and **install**
1615
2. CodeLLDB
17-
- Search for `codelldb`
18-
- Select the only one supported by Vadim Chugunov and `install`
16+
- Search for ***codelldb***
17+
- Select the only one supported by Vadim Chugunov and **install**
1918

2019
# Building and Running C++ Programs with VSCode on Mac OSX
2120

2221
## Configure json files
2322
- Launch VSCode
24-
- Select `Open Folder`
23+
- Select **Open Folder**
2524
- Select the folder where you want to edit and compile your C++ codes
2625
- Copy below codes and save file as `main.cpp` for example
2726

27+
### main.cpp
28+
2829
```c++
2930
#include <iostream>
3031
using namespace std;
@@ -35,36 +36,101 @@ int main() {
3536
}
3637
```
3738

38-
- Select `View` at the Mac menu Bar, then `Command Palatte...`
39+
- Select **View** at the Mac menu Bar, then **Command Palatte...**
3940
- Select `C/C++: Edit Configurations (UI)`
4041
- Modify **Compiler path** by clicking on the drop down arrow and select `/usr/bin/g++`
4142
- Modify **C++ standard** to `c++20`
4243
- Save... Notice `.vscode` > `c_cpp_properties.json` is automatically generated on the Activity bar
4344

45+
### c_cpp_properties.json
46+
47+
```json
48+
{
49+
"configurations": [
50+
{
51+
"name": "Mac",
52+
"includePath": [
53+
"${workspaceFolder}/**"
54+
],
55+
"defines": [],
56+
"macFrameworkPath": [],
57+
"compilerPath": "/usr/bin/g++",
58+
"cStandard": "gnu17",
59+
"cppStandard": "c++20",
60+
"intelliSenseMode": "macos-gcc-x64"
61+
}
62+
],
63+
"version": 4
64+
}
65+
```
66+
4467
VSCode require the `.cpp` file to be selected prior building it
4568

4669
- Select the `main.cpp` just created
47-
- Select `Terminal` at the Mac menu Bar, then `Configure Default Build Task...`
70+
- Select **Terminal** at the Mac menu Bar, then **Configure Default Build Task...**
4871
- Select `C/C++: g++ build active file`
4972
- A new file, `tasks.json`, is automatically generated, which contains information on how to build C++ projects
5073
- Modify line 8 - 13 as below:
5174

75+
### tasks.json
5276
```json
53-
"args": [
54-
"-g",
55-
"-Wall", // to generate all warnings
56-
"-std=c++20", // use c++20 standard
57-
// "${file}",
58-
"${fileDirname}/*.cpp", // compile all c++ files
59-
"-o",
60-
"${fileDirname}/${fileBasenameNoExtension}"
61-
],
77+
{
78+
"version": "2.0.0",
79+
"tasks": [
80+
{
81+
"type": "cppbuild",
82+
"label": "C/C++: g++ build active file",
83+
"command": "/usr/bin/g++",
84+
"args": [
85+
"-g",
86+
"-Wall",
87+
"-std=c++20",
88+
"${fileDirname}/*.cpp",
89+
"-o",
90+
"${fileDirname}/${fileBasenameNoExtension}"
91+
],
92+
"options": {
93+
"cwd": "${fileDirname}"
94+
},
95+
"problemMatcher": [
96+
"$gcc"
97+
],
98+
"group": "build",
99+
"detail": "compiler: /usr/bin/g++"
100+
},
101+
{
102+
"type": "cppbuild",
103+
"label": "C/C++: g++ build active file",
104+
"command": "/usr/bin/g++",
105+
"args": [
106+
"-g",
107+
"-Wall", // to generate all warnings
108+
"-std=c++20", // use c++20 standard
109+
// "${file}",
110+
"${fileDirname}/*.cpp", // compile all c++ files
111+
"-o",
112+
"${fileDirname}/${fileBasenameNoExtension}"
113+
],
114+
"options": {
115+
"cwd": "${fileDirname}"
116+
},
117+
"problemMatcher": [
118+
"$gcc"
119+
],
120+
"group": {
121+
"kind": "build",
122+
"isDefault": true
123+
},
124+
"detail": "compiler: /usr/bin/g++"
125+
}
126+
]
127+
}
62128
```
63129

64130
## Building C++ programs
65131

66132
- Select `main.cpp` and let's compile the code
67-
- Select `Terminal` at the Mac menu bar, then `Run Build Task...`
133+
- Select **Terminal** at the Mac menu bar, then **Run Build Task...**
68134
- Terminal should automatically appear towards the bottom and expected output
69135

70136
```c++
@@ -81,7 +147,7 @@ Terminal will be reused by tasks, press any key to close it.
81147
82148
## Running C++ Programs
83149
84-
- Right click on the `main` executable file and select `Open in Integrated Terminal`
150+
- Right click on the `main` executable file and select **Open in Integrated Terminal**
85151
- The terminal will then open at the proper file path, and input command `ls` will list the directories under the file path. The expected output is
86152
87153
```
@@ -98,4 +164,46 @@ clang: error: no such file or directory: 'test2.cpp'
98164
clang: error: no input files
99165
```
100166
101-
Despite the `.cpp` file name is different, I learned that there will be buile errors if `tes1.cpp` and `test2.cpp` are in the same directory during build.
167+
Despite the `.cpp` file name is different, I learned that there will be buile errors if `tes1.cpp` and `test2.cpp` are in the same directory during build.
168+
169+
# Debugging C++ Programs with VSCode on Mac OSXing
170+
- Select `main.cpp`
171+
- Select **Run** at the Mac menu bar, then **Add Configuration...**
172+
- Select `C++ (GDB/LLDB)`
173+
- Select `g++ - Build and debug active file`
174+
- Exit out of debugger by selecting red sqaure box
175+
- Notice `launch.json` is automatically created
176+
- Modify `launch.json` as below
177+
178+
### launch.json
179+
180+
```json
181+
{
182+
// Use IntelliSense to learn about possible attributes.
183+
// Hover to view descriptions of existing attributes.
184+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
185+
"version": "0.2.0",
186+
"configurations": [
187+
{
188+
"name": "g++ - Build and debug active file",
189+
// "type": "cppdbg",
190+
"type": "lldb",
191+
"request": "launch",
192+
"program": "${fileDirname}/${fileBasenameNoExtension}",
193+
"args": [],
194+
// "stopAtEntry": false,
195+
"cwd": "${fileDirname}",
196+
// "environment": [],
197+
// "externalConsole": false,
198+
// "MIMode": "lldb",
199+
"preLaunchTask": "C/C++: g++ build active file"
200+
}
201+
]
202+
}
203+
```
204+
205+
- Return to `main.cpp`
206+
- Begin debugging by selecting **Run and Debug** on the Activity bar
207+
- Select breakpoints with red dot next to line number
208+
- Select green triangle play button towards top left
209+
- Step through debugger with the pop up menu
Binary file not shown.

0 commit comments

Comments
 (0)