GarrickW
GarrickW

Reputation: 2241

Cannot open output file, permission denied

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.

Upvotes: 88

Views: 412041

Answers (19)

ᴇɴᴅᴇʀᴍᴀɴ
ᴇɴᴅᴇʀᴍᴀɴ

Reputation: 384

For Windows 10/11 Users:

I had the exact same problem. 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!

Upvotes: 0

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.

Upvotes: 1

afr0ck
afr0ck

Reputation: 63

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!

Upvotes: 1

chetan h
chetan h

Reputation: 1

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.

Upvotes: 0

Laiebi
Laiebi

Reputation: 61

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!

Upvotes: 6

Ahmad Asmndr
Ahmad Asmndr

Reputation: 355

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

Upvotes: 1

Wolfgang Adamec
Wolfgang Adamec

Reputation: 8544

In my case - I found a process called

cb_console_runner

I stopped this process and things were ok again.

Upvotes: 1

EternalWulf
EternalWulf

Reputation: 773

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.

Upvotes: 1

Efreeto
Efreeto

Reputation: 2271

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.

Upvotes: 1

Firefish
Firefish

Reputation: 61

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.

Upvotes: 5

Karan
Karan

Reputation: 11

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.

Upvotes: 1

Jorgesys
Jorgesys

Reputation: 126563

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.

Upvotes: 4

user2776580
user2776580

Reputation: 11

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 :)

Upvotes: 1

user2203361
user2203361

Reputation: 41

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"

Upvotes: 4

Namratha
Namratha

Reputation: 16790

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

Upvotes: 2

Sam
Sam

Reputation: 171

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.

Upvotes: 17

willll
willll

Reputation: 1839

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.

Upvotes: 139

Chris Card
Chris Card

Reputation: 3266

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

Upvotes: 1

user195488
user195488

Reputation:

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.

Upvotes: 5

Related Questions