Visual Studio 2022: How to create a launch.json with a prelaunch task?
I am developing a C++ CMake project in Visual Studio. First off, I have started the project in Visual Studio Code, with the following folder structure:
root/
- CMakeLists.txt
- CMakePresets.json
- /apps
- CMakeLists.txt
- main.cpp
- /cpp-library
- CMakeLists.txt
- includes/
- class1.h
- [other .h classes]
- src/
- class1.cpp
- [other .cpp classes]
- /srcDataFiles
- test.txt
In Visual Studio Code, I use a launch.json and tasks.json in order to run the file. Specifically, I have it setup so that whenever the "Start" button is pressed, the configuration inside launch.json runs. Inside launch.json, there is a preLaunchTask, which first builds the project (with all changes to the code) and runs the project after the preLaunchTask (after the build is finished).
Visual Studio Code launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}\\out\\build\\${command:cmake.activeConfigurePresetName}\\apps\\main.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": false,
"preLaunchTask": "CMake: build",
"logging": {
"moduleLoad": false,
"trace": true
},
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
Visual Studio Code tasks.json:
{
"tasks": [
{
"type": "cmake",
"label": "CMake: build",
"command": "build",
"targets": [
"all"
],
"preset": "${command:cmake.activeBuildPresetName}",
"group": "build",
"problemMatcher": [],
"detail": "CMake template build task",
}
],
"version": "2.0.0"
}
When the Start Item button is pressed, launch.json is ran with the cwd (current working directory) set to ${workspaceRoot}. Also, before the "program" is executed, a preLaunchTask named "CMake: build" is ran, which builds the project before it is ran.
When porting the same project to Visual Studio as an "Open Folder Project" (https://learn.microsoft.com/en-us/visualstudio/ide/develop-code-in-visual-studio-without-projects-or-solutions?view=vs-2022), you are forced to use launch.vs.json and tasks.vs.json inside the .vs root subdirectory. However, after looking at the documentation for launch.vs.json (https://learn.microsoft.com/en-us/cpp/build/launch-vs-schema-reference-cpp?source=recommendations&view=msvc-170), I see no field that would make it possible to configure a preLaunchTask in the same manner as is done in Visual Studio Code.
I have created the following launch.vs.json in Visual Studio:
{
"version": "0.2.1",
"defaults": {},
"configurations": [
{
"type": "default",
"project": "out\\build\\ConfigurePresetv1\\main.exe",
"projectTarget": "",
"name": "main.exe launch",
"currentDir": "${workspaceRoot}"
}
]
}
With the current setup of launch.vs.json, I have to manually click CTRL-SHIFT-B to build the project and afterwards select the main.exe launch Start Item to launch the code.
How can I modify launch.vs.json and tasks.vs.json to achieve the same behaviour is Visual Studio as I have in VS-Code (that is: how to define a preLaunchTask in launch.vs.json that runs before the project field is executed)?