Error: "The preLaunchTask 'C/C++: g++.exe build active file' terminated with exit code 1"

Viewed 6250

enter image description here

I've read that this error is because of some settings in the launch.json and tasks.json files. So I deleted them and made new ones, but it gives the same error and wont build and debug. How do I fix it?

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": "g++.exe - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file"
        }
    ]
}

Tasks.json:

{
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ],
    "version": "2.0.0"
}
4 Answers

By default setting, Vs. code can compile only one C++ file at a time. So we need to tell the compiler to compile all the .cpp files in that project folder. You "tasks.json" file would be

{
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "-Wall", <====== to give all warning
                "-std=c++17", <======== specific version of the complier
                "${fileDirname}\\*.cpp", <======= Compile all CPP files
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ],
    "version": "2.0.0"
}

If you have previously compiled your code and the executable (*.exe) file is already created, delete it through the VS code file explorer, and try to build your code again. This was the problem, at least in my case. VS code could not delete the executable to create a new one.

In my case,

  1. Delete .vscode folder which is auto generated when 1st time debug
  2. Now again debug ( Press F5 or Top right Debug C/C++ File)

if any list come for compiler then choose 1st.

99% this will works. otherwise try for Online C++ compiler with debug option like

https://www.onlinegdb.com/online_c++_compiler

I had this problem once. It happens if there's a mistake in the "main()" name. For example:

If you write int mai() rather than main() it can result in such problem. If you provide your code, we can then further assess what can be the possible problem.

For additional help:

You can modify your launch.json file into this:

{ 
    //Use IntelliSense to understand related attributes. 
    //Hover to see descriptions of existing properties. 
    //For more information, please visit: https://go.microsoft.com/fwlink/?linkid=830387 
    "version" :  "0.2.0" , 
    "configurations" :  [ 
        { 
            "name" :  "g++. exe-Generate and debug activity files" , 
            "type" :  "cppdbg" , 
            "request" :  "launch" , 
            "program" :  "${fileDirname}\\${fileBasenameNoExtension}.exe" , 
            "args" :  [ ] , 
            "stopAtEntry" :  false ,
             
            "environment" :  [ ] , 
            "externalConsole" :  false , 
            "MIMode" :  "gdb" , 
            "miDebuggerPath" :  "F:\\VScode\\mingw64\\bin\\gdb.exe" , 
            "setupCommands" :  [ 
                { 
                    "description" :  "Enable neat printing for gdb" , 
                    "text" :  "-enable-pretty-printing" , 
                    "ignoreFailures" :  true 
                } 
            ] , 
            "preLaunchTask" :  "C/C++: g++.exe build active file"
        } 
    ] 
}

Change the miDebugger to your MinGW path and if problem still remains then can try changing externalConsole to true

Related