Certainly! Below is an example of a launch.json file configured to work in both Windows and Linux environments using the Microsoft C/C++ extension for VSCode. This file assumes that you have a simple HelloWorld.cpp file and have already created the necessary tasks.json to build the project.
Here is the launch.json configuration:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out", // For Linux
//"program": "${workspaceFolder}/HelloWorld.exe", // For Windows alternative
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false, // Set to true if you want an external console
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb", // For Linux, change it to full path of gdb if necessary
//"miDebuggerPath": "C:/path/to/gdb.exe", // For Windows, make sure to set this appropriately
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build", // Ensure this matches the label in your tasks.json
"windows": {
"program": "${workspaceFolder}/HelloWorld.exe",
"miDebuggerPath": "C:/path/to/gdb.exe" // Ensure 'gdb.exe' path is correct
},
"linux": {
"program": "${workspaceFolder}/a.out",
"miDebuggerPath": "/usr/bin/gdb"
}
}
]
}
Explanation
version: Specifies the version of the configuration schema.
configurations: Contains various configurations for debugging.
name: A friendly name to identify the configuration.
type: The type of configurations, here it is cppdbg for C++ debugging.
request: The type of request, here it's a launch request to start the program.
program: Path to the executable to be debugged. You will see options for both Linux (a.out) and Windows (HelloWorld.exe). The correct one will automatically be selected based on the platform.
args: Command line arguments passed to your program.
stopAtEntry: Whether to break at the entry point of the program.
cwd: Current working directory of the program being debugged.
environment: Environment variables for the debugging session.
externalConsole: Whether to use an external console for the program.
MIMode: Specifies the debugger to use (gdb for this configuration).
miDebuggerPath: Path to the gdb executable.
setupCommands: Commands to be set up before the debugging session.
preLaunchTask: The task to be executed before starting the debugging session, typically the build task.
windows and linux: Platform-specific settings to differentiate between Windows and Linux environments.
Setting Up tasks.json
Just to ensure everything is clear, your tasks.json should have corresponding tasks for building your C++ code:
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"HelloWorld.cpp",
"-o",
"${workspaceFolder}/a.out" // For Linux
// "-o",
// "${workspaceFolder}/HelloWorld.exe" // For Windows alternative
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [
"$gcc"
],
"windows": {
"args": [
"HelloWorld.cpp",
"-o",
"${workspaceFolder}/HelloWorld.exe"
]
},
"linux": {
"args": [
"HelloWorld.cpp",
"-o",
"${workspaceFolder}/a.out"
]
}
}
]
}
Building and Running
- Build the project using the default build task (Ctrl+Shift+B).
- Start debugging by pressing F5 or selecting the desired configuration from the debug menu.
This multi-platform configuration should help you develop and debug your C++ programs with ease on both Windows and Linux platforms. If you need any modifications or further customization, just let me know!