Sure! Creating a launch.json file in Visual Studio Code (VSCode) will allow you to easily configure the debugging settings for your C++ program. Below is a sample launch.json file tailored for a cross-platform setup (Windows and Linux), assuming you are using the Microsoft C/C++ extension.
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/HelloWorld.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"console": "externalTerminal",
"preLaunchTask": "build" // Assuming "build" is the label defined in tasks.json for building the project
},
{
"name": "(Linux) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/HelloWorld",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build", // Assuming "build" is the label defined in tasks.json for building the project
"miDebuggerPath": "/usr/bin/gdb" // Adjust if gdb is in a different location
}
]
}
Explanation:
-
Windows Configuration:
- type:
"cppvsdbg" specifies the debugger type for Visual Studio's C++ debugger on Windows.
- program:
${workspaceFolder}/HelloWorld.exe points to the built executable file.
- args: An array for any command-line arguments you wish to provide (empty if none).
- stopAtEntry:
false indicates not to stop at the program's entry point.
- cwd:
${workspaceFolder} sets the working directory to the workspace folder.
- console:
"externalTerminal" to use an external terminal for the program's input/output.
- preLaunchTask:
"build" specifies the task to run before launching, assuming you have a corresponding build task in tasks.json.
-
Linux Configuration:
- type:
"cppdbg" specifies the debugger type for GDB on Linux.
- program:
${workspaceFolder}/HelloWorld points to the built executable file.
- args: An array for any command-line arguments you wish to provide (empty if none).
- stopAtEntry:
false indicates not to stop at the program's entry point.
- cwd:
${workspaceFolder} sets the working directory to the workspace folder.
- externalConsole:
false uses the built-in VSCode terminal.
- MIMode:
"gdb" specifies that GDB should be used for debugging.
- setupCommands: Includes a command to enable pretty-printing in GDB.
- preLaunchTask:
"build" specifies the task to run before launching, assuming you have a corresponding build task in tasks.json.
- miDebuggerPath: Path to the GDB executable.
Steps to follow:
- Ensure your
HelloWorld.cpp program is correctly placed in your workspace folder.
- Confirm your
tasks.json is correctly set up for building the .cpp file into an executable.
- Place the above
launch.json in the .vscode folder within your workspace.
- Configure your
tasks.json if not already done to include a build task similar to:
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"HelloWorld.cpp",
"-o",
"HelloWorld"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"]
}
]
}
Note: Adjust "command": "g++" to "command": "cl" or other compiler specifics if you’re on Windows and using the MSVC (Microsoft Visual C++) toolchain.
Running and Debugging:
- Open VSCode, and make sure your workspace is open.
- Build your project using
Ctrl+Shift+B (default for build).
- Use
F5 to start debugging, choosing the appropriate configuration for your OS when prompted.
With this setup, you have a streamlined process for building, running, and debugging your C++ project in VSCode on both Windows and Linux platforms.