What You’ll Need
- Visual Studio Code
- C++ compiler (
clangfor Mac) - VS Code C++ extensions
Step 1 — Install Xcode Command Line Tools
Open Terminal and run:
xcode-select --install
Follow the prompt. When done, verify it worked:
clang++ --version
You should see something like Apple clang version 15.x.x.
Step 2 — Install Visual Studio Code
Download and install from: https://code.visualstudio.com
Step 3 — Install C++ Extensions in VS Code
- Open VS Code
- Go to the Extensions tab (left sidebar or
Cmd+Shift+X) - Search for and install:
- C/C++ by Microsoft
- C/C++ Extension Pack by Microsoft (includes the debugger and IntelliSense)
Step 4 — Create and Run a Simple C++ Program
- Create a new folder for your project (e.g.,
cpp-labs) - Open the folder in VS Code: File → Open Folder
- Create a new file:
hello.cpp
Paste the following:
#include <iostream>
int main() {
std::cout << "Hello, world!" << std::endl;
return 0;
}
- Open the Terminal in VS Code: Terminal → New Terminal
- Compile the code:
clang++ hello.cpp -o hello
- Run the program:
./hello
You should see: Hello, world!
Step 5 — Configure VS Code to Build with One Keystroke
Instead of typing compile commands every time, set up a VS Code build task.
Create the tasks file
- In VS Code, go to Terminal → Configure Default Build Task
- Select C/C++: clang++ build active file
VS Code will create a .vscode/tasks.json file. Replace its contents with this:
{
"version": "2.0.0",
"tasks": [
{
"label": "Build C++",
"type": "shell",
"command": "clang++",
"args": [
"-g",
"-std=c++17",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"]
}
]
}
Note: The -g flag includes debug information — required for the debugger to work. The -std=c++17 flag enables modern C++ features.
Now build any open .cpp file by pressing Cmd+Shift+B.
Step 6 — Set Up the Debugger
The debugger lets you pause your program mid-execution, inspect variables, and step through code line by line.
Create the launch configuration
- Click the Run and Debug icon in the left sidebar (or press
Cmd+Shift+D) - Click create a launch.json file
- Select C++ (GDB/LLDB), then clang++ — Build and debug active file
VS Code will create .vscode/launch.json. Replace its contents with:
{
"version": "0.2.0",
"configurations": [
{
"name": "Build and Debug",
"type": "lldb",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"cwd": "${fileDirname}",
"preLaunchTask": "Build C++"
}
]
}
Note: This uses lldb, the debugger that ships with Xcode on Mac. If you see an error about lldb, install the CodeLLDB extension from the VS Code marketplace.
Debugger Controls
| Action | How |
|---|---|
| Set a breakpoint | Click in the gutter (left of the line number) — a red dot appears |
| Start debugging | F5 or click the green play button |
| Step over a line | F10 — runs the line, stays at the same level |
| Step into a function | F11 — follows the call inside the function |
| Step out | Shift+F11 — finishes the function and returns |
| Continue to next breakpoint | F5 |
| Stop debugging | Shift+F5 |
| Inspect a variable | Hover over it while paused, or check the Variables panel |
Step 7 — Try It with a Larger Program
Here’s a student grade tracker with enough complexity to make debugging practice worthwhile. It uses functions, loops, vectors, and structs.
Create a new file called grades.cpp and paste this in:
#include <iostream>
#include <vector>
#include <string>
#include <numeric>
#include <algorithm>
#include <iomanip>
struct Student {
std::string name;
std::vector<double> scores;
};
double average(const std::vector<double>& scores) {
if (scores.empty()) return 0.0;
double total = std::accumulate(scores.begin(), scores.end(), 0.0);
return total / scores.size();
}
char letterGrade(double avg) {
if (avg >= 90) return 'A';
if (avg >= 80) return 'B';
if (avg >= 70) return 'C';
if (avg >= 60) return 'D';
return 'F';
}
double highest(const std::vector<double>& scores) {
return *std::max_element(scores.begin(), scores.end());
}
double lowest(const std::vector<double>& scores) {
return *std::min_element(scores.begin(), scores.end());
}
void printStudentReport(const Student& s) {
double avg = average(s.scores);
std::cout << "\nStudent: " << s.name << "\n";
std::cout << " Scores: ";
for (double score : s.scores) {
std::cout << std::setw(6) << score;
}
std::cout << "\n";
std::cout << std::fixed << std::setprecision(1);
std::cout << " Average: " << avg << "\n";
std::cout << " Highest: " << highest(s.scores) << "\n";
std::cout << " Lowest: " << lowest(s.scores) << "\n";
std::cout << " Grade: " << letterGrade(avg) << "\n";
}
void printClassSummary(const std::vector<Student>& students) {
std::cout << "\n========== Class Summary ==========\n";
double classTotal = 0.0;
std::string topStudent;
double topAvg = -1.0;
for (const Student& s : students) {
double avg = average(s.scores);
classTotal += avg;
if (avg > topAvg) {
topAvg = avg;
topStudent = s.name;
}
}
double classAvg = classTotal / students.size();
std::cout << std::fixed << std::setprecision(1);
std::cout << "Number of students: " << students.size() << "\n";
std::cout << "Class average: " << classAvg << " (" << letterGrade(classAvg) << ")\n";
std::cout << "Top student: " << topStudent << " (" << topAvg << ")\n";
int countA = 0, countB = 0, countC = 0, countD = 0, countF = 0;
for (const Student& s : students) {
char g = letterGrade(average(s.scores));
if (g == 'A') countA++;
else if (g == 'B') countB++;
else if (g == 'C') countC++;
else if (g == 'D') countD++;
else countF++;
}
std::cout << "\nGrade distribution:\n";
std::cout << " A: " << countA << "\n";
std::cout << " B: " << countB << "\n";
std::cout << " C: " << countC << "\n";
std::cout << " D: " << countD << "\n";
std::cout << " F: " << countF << "\n";
}
int main() {
std::vector<Student> roster = {
{"Alice", {92, 88, 95, 91, 84}},
{"Bob", {73, 65, 70, 68, 72}},
{"Carol", {85, 90, 88, 93, 96}},
{"David", {55, 60, 58, 62, 50}},
{"Eva", {78, 82, 79, 81, 76}},
{"Frank", {100, 98, 95, 99, 97}},
{"Grace", {40, 45, 38, 50, 42}},
};
std::cout << "========== Individual Reports ==========";
for (const Student& s : roster) {
printStudentReport(s);
}
printClassSummary(roster);
return 0;
}
Compile and run it
clang++ -g -std=c++17 grades.cpp -o grades
./grades
Things to try with the debugger
- Set a breakpoint on the
printStudentReportcall inside theforloop inmain. Press F5. The program pauses for each student — expand thesvariable in the Variables panel to see their name and scores. - Step into
average()from insideprintStudentReportusing F11. Watchtotalgrow asaccumulateruns. - Step over lines in
printClassSummaryand watchtopStudentandtopAvgupdate as each student is evaluated. - Hover over variables while paused — VS Code shows their current value inline.
Quick Reference
| Task | Shortcut |
|---|---|
| Build | Cmd+Shift+B |
| Start / continue debugging | F5 |
| Step over | F10 |
| Step into | F11 |
| Step out | Shift+F11 |
| Stop debugging | Shift+F5 |
| Toggle breakpoint | Click gutter or F9 |
| Open terminal | Ctrl+` |
Troubleshooting
“lldb not found” or debugger won’t start
Install the CodeLLDB extension in VS Code, then make sure launch.json has "type": "lldb".
“command not found: clang++”
Re-run xcode-select --install in Terminal and make sure the installation completed.
Breakpoints are grey (unverified)
Make sure you compiled with the -g flag. Without it, the debugger has no line information.
Changes to code don’t show up when debugging
Always rebuild (Cmd+Shift+B) before starting a new debug session, or use the Build and Debug launch config which rebuilds automatically.
