Problem to be addressed
On Linux it is pretty straightforward to set up the debugger for C++ on VSCode. The workflow is:
Write your code
Go to the [Run and Debug] VSCode section
Click on run active file or something similar
Debug your code
On macOS you will run into a lot of silly problems, such as:
Can't find
#include<bits/stdc++.h>
Installed g++ and got the debugger running, buck can't write input to
std::cin
in the integrated terminal, because it says
Build finished successfully.
* Terminal will be reused by tasks, press any key to close it.
and then closes the terminal, making it impossible to write to std::cin
.
After some time browsing the internet I found the solution for all those problems and got my setup working just like on linux.
Solution
Let's brake the solution step by step, from a almost brand new macOS.
Install g++ (
brew install gcc
)Download VSCode
Install C++ extension on VSCode
On VSCode type ⌘+shift+p to open up the command palette. Then write
C/C++ Edit Configurations (UI)
. A window will open up, then you must setCompiler path
to/opt/homebrew/bin/g++-14
(or whatever version is installed; you can see the files inside the dir/opt/homebrew/bin/
by runningls /opt/homebrew/bin/
on your terminal and from there you can figure it out yourself). Still on this window setC++ standard
toc++23
for latest features.Install extension CodeLLDB.
Okay, now let's say you created a /codeforces directory on your home, where you will run some c++ files. Inside this directory create a .vscode
directory if one does not exist already. Then create a tasks.json
file and put this code inside it:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++-14 build active file",
"command": "/opt/homebrew/bin/g++-14",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"$$${fileDirname}/$$${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
The only purpose of this task called "C/C++: g++-14 build active file" is to build the c++ code in the current active file (as the name suggests).
In the same .vscode
dir create another file called launch.json
and write this code inside it:
{
// 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": [
{
"type": "lldb",
"request": "launch",
"name": "Debug C++",
"program": "$$${fileDirname}/$$${fileBasenameNoExtension}",
"args": [],
"cwd": "${fileDirname}",
"preLaunchTask": "C/C++: g++-14 build active file",
}
]
}
This prelaunch task is necessary, so it compiles the code before running it, otherwise you will have to compile and then run the debugger every single time, which is annoying and error prone. Also mind you that the name of the preLaunchTask is exactly the same as the label of the task we created previously, so be mindful if you change names.
After all that setup you should be able to go to your [Run and Debug] VSCode section and choose Debug C++
, which will behave exactly like it did on linux.
If you have any questions feel free to ask!