84

So I keep getting this error when trying to compile C++ code using CodeBlocks.

cannot open output file [filename.exe] permission denied

It only started today, and it's sporadic and inconsistent. It usually goes away if I shut CodeBlocks down and restart the project, but not always. Sometimes it even goes away when I just press F9 (build & run) several times. I always check Task Manager, but there are never any .EXEs running with my file's name, or anything related to it.

I've also noticed that if this problem occurs and I then try to delete the .EXE manually or otherwise interact with it, Windows tells me I need administrator permission to do so (this is a private PC, and as far as I know I am an administrator on it).

The first time it occurred, the only thing I had added to the code beyond what was in the previous version was a debugging report sent to cout - hardly anything arcane. It sometimes occurs even when no code was changed from a previous version.

What could this be? It's not game-breaking, yet, but it's irritating and I'd rather it didn't get worse.

EDIT: This is old by now, but just in case anyone else is having similar problems, the workaround I currently use is just to consistently delete [filename.exe] before trying to run the code again. This avoids the problem, though it is annoying.

GarrickW
  • 2,181
  • 5
  • 31
  • 38
  • When I try to delete the executable file, it errors because it is still running in the background. The way I fix it is to find the task in Task Manager and then end the task. – ᴇɴᴅᴇʀᴍᴀɴ Aug 05 '23 at 18:26

20 Answers20

136

check that "filename.exe" is not running, I guess you are using Microsoft Windows, in that case you can use either Task Manager or Process Explorer : http://technet.microsoft.com/en-us/sysinternals/bb896653 to kill "filename.exe" before trying to generate it.

willll
  • 1,809
  • 1
  • 16
  • 23
  • 6
    It doesn't have to be running, another process could have an open handle on it, Process Explorer is the weapon of choice for that sort of work – Gearoid Murphy Jul 29 '11 at 15:38
17

I have encountered the same problem you have. I found that it may have some relationship with the way you terminate your run result. When you run your code, whether it has a printout, the debugger will call the console which print a "Press any key to continue...". If you terminate the console by pressing key, it's ok; if you do it by click the close button, the problem comes as you described. When you terminate it in the latter way, you have to wait several minutes before you can rebuild your code.

Rndm
  • 6,710
  • 7
  • 39
  • 58
Sam
  • 171
  • 1
  • 3
6

I just had the same issue. It always happens when I run the program and change some code when the program is still running. After that, the "permission denied" message appears.

I fixed it by clicking the "Terminate" button at the very top-right side of the console window (red button) and then the "remove all terminated launches" (double X right next to the terminate button). This closes the running program. Everything worked fine afterwards. Hope this helps somebody!

Laiebi
  • 61
  • 1
  • 1
5

I was having the same problem too and it was driving me crazy:

Windows7 64 bit, MinGW, Eclipse, CDT

Permission denied errors, executable disappearing, ld.exe errors. It would work once, but never again. Statements like: "cannot open output file ***.exe: Permission denied"

Check your ANTIVIRUS Software. Once I turned off my Antivirus software in workspace then cleaned the workspace and started the project over again I never had the problem again.

Firefish
  • 61
  • 1
  • 2
4

This error usually occurs when the IDE has a problem due to a crash or other failure and it still has a hold on the EXE, preventing the user (yourself) from overwriting / deleting the EXE during a rebuild.

3

Hello I realize this post is old, but here is my opinion anyway. This error arises when you close the console output window using the close icon instead of pressing "any key to continue"

3

The problem is related to Sam´s response:

"have encountered the same problem you have. I found that it may have some relationship with the way you terminate your run result. When you run your code, whether it has a printout, the debugger will call the console which print a "Press any key to continue...". If you terminate the console by pressing key, it's ok; if you do it by click the close button, the problem comes as you described. When you terminate it in the latter way, you have to wait several minutes before you can rebuild your code."

Avoid kill processes, and we have two choices, wait until the process release the .EXE file or this problem will be solved faster restarting the IDE.

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – AstroCB Sep 25 '14 at 20:19
  • My answer is: "Avoid kill processes, and we have two choices, wait until the process release the .EXE file or this problem will be solved faster restarting the IDE." read again... – Jorgesys Sep 25 '14 at 20:58
  • @Elanasys I'm sorry if I misunderstood your answer. This was flagged as "very low quality," and I arrived here from the review queues. You may want to consider expanding on your answer a bit because, as it stands, it depends on Sam's answer for full clarity. Either way, a downvote on one of my answers is not warranted. – AstroCB Sep 25 '14 at 21:14
  • Also consider that your "answer" would serve better as a comment on Sam's post, for as it stands, it does not appear to directly answer the question. – AstroCB Sep 25 '14 at 23:16
2

Try restarting your IDE. It worked for me. Although I tried to end the process in the task manager, the process never got killed.

Namratha
  • 16,630
  • 27
  • 90
  • 125
2

I had the same Problem. Just rename your .CPP file to other name and try it again. It worked for me.

Ahmad Asmndr
  • 187
  • 1
  • 7
1

I re-installed C::B in drive D, whereas my program files folder is in drive C

I don't know the reason , but it works :)

Tyler Jandreau
  • 4,245
  • 1
  • 22
  • 47
1

The problem is that you don't have the administrator rights to access it as running or compilation of something is being done in the basic C drive. To eliminate this problem, run the devcpp.exe as an administrator. You could also change the permission from properties and allowing access read write modify etc for the system and by the system.

demongolem
  • 9,474
  • 36
  • 90
  • 105
Karan
  • 11
  • 1
1

Once I had the same problem, and after tracking down the process that was getting the file in use, I discovered that it was the my anti-virus program (PANDA). I just unlocked the file and it worked out. I used Unlock 1.9.2 try it out!

afr0ck
  • 65
  • 6
1

Make sure to run 7zip in 'Administrator mode' for extracting to Program Files.

Also, temporarily turning off virus protection worked for some people in the past.

Efreeto
  • 2,132
  • 1
  • 24
  • 25
1

A major cause of this (which I had recently), is if you have this on for example a flash drive.

You can develop and do everything, but on most systems it stops you from running the .exe file from there, whether it be the debug or release version.

EternalWulf
  • 742
  • 9
  • 21
1

I tried what @willll said, and it worked. I didn't find exactly the executable named after my project, but I did kill some weird looking tasks (after checking on the internet they were not critical), and it worked.

J3STER
  • 1,027
  • 2
  • 11
  • 28
  • In my case the problem happened when I was running a programme in CLion IDE. I was in the middle of a programme that was running, and I closed the output window instead of pressing any key to end it. I tried most of the suggestions above and none worked for me. To solve it, I went into the cmake-build-debug folder in my project folder, and I deleted the projectname.exe (projectname.exe here means the name of your file that you are running) file. I restarted my computer and everything was now working fine. – aye decoder Oct 03 '22 at 17:15
1

I ran the file after ending the IDE from Task Manager and cleaning all temporary files from mydrive. I started my IDE and the file runs properly.

1

In my case - I found a process called

cb_console_runner

I stopped this process and things were ok again.

Wolfgang Adamec
  • 8,374
  • 12
  • 50
  • 74
1

You can use process explorer from sysinternals to find which process has a file open.

Chris Card
  • 3,216
  • 20
  • 15
0

For Linux Users:

Go to the directory where you created your file. Right click on the file, if you are unable to change permission then open the Terminal (Command Prompt) to change the permission.

First go to that executable file using the 'cd' command then type "chmod u+x" to change the file permission. Then open Code:Blocks and execute the file. It should work after that.

0

For Windows 10/11 Users:

I had the exact same problem. For me, what worked was scrolling through all the tasks running in Task Manager, finding the .exe file that has the same name as the .cpp file you are trying to build and run, and then ending the task. Then I built and ran my .cpp file and everything worked fine.

I noticed that this problem arose when I clicked the red 'X' button on the Code:Blocks menu. To prevent this, you should either close the console window the regular way or run the program until it says "Press any key to continue." These are the safest ways to end the program so the executable is not still running in the background.

Hope this helps!