What does "collect2: error: ld returned 1 exit status" mean?

CCompiler Errors

C Problem Overview


I see the error collect2: error: ld returned 1 exit status very often. For example, I was executing the following snippet of code:

void main() {
  char i;

  printf("ENTER i");
  scanf("%c",&i);

  clrscr();

  switch(i) {
    default:
      printf("\nHi..\n");
      break;
    case 1:
      printf("\n\na");
      break;
    case 2:
      printf("\nb\n");
      break;
    case 3:
      printf("\nc");
      break;
  }
}

and I got this:

main.c:(.text+0x33): undefined reference to `clrscr'                       
collect2: error: ld returned 1 exit status 

What does it mean?

C Solutions


Solution 1 - C

The ld returned 1 exit status error is the consequence of previous errors. In your example there is an earlier error - undefined reference to 'clrscr' - and this is the real one. The exit status error just signals that the linking step in the build process encountered some errors. Normally exit status 0 means success, and exit status > 0 means errors.

When you build your program, multiple tools may be run as separate steps to create the final executable. In your case one of those tools is ld, which first reports the error it found (clrscr reference missing), and then it returns the exit status. Since the exit status is > 0, it means an error and is reported.

In many cases tools return as the exit status the number of errors they encountered. So if ld tool finds two errors, its exit status would be 2.

Solution 2 - C

In your situation you got a reference to the missing symbols. But in some situations, ld will not provide error information.

If you want to expand the information provided by ld, just add the following parameters to your $(LDFLAGS)

-Wl,-V

Solution 3 - C

clrscr is not standard C function. According to internet, it used to be a thing in old Borland C.
https://stackoverflow.com/questions/930138/is-clrscr-a-function-in-c

Solution 4 - C

Try running task manager to determine if your program is still running.

If it is running then stop it and run it again. the [Error] ld returned 1 exit status will not come back

Solution 5 - C

I got this error even with the basic HELLO WORLD program:

cout << "Hello World";

The problem was easy to fix : I FORGOT TO CLOSE previously running console window, simply minimized it and forgot. That's why I kept getting this error when I tried to run my future program. Just close it :)

Hope It would help, Though it could be that you have other issues.

Solution 6 - C

Include: #include<stdlib.h>

and use System("cls") instead of clrscr()

Solution 7 - C

Just press Ctrl+S then do the execution part

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3682120View Question on Stackoverflow
Solution 1 - CWojtek SurowkaView Answer on Stackoverflow
Solution 2 - CfazinerosoView Answer on Stackoverflow
Solution 3 - CRaivis RengelisView Answer on Stackoverflow
Solution 4 - CMuhammad Kashif ArifView Answer on Stackoverflow
Solution 5 - CKanishk MewalView Answer on Stackoverflow
Solution 6 - CPratikView Answer on Stackoverflow
Solution 7 - CMayaView Answer on Stackoverflow