127

I see the error collect2: error: ld returned 1 exit status very often. For example, I was trying to build 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?

Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
user3682120
  • 1,273
  • 2
  • 9
  • 4
  • Try including `conio.h` if you are using TurboC. If you are using GCC,this won't work even if you include it.Also,use `int main()` instead of `void main()` and add a `return 0;` at the end.Also the program would just print `Hi..` and exit whatsoever be the input – Spikatrix Dec 03 '14 at 13:07
  • "`collect2: error: ld returned 1 exit status`" is a signature of [GCC](https://en.wikipedia.org/wiki/GNU_Compiler_Collection) ([ld](https://en.wikipedia.org/wiki/Linker_%28computing%29#GNU_linker)). – Peter Mortensen Jan 22 '23 at 02:13
  • Some answers claim this can be due to something outside the program and the compilation process itself, like running something at the same time (double). Can someone get to the bottom of this? For example, how can it be reproduced? – Peter Mortensen Jan 22 '23 at 02:37
  • Possibly related: *[Exit code 130 on Linux and 2 on Windows on SIGINT](https://stackoverflow.com/questions/66321338/exit-code-130-on-linux-and-2-on-windows-on-sigint)* and *[Are there any standard exit status codes in Linux?](https://stackoverflow.com/questions/66321338/exit-code-130-on-linux-and-2-on-windows-on-sigint)*. – Peter Mortensen Jan 22 '23 at 02:40

7 Answers7

146

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 the number of errors they encountered as the exit status. So if the ld tool finds two errors, its exit status would be 2.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
  • 1
    Hi Sorowka,Thanks!! "In many cases tools return as the exit status the number of errors they found" means if there are 2 errors it'll return ld returned 2 exit status . – user3682120 Dec 03 '14 at 13:17
  • 32
    The very same error (`collect2: error: ld returned 1 exit status`) can be caused when there is not enough space left at `/usr/tmp/`. Because linker wont be able to create temporary files. – Kamil S Jaron Feb 07 '17 at 13:10
  • ld is the GNU linker and is typically the last step in the C compilation process. Here is the online man page for ld: https://ftp.gnu.org/old-gnu/Manuals/ld-2.9.1/html_mono/ld.html Your error is probably due to clrscr not being defined in the libraries you provided to the linker. – Aloha Churchill Nov 23 '21 at 17:13
  • 1
    I want to emphasize that "undefined reference to clrscr" is just an _example_ (based on the original question) of an "earlier error" that can trigger the confusing "ld returned 1 exit status" message. _Whenever_ you get "ld returned 1 exit status", look immediately above it for an earlier error message that gives more details of the problem. – zwol Nov 20 '22 at 18:24
17

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
fazineroso
  • 7,196
  • 7
  • 31
  • 42
  • 4
    On OS X I had to use `-Wl,-t` as `ld` does not recognize `-V` as an option, but `-t` causes it to list all libraries it is attempting to link (which helped fix my problem and gets you part way to what `-V` does) – inspector-g Nov 02 '15 at 21:21
  • @inspector-g. Same here with GNU ld 2.38 – user3817445 Oct 31 '22 at 08:39
7

clrscr is not standard C function. According to the Internet, it used to be a thing in old C++Builder.

Is clrscr(); a function in C++?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Or [even Turbo](https://www.quora.com/Why-cant-GCC-and-Clang-just-work-like-Turbo-C-for-Yashavant-Kanetkar-books-eg-Let-Us-C-I-read-Let-Us-C-and-Turbo-C-just-works-and-if-I-type-the-same-code-in-GCC-and-Clang-it-says-gotoxy-is-undeclared/answer/Paulina-Jonu%C5%A1ait%C4%97) [C++](https://www.quora.com/Why-do-people-criticise-Yashavant-Kanetkar-books-like-Let-Us-C-Why-dont-more-people-use-Turbo-C/answer/Joe-Zbiciak)? – Peter Mortensen Jan 22 '23 at 02:06
  • Though "`collect2: error: ld returned 1 exit status`" is a signature of [GCC](https://en.wikipedia.org/wiki/GNU_Compiler_Collection) ([ld](https://en.wikipedia.org/wiki/Linker_%28computing%29#GNU_linker)). – Peter Mortensen Jan 22 '23 at 02:09
2

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • While this is generally true, it is not related to the question. It will not fix any undefined references errors that caused the failure. – Gerhardh Aug 25 '19 at 10:13
  • This answer may make more sense by reading [this answer](https://stackoverflow.com/questions/27272525/what-does-collect2-error-ld-returned-1-exit-status-mean/67610346#67610346). – Peter Mortensen Jan 22 '23 at 02:20
1

I got this error even with the basic Hello, World! program:

cout << "Hello, World!";

The problem was easy to fix: I forgot to close the 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 :)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kanishk Mewal
  • 399
  • 2
  • 10
  • So it can ***appear*** to be a linker error with the program even when it isn't? What is the explanation? Even if there is another window, haven't those processes exited already? Are there shared files? – Peter Mortensen Jan 22 '23 at 02:24
0

Include: #include<stdlib.h>

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

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Pratik
  • 27
  • 2
-3

Just press Ctrl+S then do the execution part

Maya
  • 5
  • 2
  • 4
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 28 '21 at 11:07
  • Ctrl + S in what context? [Visual Studio Code](https://en.wikipedia.org/wiki/Visual_Studio_Code)? How is it related to the question? – Peter Mortensen Jan 17 '23 at 22:04
  • Related: *[Why do I need 50 reputation to comment? What can I do instead?](https://meta.stackexchange.com/questions/214173/)*. – Peter Mortensen Jan 17 '23 at 22:06