-
Notifications
You must be signed in to change notification settings - Fork 323
C and Cpp Debugging in VS Code
First, install your compiler.
Then, go to the extensions panel in VS Code and search for C/C++
. Choose the extension by Microsoft (ms-vscode.cpptools).
Install that extension and restart VSC.
Currently, this build process only works for MacOS and Linux. It is unknown if it will work on Windows 10 with WSL. See below for additional instructions on compiling in Windows 7 with Cygwin.
What we want to be able to do is build the project with CTRL-SHIFT-B (or COMMAND-SHIFT-B).
The tasks.json
file controls which tasks VSC can run. The one we're interested in is the Default Build Task.
- With VSC open to your root project directory, hit CTRL-SHIFT-P (or COMMAND-SHIFT-P).
- Type
tasks
and choose "Tasks: Configure Default Build Task". - Choose "Create tasks.json file from template".
- Choose "Others".
At this point you should be editing tasks.json
.
- Change the
label
to something representative, likeBuild
- Change the
command
to the command you want to run, likegcc -Wall -o foo foo.c
ormake
- Hit CTRL-SHIFT-P (or COMMAND-SHIFT-P) again.
- Type
default
and choose "Tasks: Configure Default Build Task" again. - Select the value you put in for
label
in step 1. - Save
tasks.json
.
At this point, your tasks.json
should look something like this:
{
// 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",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Now you should be able to hit CTRL-SHIFT-B (or COMMAND-SHIFT-B) and see a build.
Adding a Problem Matcher will automatically extract error information from the build and put it in the PROBLEMS panel to make it easier to jump to those line numbers.
Here is a tasks.json
file with a problemMatcher
for C/C++ added in:
{
// 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",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceFolder}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
]
}
tasks.json
will be found in the .vscode/
directory in the project workspace.
We need to complete a few more setup items to be able to compile using the Cygwin Bash terminal. We will be able to build programs, but the above build shortcut and problem matcher will not work. Please let us know if you have found a workaround!
- We need to install two additional packages for Cygwin. Open cmd.exe as an administrator (not the Cygwin terminal).
- Navigate to the directory where you saved the installer.
- Run the following command:
setup-x86_64.exe -q -P chere -P gdb
.- If this step is not performed, the bash terminal will crash on open in VS Code.
- Press
Ctrl + Shift + P
and type inOpen User Settings
. Click on the result. This will open a file calledUser Settings
- Add the following lines to your settings. Note that items in this object are comma separated.
"terminal.integrated.shell.windows": "C:\\cygwin64\\bin\\bash.exe", "terminal.integrated.shellArgs.windows": ["/bin/xhere","/bin/bash"]
- Verify that there are no errors indicated by squiggles, then save and close the file.
These steps are necessary for each new project you work on:
- Open your project in VS Code. If you don't have another one yet, use the project linked above to make sure this works.
- Open a file with an
#include
statement at the top. Open \vscdebugc\hello.c if you are using the example project. - The lines with
#include
statements will have green squiggles underneath them. Click on one, then click on the lightbulb that appears on the left side. - Select
Edit "includePath" Settings
. This will create and open a file calledc_cpp_properties.json
. - Scroll to the
"Win32"
section. Under"includePath"
AND the"path"
part of"browse"
, add the following:
"C:/cygwin64/lib/gcc/x86_64-pc-cygwin/6.4.0/include", "C:/cygwin64/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/*", "C:/cygwin64/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/tr1/", "C:/cygwin64/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/tr2/", "C:/cygwin64/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/", "C:/cygwin64/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/x86_64-pc-cygwin/",
- Verify that this has worked by returning to
hello.c
.- The squiggles under any
#include
s should be gone. - Start to type
printf()
. If Intellisense is working, the function should pop up.
- The squiggles under any
To compile and run your program, you cannot use the Ctrl + Shift + B
shortcut. Instead:
- Open the terminal (View -> Integrated Terminal).
- Navigate to the folder containing your project.
- To get to your Windows directory from bash, type
cd cygdrive/c/Users/YourUserName
- To get to your Windows directory from bash, type
- If there is a makefile set up, you can navigate to the location with that file and simply enter the command
make
.- In the demo, this is located in
/CS-Wiki/vscdebugc
- In the demo, this is located in
- Otherwise, enter
gcc -Wall -Wextra -o foo foo.c
- In the demo, this command would be
gcc -Wall -Wextra -o hello hello.c
.
- In the demo, this command would be
- Other than a slight hitch in the terminal, it may appear that nothing has happened when compiling a small program. If this is the first time compiling, foo.exe will appear in the Explorer.
- It can be useful to keep a window open so that you can easily see the last updated time and verify that the build completed.
- To run your new program, type
./foo.exe
in the terminal.
IMPORTANT: You must build your C code with the -g
switch to gcc
or clang
. This causes debugging information to be included in the binary.
To set up debugging, perform the following steps:
-
Open the debugging panel (with the "anti-bug" symbol on the left).
-
Where it says "No Configurations on the top, pull down that menu.
-
Choose "Add Configuration".
-
Choose C++
-
It'll pop up an editor for
launch.json
. Edit thename
to be something you like, e.g. "My Server" -
Change
program
to be the name of your binary from the build step, e.g."${workspaceFolder}/myserver"
-
Change
miDebuggerPath
to the location of gdb.exe. This will be somewhere in your compiler folder. -
If you are using
fork()
, it might be helpful to modify GDB'sdetach-on-fork
setting tooff
insetupCommands
.text
, for example:"text": "-enable-pretty-printing -gdb-set detach-on-fork off",
-
Save
launch.json
.
Example launch.json
:
{
// 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": "Hello world",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/hello",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "/path/to/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb and add better child debugging",
"text": "-enable-pretty-printing -gdb-set detach-on-fork off",
"ignoreFailures": true
}
]
}
]
}
launch.json
will be found in the .vscode/
directory in the project workspace.
Follow the instructions on this page.
Note that if <stdio.h>
fails to highlight, you might need to add a final /*
to the last line in the includePath
section:
"${localappdata}/Packages/CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc/LocalState/rootfs/usr/include/*"
- Perform a build.
- In the editor for the file you wish to debug, click just left of the line number where you want a breakpoint. A red dot will appear.
- In the debugger panel, click the green run arrow at the top, or select
Debug
→Start Debugging
, or hitF5
. - The run should break on the line with the breakpoint.
If the run doesn't break on the line you specified you probably didn't compile with the -g
switch for gcc
or clang
. Make sure to rebuild.
The debugging panel will show local variables in the VARIABLES
pane. You can add expressions to watch in the WATCH
pane, e.g.
node->next->val
If you have a char a[1000]
that you want to view as a string instead of an array, add this to the WATCH
section:
(char *)a
Console input and output will be in another window that pops up. It might hide behind the VSCode window, so look for it there.
Once you've hit the breakpoint, you can do one of these main things:
-
Continue:
F5
or green arrow. Keep running to the next breakpoint or the end. -
Step Over:
F10
or "jump over" icon. Execute this line and go to the next without entering any functions. -
Step Into:
F11
or "down arrow" icon. If this line is a function call, step into that function. -
Step Out:
SHIFT-F11
or "up arrow" icon. Continue running until you get out of this function.
See the "Debug" pulldown menu for all options.
Happy hacking!