5

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"
}
M.M
  • 138,810
  • 21
  • 208
  • 365
Zlushiie
  • 51
  • 1
  • 1
  • 3

8 Answers8

3

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"
}
Community
  • 1
  • 1
2

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

Jaykumar
  • 137
  • 2
  • 12
1

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.

agagelis
  • 31
  • 1
  • 8
0

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

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"
}
hth_zard
  • 29
  • 5
0

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.
0

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

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Shawn Aug 14 '23 at 08:28
0

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.

Divinity
  • 93
  • 2
  • 7