21

我知道这表明存在链接器问题,主要是未解析的符号。我知道要解决该问题/摆脱该错误消息,必须提供更多信息。我知道已经有很多关于解决这个问题的问题。

我的问题旨在帮助理解 make 和 ld,找出什么(和谁)试图用这条线表达什么。

collect2: ld returned 1 exit status
  • “收集2:”是什么意思?它是一个步骤 make 调用吗?我在我的系统上找不到具有该名称的可执行文件。
  • 这是否意味着我正在使用 ld ?我配置了我的项目/Makefile,以便 g++ 应该进行链接,那么为什么仍然涉及 LD
  • 谁在写那封邮件?制作 ?ld?克++?
  • 是否有一个有意义的可能退出代码列表?
4

2 回答 2

28

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) options 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.

于 2012-09-25T13:52:33.277 回答
3

正如 H2CO3 所暗示的那样,为了了解手动链接的难度,请尝试使用详细开关 (-v) 运行 g++。它将为过程中的所有步骤(预处理、编译、组装、链接)输出命令(以及一些其他信息)。

例如,在 Cygwin 上使用 g++ 构建一个简单的 'Hello, World' 会产生:

/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

...对于链接阶段。

于 2013-06-07T02:17:28.403 回答