Programming Ansi C in Windows isn’t as straight forward such as doing
it in linux. But some people just can’t use Linux on their PC because
they require other software they need. The solution? MinGW. It’s
packaged with GCC, GDB, and a few other useful programs. Along with
MinGW, you can install MSYS which adds commands such as
The issue though? Whenever you compile your program on windows, you
either get an error such as
cannot open output file a.exe: Permission
denied or an error similar to
make: *** [a.exe] Error 1. All of the
answers online you have tried thus far don’t work, and you believe you
are out of luck. The good news, I can help you, since I finally figured
it out for myself.
It can be one of the following issues. Mine was the last issue, but I’ll get the more common issues out of the way first.
Program is still Running
If your compiled executable is still running, MinGW and GCC won’t be able to write to the file. Make sure you open up your task manager and make sure it’s still not running in the background. Also, you will want to use a program such as Process Explorer to make sure no other programs have control of the program. For example. a lot of IDE’s will launch the program and keep a handle to it until you force it to stop.
MinGW Doesn’t have Write Access
This is a simple error to test. Delete your previous
EXE file and
recompile it. If it compiles without issue, you are not experiencing
this issue and can move on. If you still cannot compile and are getting
permission errors, you have to change folder permissions. Right click
on the folder and make sure “Everyone” or at least your specific user
can write, modify, read files and folders inside that directory.
Your Anti-virus Software Blocks It
I use Comodo and whenever I tried to compile, it just wouldn’t. Disable your anti-virus software (and check task manager to make sure it’s truly disabled / turned off) and compile again. If it compiles without any issues, add everything in C:\mingw\ into your allowed software / exceptions inside of your anti-virus software. If you don’t want to add every file, you can mostly add the items in the bin folders. For Comodo, you can also disable HIPS instead of adding everything in Mingw to exceptions.
The reason Anti-virus software causes the issue is because they don’t allow software to modify exe’s. I believe that gcc doesn’t delete the file and recreate. GCC just opens the exe and re-writes the file, which throws some red flags to your AV. For me, I was never notified of my anti-virus blocking any actions since it did it all in the background, and just prevented the writing and nothing else. It didn’t open the file or anything, so it was hard to find.
Hopefully this will help you guys out, and if it doesn’t, below are some more resources on how you could possibly resolve this issue.