[SOLVED] Error: the preLaunchTask build terminated with exit code -1, launch program does not exist

Question

This Content is from Stack Overflow. Question asked by Chalky

Recently I’ve been trying to move from CodeBlocks to Visual Studio Code, but the latter has been giving me problems especially when making projects with classes, in this case when trying to run it I get this window:

the preLaunchTask C/C++:g++.exe build active file terminated with exit code -1

Then after clicking debug anyway this appears:

launch program does not exist

It’s been a while since I’ve used VScode but I’m pretty sure that the launch program gets created when running the code of the program for the first time right?

Here are my launch.json and task.json:

launch.json:

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    {
        "name": "(Windows) Launch",
        "type": "cppvsdbg",
        "request": "launch",
        "program": "${workspaceFolder}/myfile.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": true
    },
    {
        "name": "(Windows) Attach",
        "type": "cppvsdbg",
        "request": "attach",
        "processId": "${command:pickProcess}"
    }
]

}

tasks.json:

{
"version": "2.0.0",
"tasks": [
    {
        "label": "build myfile",
        "type": "shell",
        "command": "g++",
        "args": [
            "-std=c++14",
            "-g",
            "-o",
            "myfile.exe",
            "myfile.cpp"
        ],
        "group": "build"
    },
    {
        "type": "cppbuild",
        "label": "C/C++: g++.exe build active file",
        "command": "C:\msys64\mingw64\bin\g++.exe",
        "args": [
            "-fdiagnostics-color=always",
            "-g",
            "${file}",
            "-o",
            "${fileDirname}\${fileBasenameNoExtension}.exe"
        ],
        "options": {
            "cwd": "${fileDirname}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "detail": "Task generated by Debugger."
    }
]

}

I got all the .json from this answer to a similar question: https://stackoverflow.com/a/50658089/19009898 I used to have different.json which I guess were the default ones but this change doesn’t seem to make any trouble for other previous code without classes that I’ve made so I’m guessing it’s ok.

Would appreciate the help.

Solution

Found a way in the spanish stack overflow.

In tasks.json I added this:

"label": "g++.exe build active file",
 "args": [
"-g",
"${fileDirname}\\**.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
],

Then run by either debuging or running the C/C++ file NOT just clicking run code and then it works perfectly.

Answered by Chalky


This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 4.0.

people found this article helpful. What about you?