Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

C++ refuses to run on vs code, with the error "The preLaunchTask 'C/C++: g++.exe build active file' terminated with exit code -1."

$
0
0

So I thought that I would learn a little bit of C++ on my own (after all, how hard could it be?). I followed this guide on VS code website to set install the MinGW-W64 toolchain:

https://code.visualstudio.com/docs/cpp/config-mingw, 

After checking in a command prompt, the install was successful.

Even though I did that, when I try to run/debug any kind of code in vs code, I get the said:

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

I know the code is not at fault, for I am literally pasting it from W3schools.com. I don't know if it is important, but I am pasting the code, just in case it helps:

\#include \<iostream\>using namespace std;int main() {cout \<\< "Hello World!";return 0;}

When I run the code, the terminal does this:

Starting build...C:\\msys64\\ucrt64\\bin\\g++.exe -fdiagnostics-color=always -g D:\\Lea\\lol.cpp -o D:\\Lea\\lol.exeC:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe:    cannot open output file D:\\Lea\\lol.exe: Permission deniedcollect2.exe: error: ld returned 1 exit statusBuild finished with error(s).* The terminal process terminated with exit code: -1.* Terminal will be reused by tasks, press any key to close it.

It also creates a .vscode folder, in which there is a tasks.json, inside of it the code looks like this:

{"tasks": [        {"type": "cppbuild","label": "C/C++: g++.exe build active file","command": "C:\\msys64\\ucrt64\\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."        }    ],"version": "2.0.0"}

There is no launch.json inside my .vscode folder

And lastly, a lol.exe file is created, (with the same name my program is having), which if I try to run, it closes on itself after half a second.

After seeing this problem, I searched for it in google. I found that a person had the almost same problem as me here:

https://stackoverflow.com/questions/64519454/error-the-prelaunchtask-c-c-g-exe-build-active-file-terminated-with-exi

the problem is that their exit code is not -1, but 1. Even though the slight difference in our problems, I heeded to the advice of the most upvoted comment, which stated that you should delete the ".vscode" folder and right after try to debug, which didn't work at all. (resulted in the same error message)

Closing vs code and reopening it didn't yield me any good results, either.

After searching for this problem in stackoverflow instead, I found a question which has the problem I am looking for:

https://stackoverflow.com/questions/67064643/trying-to-debug-c-through-vscode

The only comment here is that inside the "tasks.json", you should set the "preLaunchTask to ${defaultBuildTask}."

Except I found no such thing inside my tasks.json as a "preLaunchTask"

though I did find that inside my "tasks.json", the "type" is "cppbuild". After changing that to "shell", (because why the hell not at this point), my error code changed to something like 'C/C++: g++.exe build active file' terminated with exit code 1." (this is only speculation, though) AWESOME, I thought!

inside the terminal, it looks like this:

*  Executing task: C:\msys64\ucrt64\bin\g++.exe -fdiagnostics-color=always -g D:\Lea\lol.cpp -o D:\Lea\lol.exe C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot open output file D:\Lea\lol.exe: Permission deniedcollect2.exe: error: ld returned 1 exit status*  The terminal process "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -Command C:\msys64\ucrt64\bin\g++.exe -fdiagnostics-color=always -g D:\Lea\lol.cpp -o D:\Lea\lol.exe" terminated with exit code: 1. *  Terminal will be reused by tasks, press any key to close it. 

After trying to run it a second time, to actually get a better glimpse at the new error code, it did the normal thing of reverting back to the earlier problem. cool. (so now the error code is back again to -1) (except the difference that it's now executing the gcc.exe instead of the g++.exe)

Terminal:*  Executing task: C/C++: gcc.exe build active file Starting build...C:\msys64\ucrt64\bin\gcc.exe -fdiagnostics-color=always -g D:\Lea\lol.cpp -o D:\Lea\lol.exeC:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot open output file D:\Lea\lol.exe: Permission deniedcollect2.exe: error: ld returned 1 exit statusBuild finished with error(s).*  The terminal process terminated with exit code: -1. *  Terminal will be reused by tasks, press any key to close it.

After changing the debug configuration to (Windows) Launch (or it could have been (gdb) launch, I was too happy that something changed that my brainfish (meant goldfish here but brainfish sounds so funny i won't correct it) memory forgot.), my tasks.json file changed, and the launch.json file came to be!

tasks.json:

{"tasks": [        {"type": "shell","label": "C/C++: g++.exe build active file","command": "C:\\msys64\\ucrt64\\bin\\g++.exe","args": ["-fdiagnostics-color=always","-g","${file}","-o","${fileDirname}\\${fileBasenameNoExtension}.exe"            ],"options": {"cwd": "${fileDirname}"            },"problemMatcher": ["$gcc"            ],"group": "build","detail": "Task generated by Debugger."        },        {"type": "cppbuild","label": "C/C++: gcc.exe build active file","command": "C:\\msys64\\ucrt64\\bin\\gcc.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."        }    ],"version": "2.0.0"    }

launch.json:

{"configurations": [        {"name": "(Windows) Launch","type": "cppvsdbg","request": "launch","program": "enter program name, for example ${workspaceFolder}/a.exe","args": [],"stopAtEntry": false,"cwd": "${fileDirname}","environment": [],"console": "externalTerminal"        },        {"name": "C/C++: gcc.exe build and debug active file","type": "cppdbg","request": "launch","program": "${fileDirname}\\${fileBasenameNoExtension}.exe","args": [],"stopAtEntry": false,"cwd": "${fileDirname}","environment": [],"externalConsole": false,"MIMode": "gdb","miDebuggerPath": "C:\\msys64\\ucrt64\\bin\\gdb.exe","setupCommands": [                {"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true                },                {"description": "Set Disassembly Flavor to Intel","text": "-gdb-set disassembly-flavor intel","ignoreFailures": true                }            ],"preLaunchTask": "C/C++: gcc.exe build active file"        },        {"name": "C/C++: g++.exe build and debug active file","type": "cppdbg","request": "launch","program": "${fileDirname}\\${fileBasenameNoExtension}.exe","args": [],"stopAtEntry": false,"cwd": "${fileDirname}","environment": [],"externalConsole": false,"MIMode": "gdb","miDebuggerPath": "C:\\msys64\\ucrt64\\bin\\gdb.exe","setupCommands": [                {"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true                },                {"description": "Set Disassembly Flavor to Intel","text": "-gdb-set disassembly-flavor intel","ignoreFailures": true                }            ],"preLaunchTask": "C/C++: g++.exe build active file"        },        {"name": "(gdb) Launch","type": "cppdbg","request": "launch","program": "enter program name, for example ${workspaceFolder}/a.exe","args": [],"stopAtEntry": false,"cwd": "${fileDirname}","environment": [],"externalConsole": false,"MIMode": "gdb","miDebuggerPath": "/path/to/gdb","setupCommands": [                {"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true                },                {"description": "Set Disassembly Flavor to Intel","text": "-gdb-set disassembly-flavor intel","ignoreFailures": true                }            ]        }    ],"version": "2.0.0"    }

Even though this happened, the exit code error still stayed at -1.

After acquiring the launch.json, I immidietly went ahead and changed all of the preLaunchTasks to "${defaultBuildTask}", and it rightfully didn't change anything.

For my next attempt, I once again changed the tasks.json "type" to "shell", which gave me back the exit code 1 instead of the -1. My new error is:

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

After trying to run it again, to see if the exit code would change back to -1, when it asked me which debug configuration i would like to use, instead of giving me 4 options, it now gave me 10.

All of my options, from top to bottom are:

  1. C/C++: gcc.exe build and debug active file prelaunchTask:C/C++: gcc.exe build active file Detected task (compiler: C:\msys64\ucrt64\bin\gcc.exe)
  2. C/C++: g++.exe build and debug active file prelaunchTask:C/C++: g++.exe build active file Detected task (compiler: C:\msys64\ucrt64\bin\g++.exe)
  3. C/C++: gcc.exe build and debug active file preLaunchTask: $(defaultBuildTask)
  4. C/C++: g++.exe build and debug active file preLaunchTask: $(defaultBuildTask)
  5. (gdb) Launch
  6. C/C++: gcc.exe build and debug active file preLaunchTask: $(defaultBuildTask)
  7. C/C++: g++.exe build and debug active file preLaunchTask: $(defaultBuildTask)
  8. (gdb) Launch
  9. (Windows) Launch
  10. (Windows) Launch

It seems that the error code now consistently stays as 1, opposed to it changing back to -1 (if I select the options that start with C/C++)

For not having any ideas, I tried to delete the auto generated .vscode folder, because my error code now matches their error code, thus it might fix my stuff: It instead gave me back the -1 code.

Restoring back the folder from the garbage can worked, so I am not ultimately screwed. (the error code is now back to 1), however I have no fricking clue what to do next.

Any help is appreciated.


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>