Autogenerating Launch.json file
It looks like you have the basic structure of a launch.json file, but it currently doesn't have any configurations. Let’s walk through the steps to automatically generate a configuration in launch.json that references your existing tasks.json.
Step 1: Auto-generate a launch.json Configuration
launch.json ConfigurationOpen VSCode and Your Project Folder: Ensure your project is open in VSCode, and the
.vscodefolder contains thetasks.jsonfile you set up.Open the Command Palette:
Press
Ctrl+Shift+Pto open the Command Palette.
Add a Debug Configuration:
In the Command Palette, type "Add Configuration" and select "C++ (GDB/LLDB)" from the list. This will automatically generate a new
launch.jsonfile with a basic configuration.
Edit the
launch.jsonFile:VSCode will now open the newly created
launch.jsonfile with a configuration that might look something like this:
jsonCopy code{ "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${fileDirname}/${fileBasenameNoExtension}", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "build", // Automatically references the "build" task from tasks.json "miDebuggerPath": "/usr/bin/gdb", "logging": { "trace": true, "traceResponse": true, "engineLogging": true } } ] }
Step 2: Review and Customize the Configuration
name: The name of the debug configuration. This is how it will appear in the debug menu.type: This should becppdbg, which is the debugger type for C++ (GDB/LLDB).request: Set to"launch"to start a new debugging session.program: Automatically references the output program to be debugged (e.g.,${fileDirname}/${fileBasenameNoExtension}).preLaunchTask: The name of the task from yourtasks.jsonthat should be run before starting the debugger. If you named your build task"build"(as in the example), it will automatically reference that task.
Step 3: Save and Use
Save the
launch.jsonfile. Now, whenever you start debugging (by pressingF5), VSCode will automatically build your project using the task defined intasks.jsonand then launch the debugger according to the settings inlaunch.json.Run Debugging:
Press
F5to start the debugging session. VSCode will first execute thepreLaunchTask(which builds your code) and then start debugging the compiled program.
Summary
Auto-creation: VSCode can auto-generate a
launch.jsonthat references an existingtasks.json.Reference: The
preLaunchTaskinlaunch.jsonis automatically set to the appropriate task label intasks.json.Customization: You can further customize the
launch.jsonto suit your debugging needs, including setting program arguments, environment variables, and more.
This setup ensures that your project is built and debugged with the correct configurations automatically.
Last updated