Certainly! To set up a launch.json for debugging your HelloWorld.cpp in Visual Studio Code (VSCode) on both Windows and Linux, you need to include configuration for both platforms. The following launch.json file assumes that you are using the Microsoft C/C++ extension and provides settings for both Windows (with msvc or MinGW) and Linux (with g++).
Here's an example launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/HelloWorld.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"preLaunchTask": "build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"windows": {
"MIMode": "gdb"
}
},
{
"name": "(Linux) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/HelloWorld",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"preLaunchTask": "build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"miDebuggerPath": "/usr/bin/gdb",
"linux": {
"MIMode": "gdb"
},
"sourceFileMap": {
"/mnt/c": "c:\\",
"/usr": "usr"
}
}
]
}
Explanation
-
Windows Configuration:
"type": "cppvsdbg": This uses the Visual Studio Windows Debugger.
"program": "${workspaceFolder}/HelloWorld.exe": Path to the executable, assuming that it is built in the workspace folder.
"preLaunchTask": "build": This refers to the build task you have defined in tasks.json.
-
Linux Configuration:
"type": "cppdbg": This uses the GDB debugger for Linux.
"program": "${workspaceFolder}/HelloWorld": Path to the executable for Linux.
"preLaunchTask": "build": This also refers to the build task defined in tasks.json.
"miDebuggerPath": "/usr/bin/gdb": Path to the GDB debugger on Linux.
Assumptions
- Your project and tasks setup builds the executable named
HelloWorld on Linux and HelloWorld.exe on Windows.
- The
build task is defined in your tasks.json to build the HelloWorld executable.
Additional Configuration
- Adjust
miDebuggerPath if GDB is installed in a different location.
- You can customize
"args", "environment", and other options as necessary for your particular setup.
Place launch.json inside the .vscode folder in your workspace. This configuration will allow you to easily debug your HelloWorld.cpp on both Windows and Linux platforms using VSCode.