Linux 如何解决“collect2:ld 返回 1 个退出状态”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4222501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:52:50  来源:igfitidea点击:

How to solve 'collect2: ld returned 1 exit status '?

linuxhyperlink

提问by sudha

when I build my source code in linux I got an error like

当我在 linux 中构建我的源代码时,我得到了一个错误

qstring.cpp:(.text+0x2c01): undefined reference to `terminate(void)'
collect2: ld returned 1 exit status

How to solve this problem?

如何解决这个问题呢?

回答by paxdiablo

You need to find out which object file or library terminatelives in and include it in your compile/link command.

您需要找出terminate存在于哪个目标文件或库中,并将其包含在您的编译/链接命令中。

If it's in an object or source file, just give it to your gcc(assuming you're actually usinggcc, if not, the method will probably be similar) command as per normal. If it's in a library, you should look into the -L(library path) and -l(library name) options.

如果它在对象或源文件中,只需按照正常方式将其提供给您的gcc(假设您实际上正在使用gcc,如果没有,该方法可能会类似)命令。如果它在库中,您应该查看-L(library path) 和-l(library name) 选项。

回答by Ben Voigt

void terminate(void) { raise(9); }

void terminate(void) { raise(9); }

回答by Adam Rosenfield

terminateis defined in the C++ standard library, so make sure that you're linking that in. Assuming you're using gcc to compile, you should use the g++executable to compile your source code, notthe gccexecutable:

terminate在C ++标准库的定义,所以请确保您连接,在假设你使用gcc来编译,你应该使用的。g++可执行文件编译源代码,没有gcc可执行文件:

g++ source.cc -o output

When executed as g++, the linker automatically links in the C++ standard library (libstdc++) for you. If you instead execute gcc as plain gcc, or you directly invoke the linker ld, then you need to add -lstdc++yourself to link in the library, e.g.:

当作为 执行时g++,链接器会自动为您链接 C++ 标准库 (libstdc++)。如果您改为以普通方式执行 gcc gcc,或者您直接调用链接器ld,那么您需要将-lstdc++自己添加到库中的链接,例如:

gcc source.cc -o output -lstdc++  # Compile directly from source
ld source1.o source2.o -o output -lstdc++  # Link together object files