Running and debugging#
Typing each time commands to the terminal to compile and run our program is not necessary. Many IDEs feature a shortcut like F5 which combines compilation and inspection of our program. The latter is a natural process of program development, because we engineers make errors and introduce so called bugs into our program.
- debugging
the process of finding the root cause, workarounds, and possible fixes for bugs.
- bug
a design defect in an engineered system
In this section we will install and configure extension CodeLLDB, which uses the LLDB debugger that is part of the clang project. Then we will create a debug configuration that will be used upon F5.
Installing CodeLLDB#
Install
CodeLLDB
extension from the authorvadimcn
in the editor.Click on
Restart Extensions
Creating a launch configuration#
Warning
If the following steps do not work, skip these and create launch.json
by creating a new file and copy pasting the content at the end of this section.
Details here.
So let us add a configuration which will describe what happens if we press F5.
Go back to
main.c
tab.Press F5. You should see a popup window stating
Cannot start debugging because no launch configuration has been provided.
.Click
OK
. Alaunch.json
will be created in your project folder under.vscode
folder. It will contain the configuration documented here.configurations
contains a configuration namedDebug
. Our executable program should be assigned toprogram
.Now substitute the name of your program for
<executable file>
. If you cannot remember it, take a peek to the previous section where we compiled and run our program.We want to work in an edit, debug cycle, however our launch configuration is not aware of the fact that our program must be compiled before running and debugging. So add the following line to your configuration so that the default build task is executed before debug:
"preLaunchTask": "${defaultBuildTask}",
In overall, your launch.json
should look like this without the comments on the top:
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/a.exe",
"args": [],
"cwd": "${workspaceFolder}",
"preLaunchTask": "${defaultBuildTask}",
}
]
}
Removing running from our build & run task#
Our default task builds and runs our program. At the same time debugging takes care of running our program also. Running is not required anymore in our build task, so let us remove it:
.vscode/tasks.json
without the default comment lines and modified lines:
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "clang main.c",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
In the next section, we will try our configuration. You don’t have to save launch.json
. Project files are typically saved when you press F5.
Starting a debug session#
Click on the tab
main.c
.Press F5.
Run and debug
bar will be activated. Additionally, you should see some output in theDebug console
below as follows:... Process exited with code 42.
You see the same number we saw earlier.
Now you can edit your code and run & debug it again and again in cycle.
Debugger can also inspect our code by placing red breakpoints, which we will introduce later. For now, we will use the debugging feature for a convenient way of compiling and running our code.