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.
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 - CodeLLDBextension from the author- vadimcnin 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.ctab.
- Press F5. You should see a popup window stating - Cannot start debugging because no launch configuration has been provided..
- Click - OK. A- launch.jsonwill be created in your project folder under- .vscodefolder. It will contain the configuration documented here.
- configurationscontains a configuration named- Debug. Our executable program should be assigned to- program.- 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}", 
- You can remove - cwd(current working directory) line, because we will use the default setting.
After these changes, your launch.json should look like this without the comments on the top for Windows. Change a.exe to a.out for other operating systems.
{
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug",
            "program": "${workspaceFolder}/main",
            "args": [],
            "preLaunchTask": "${defaultBuildTask}",
        }
    ],
    "version": "0.2.0",
}
Debugging takes care of running our program. Our default build task builds our program without running. That is the reason why we specified ${defaultBuildTask}.
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 debugbar will be activated. Additionally, you should see some output in the- Debug consolebelow as follows:- ... Process exited with code 0. - And in the terminal the hello message. Troubleshooting- If you get an error, e.g., - ${defaultBuildTask} not found, check if you have CodeLLDB extension installed.
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.
