0
votes

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!!

3
try to find it using cmd commend tasklist, and kill it using taskkill - Olexy
looks like collect2.exe is locked by the OS. Google "Windows find locked file" or something. There are programs you can use which will tell you what files are using what files so you can end the proper task. - Andy
@Andy When I search n the Process Explorer when i search for collect2.exe i get 0 matching results. - Michael DeAngelis
Don't use process explorer, use LockHunter -- it integrates into Windows Explorer. You just right-click on it and it will tell you what is locking it. If it comes up with nothing, manually delete the file and try again. - Andy
This part of your command doesn't look right "c:\Users\imaco\Desktop\Cpppractice"main There's a backslash missing between Cpppractice and main. - john

3 Answers

0
votes

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.

-1
votes

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

(assuming the terminal opens up in your working directory)

-1
votes

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.