Actual Reason: collect2: error: ld returned 1 exit status

This morning, I just got an error “collect2: error: ld returned 1 exit status”. So I am here to tell you when it occurred and the actual reason for this error. We will also understand how to resolve this.

When I find this error:

In my case, this was my 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;
  }
}

I got an error like this:

undefined reference to `clrscr'
collect2: error: ld returned 1 exit status

When I searched the internet for help, I found a lot of suggestions.

A guy (C++ developer) told me this:

Alright, so if you’re working with TurboC, you should include conio.h. However, keep in mind that if you’re using GCC, simply including it won’t work. When coding, opt for int main() instead of void main(), and make sure to add return 0; at the end. In terms of the program’s functionality, it seems that regardless of the input, it will print “Hi..” and then exit.

Henry B, System Engineer

The actual reason of collect2: error: ld returned 1 exit status

Ld is nothing but GNU linker. (Implementation of Unix command ld). So you can find this error in Unix system.

The “ld returned 1 exit status” error is a consequence of previous errors. In the example I provided, the underlying issue is an earlier error stating “undefined reference to ‘clrscr,'” and this is the root cause of the problem. The exit status error merely indicates that the linking step in the build process encountered errors. Typically, an exit status of 0 signifies success, while an exit status greater than 0 indicates errors.

During the program building process, various tools are executed as separate steps to generate the final executable. In this case, one such tool is ld, which first identifies the error (in this instance, the missing reference to ‘clrscr’), and then it returns the exit status. The fact that the exit status is greater than 0 signals an error.

It’s noteworthy that many tools use the exit status to communicate the number of errors encountered. For instance, if the ld tool identifies two errors, its exit status would be 2.

How to fix collect2: error: ld returned 1 exit status

Here are some solutions:

Due to the storage shortage:

Note that:

If there is not enough storage left at /usr/tmp/ then also you can get this error. Because the GNU linker needs space to create temp files in your system.

Due to not closing the previously opened console window or terminal:

If you do not close the previously running console window, then no matter what your code is, you will get the same error.

So make sure that you close the windows. (Do not minimize. Close that completely and then run your program)

Avoid using clrscr()

Just include #include<stdlib.h>

Avoid clrscr() and instead of that, you can use System("cls") (If you are on windows this is a great solution)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top