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

  1. Open VSCode and Your Project Folder: Ensure your project is open in VSCode, and the .vscode folder contains the tasks.json file you set up.

  2. Open the Command Palette:

    • Press Ctrl+Shift+P to open the Command Palette.

  3. 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.json file with a basic configuration.

  4. Edit the launch.json File:

    • VSCode will now open the newly created launch.json file 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 be cppdbg, 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 your tasks.json that 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

  1. Save the launch.json file. Now, whenever you start debugging (by pressing F5), VSCode will automatically build your project using the task defined in tasks.json and then launch the debugger according to the settings in launch.json.

  2. Run Debugging:

    • Press F5 to start the debugging session. VSCode will first execute the preLaunchTask (which builds your code) and then start debugging the compiled program.

Summary

  • Auto-creation: VSCode can auto-generate a launch.json that references an existing tasks.json.

  • Reference: The preLaunchTask in launch.json is automatically set to the appropriate task label in tasks.json.

  • Customization: You can further customize the launch.json to 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