How to Setup VS Code for Debugging C++ on Mac

Published on Mar 06, 2022 by Arun Michael Dsouza

How to Setup VS Code for Debugging C++ on Mac

VS Code has gained a lot of traction over the past few years due to its ease of use and extensibility. It offers a wide variety of plugins to use and customize workflows for any language or technology.

One of the most powerful features of VS Code is its debugger. It has Node JS runtime debugging support which can be used to debug JavaScript, TypeScript and other languages that can be transpiled to JavaScript. For debugging languages other than JavaScript, their corresponding debugging extension needs to be installed in VS Code.

In this blog post, we will be looking at how we can set up VS Code to debug C++ on Mac. The steps are pretty much the same for other operating systems but they have been covered in separate blog posts that you can check out below -

Setting up VS Code for C++ Development

Since C++ is a compiled language, what we first need is a compiler. The compiler will compile the code and execute it, then we can attach a debugger to it and start debugging the code that is being executed.

If you already have the Xcode command line tools on your system then you should have clang, clang++, gcc or the g++ compiler installed. You can open up a terminal window and use these commands to verify if a version of these compilers is installed -

clang++ --version
g++ --version
clang --version
gcc --version

If no compiler is installed then you can install the Xcode command line tools using this command -

xcode-select --install

Alternatively, you can use this link - https://developer.apple.com/download/all/

Note: In this blog post, we will be using the clang++ compiler.

Now that we have a C++ compiler installed, the next step would be to install the C/C++ Extension for VS Code which offers intellisense and debugging support. Once installed restart VS Code.

VS Code C/C++ Extension
VS Code C/C++ Extension

Creating a C++ Debug Configuration

Open the C++ source file that you want to debug in VS Code. To start the debugging process we need to create a debug configuration that will help us in debugging and executing C++. Go to the Run and Debug tab and click on the Run and Debug button, this will open up the environment selection menu. Or you can press F5 alternatively.

VS Code supports the LLDB and GDB debuggers on macOS, so click on the C++ (GDB/LLDB) menu item.

Selecting a Debug Environment
Selecting a Debug Environment

This will open up another menu to select the compiler configuration with a list of supported compilers in the list.

Selecting a Compiler Configuration
Selecting a Compiler Configuration

Selecting a compiler will create the debug configuration and save it inside the .vscode directory with two files -

launch.json

 1{
 2  "version": "0.2.0",
 3  "configurations": [
 4    {
 5      "name": "clang++ - Build and debug active file",
 6      "type": "cppdbg",
 7      "request": "launch",
 8      "program": "${fileDirname}/${fileBasenameNoExtension}",
 9      "args": [],
10      "stopAtEntry": false,
11      "cwd": "${fileDirname}",
12      "environment": [],
13      "externalConsole": false,
14      "MIMode": "lldb",
15      "preLaunchTask": "C/C++: clang++ build active file"
16    }
17  ]
18}

And tasks.json

 1{
 2  "tasks": [
 3    {
 4      "type": "cppbuild",
 5      "label": "C/C++: clang++ build active file",
 6      "command": "/usr/bin/clang++",
 7      "args": ["-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"],
 8      "options": {
 9        "cwd": "${fileDirname}"
10      },
11      "problemMatcher": ["$gcc"],
12      "group": {
13        "kind": "build",
14        "isDefault": true
15      },
16      "detail": "Task generated by Debugger."
17    }
18  ],
19  "version": "2.0.0"
20}

It will also compile the currently opened C++ source file, just make sure that the file is open while selecting the compiler. You will also notice that if you have a breakpoint set, the debug process will stop on it.

C++ Breakpoint Being Hit
C++ Breakpoint Being Hit

Tip: If you want to begin debugging right when the program starts, you can set the stopAtEntry property in your launch.json file to true. This will stop the program on the main() function.

So there you have it! You can now start building and debugging C++ on Mac with VS Code. If you want to set it up for a different platform you can check out the following blog posts -

If you have any questions or suggestions, please leave a comment down below. Also if you want to stay up to date with the latest happenings at example.com, feel free to follow me on Twitter.

Cheers!

Arun Michael Dsouza

Arun Michael Dsouza

Lorem ipsum dolor, sit amet consectetur adipisicing elit. Veniam recusandae consequatur necessitatibus modi nostrum ullam.