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

前端 未结 2 542
无人共我
无人共我 2021-01-04 00:05

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

相关标签:
2条回答
  • 2021-01-04 00:35

    As H2CO3 alludes to, and to get an idea of how hard it would be to manually link, try running g++ with the verbose switch (-v). It will output the commands (along with some other information) for all the steps in the process (pre-process, compile, assemble, link).

    For instance, building a simple 'Hello, World' with g++ on Cygwin, yields:

    /usr/lib/gcc/i686-pc-cygwin/3.4.4/collect2.exe -Bdynamic --dll-search-prefix=cyg
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../crt0.o -L . -L/usr/lib/gcc/i686-pc-cygwin/3.4.4
    -L/usr/lib/gcc/i686-pc-cygwin/3.4.4 -L/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../..
    /tmp/cc4btl0k.o -lfoo -lstdc++ -lgcc -lcygwin -luser32 -lkernel32 -ladvapi32 -lshell32 -lgcc
    

    ...for the linking phase.

    0 讨论(0)
  • 2021-01-04 00:50

    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.

    0 讨论(0)
提交回复
热议问题