Sublime Forum

Collect2.exe: error: ld returned 1 exit status

#1

#include <stdio.h>

int main(){
printf(“Hello, World!\n”);
return 0;
}

The above code, i build successfully in sublime Text.
[Finished in 1.9s]

#include <stdio.h>
#include <string.h>

int main()
{
char key]=“apple\n”;
char input[80];
do{
printf(“Guess my favorite fruit?”);
fgets(input,80, stdin);
}while(strcmp(key,input)!=0);
puts(“Good, Correct Answer”);

return 0;

}
The above code i cannot compile, the error is below:
c:/mingw/64/bin/…/lib/gcc/x86_64-w64-mingw32/4.8.1/…/…/…/…/x86_64-w64-mingw32/bin/ld.exe: cannot open output file C:\MyCode/Apple.exe: Permission denied
collect2.exe: error: ld returned 1 exit status
[Finished in 1.5s]
When i compile cplus plus code, i also cannot compile, pls help.

0 Likes

#2

Is there some reason the linker wouldn’t be able to create “C:\MyCode/Apple.exe”? Possibilities to consider might be:

  • c:\MyCode doesn’t exist
  • permissions problems (as mentioned in the error)
  • interference from antivirus programs
0 Likes

#3

Question:
Why HelloWorld in the same directory works?

  • c:\MyCode doesn’t exist -> Yes, it exist.
  • permissions problems (as mentioned in the error) ->it works for Helloworld
  • interference from antivirus programs ->

it works for Helloworld? i run the code in msys
g++ apple.c
./a
it works, but not in Sublime !!!

Tools>Build System>New System
How to overwrite default New System in Sublime
What is the command to run in
{
“shell_cmd”: “make”
}

0 Likes

#4

The command g++ apple.c won’t try to overwrite “apple.exe” since it creates “a.exe”.

See if running “make” (which is what the Sublime project is configured to do) from the command line gives you the same error or not.

If it doesn’t produce the same error, then you might need to use some other debugging tool like SysInternal’s Process Monitor to get an idea of why the build process launched from Sublime doesn’t have permission to overwrite “apple.exe”.

0 Likes

#5

Even I am facing the same problem. Can you please help me to solve the problem?

0 Likes

#6

When this happens, the most likely culprit is that the program that you built is already running, and because Windows locks files that are in use, attempts to re-link it fail because the existing executable is locked and thus the linker is not allowed to touch it. So the solution is, kill the running program (you may need to use the task manager to do this).

This question has come up innumerable times on Stack Overflow and the reason is always due to trying to run a program that gathers input. The program listed above does this, for example.

In short, that doesn’t work; Sublime doesn’t forward any input that you type into the output panel to your running program, so it’s effectively hung forever in the background waiting for input you can’t provide. Then you try to rebuild but the linker fails because it can’t delete the existing executable because it’s already running.

The solution there is either don’t execute (console) programs from Sublime that need to take interactive input, or if you do use a build system that will first open a command prompt window and execute your program in that; then you can interact with it directly.

1 Like