let's analyse "collect2: ld returned 1 exit status"?

lImbus picture lImbus · Sep 25, 2012 · Viewed 20.7k times · Source

I know this indicates a linker problem, mostly unresolved symbols. I know that to resolve that problem / to get rid of that errormessage, one would have to provide much more information. I know there is a lot of questions on resolving this problems on SO already.

My questions aims at helping to understand make and ld, to find out what (and who) is trying to express what with this line.

collect2: ld returned 1 exit status
  • What does "collect2:" mean? Is it a step make invokes ? I can't find an executable with that name on my system.
  • Does it mean I am using ld ? I configured my project / Makefile so that g++ should do the linking, so why is LD still involved
  • Who is writing that message ? make ? ld ? g++ ?
  • Is there a meaningful list of possible exit codes ?

Answer

user529758 picture user529758 · Sep 25, 2012

First things first, you need to know that the command-line programs named gcc and g++ are only umbrella (wrappers) around the actual preprocessor/parser-compiler/assembler/linker commands. Their real name is cpp, cc1 or cc1plus, as and ld, respectively. GCC helps in unifying their use, command line interface and providing a meaningful set of default (and required) otions for them. It is very hard, for example, to link a binary directly using ld - if ld is not run with all the 20+ (IIRC) correct options, it just fails to work.

Now that you know that, you can see:

Does it mean I am using ld ? I configured my project / Makefile so that g++ should do the linking, so why is LD still involved

It rather means you invoke GCC but in turn it invokes LD. GCC itself knows nothing - neither compiling, neither linking, as it's just a wrapper. (Go do a wc -c on /usr/bin/gcc and be surprised that it's only a few kilobytes! Now do the same for /usr/libexec/gcc/cc1plus and find out the horrifying truth: it is several 10 megs big!)

What does "collect2:" mean? Is it a step make invokes ? I can't find an executable with that name on my system.

Collect2 is also another level of indirection between gcc and ld. More about it on its official website.

Who is writing that message ? make ? ld ? g++ ?

Either g++ (that's it as far as I know) or collect2 itself maybe.

Is there a meaningful list of possible exit codes ?

The meaning is conventional - zero means success, nonzero means failure. If an exhaustive list exists, it should be able to be viewed by invoking man ld.