'VIsual Studio Code error cannot open output file main.exe

This is my first project with Visual Studio Code. When i go to run my "Hello World" project I get this

cd "c:\Users\imaco\Desktop\Cpppractice" && g++ main.cpp -o main && "c:\Users\imaco\Desktop\Cpppractice"main c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot open output file main.exe: Permission denied collect2.exe: error: ld returned 1 exit status

my code reads

1    #include <iostream>
2
3
4
5     int main()
6  {
7   std:: cout << "Hello World!";
8   }

-I've made sure it isn't running in the background. -I've closed and reopened the program and ran the code again with same results. -I've allowed it as an exception in Bit Defender.

Any help would be greatly appreciated!!



Solution 1:[1]

I need 50 reputation to comment, otherwise I would have added a comment. I ran into this issue myself and I solved it using this. However, it worked for me:

"cannot open output file main.exe: Permission denied collect2.exe: error: ld returned 1 exit status"

It says that the permission has been denied. This probably means that you have not given execute access to visual studio in a certain folder or the file has execute restrictions. You could probably be storing this file in a folder that has restricted access. There are a couple of options:

  • You could probably move it to a new folder that does not have any execute access restrictions.
  • You may have have to change the permission of that folder/file to read, write and execute using admin privileges.

If this does not work, let me know in the comments section, because I faced the same issue of permission denied.

Solution 2:[2]

Try to open terminal (Ctrl + `) and type: g++ main.cpp then .\a.exe

(assuming the terminal opens up in your working directory)

Solution 3:[3]

This usually happens when VS decides to keep a chunk of code running as a separate entity in the background. A way to fix this is that since you are on Windows, I would right-click the taskbar and click on task manager. This will open up all the tasks that are currently running on the computer. Then, search for "Windows console host." You can change the sorting of the list. Once you find it you can click on it and then at the bottom right it will have an "End Task" available for you to click. Click that and then retry again.

You should be good after that, but let me know if you are still having issues.

Solution 4:[4]

when this occurs i usually just remove the exe file and build again

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Programming Rage
Solution 2 stackunderflow
Solution 3 Oscar Solis
Solution 4 Ammar