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

59.4k Views Asked by At

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"
}
10

There are 10 best solutions below

0
agagelis On

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.

2
Jaykumar On

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

1
AudioBubble On

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

0
Dasarathan Sampath On

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"
}
0
hth_zard On

in my case it was a problem with the location of the gcc.exe file in command location of the tasks.json file just change this and it should work fine.

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe build active file",
            "command": "C:\\Users\\hrish\\Documents\\gcc\\bin\\gcc.exe",/* here the file location was different */
            "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."
        }
    ],
    "version": "2.0.0"
}
0
Venkat Rahul On

In my case:

  • I deleted the contents in .vscode folder,
  • closed the remote connection,
  • closed and reopened VSCode,
  • clicked the Run c/c++ File on top right corner and the program executed successfully.
1
Sourav_Indi-student On

Just close all the terminals open in the VS code terminals tab in the lower part of the screen. (if many terminals are running together, it may not allow deleting the old exe file to create a new one.) check this photo for clarity

0
Divinity On

I had this issue recently and what fixed it was selecting g++ as my compiler rather than gcc

This is the image

The difference between gcc and g++ is that gcc was made for c programs but can run c++ programs while g++ was specifically made to run c++ programs.

To access the dropdown that would allow you switch compilers you'd have to delete the .vscode folder and then run your c++ file.

0
Awais On

You can delete the mingw directory and install the new one. Open this link https://sourceforge.net/projects/mingw-w64/files/mingw-w64/ and download the One with dwarf at its end

Open this. In my case this was the latest one, but in your case it may be same or different. Use winrar to extract the files. mingw32 now set the environment variables. VS Code should now work.

0
AR_Hasani On

I solved this issue by replacing "${file}" with "${workspaceFolder}\*.cpp" in the tasks.json file.