Eyzuky Eyzuky - 6 months ago 28
C Question

"Permission denied" after a compilation error occured

I am working on this piece of code, trying to learn basic C programming.
I managed to compile and run a basic program.

Then, when I try to run a code on which the compiler raises an error, the problem starts -- from then on I get:

c:/mingw/bin/../lib/gcc/mingw32/4.9.3/../../../../mingw32/bin/ld.exe: cannot open output file tests.exe: Permission denied
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [tests.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/tests.dir/all] Error 2

No matter what I change to code to, even to just a basic
int main() {return 0;}
code, I keep getting that problem.

Edit (integrated from comment):

Found the solution, I had the program still running and I had to manually stop it. I am used to python/java, where once you get an error the program stops. Should it not be like that in C as well?


I assume the error that triggers the linker failure later on is actually a runtime error, not a compiler one.

As you have found out, Windows cannot overwrite a file which is opened in a running program (or is the running program itself), hence the error when ld.exe (the linker) tries to create a new executable.

The most probable cause is that you are starting your program in debug mode (the green beetle icon) instead of a normal run (the green "play" arrow). This attaches a debugger (probably gdb) to your program. When your program crashes, the debugger stops it in its tracks and waits for you to investigate, thus keeping the process alive.

Make sure the debug tools are not hidden in some way, or just launch your program without a debugger when you want it to fail fast.