简体   繁体   English

如何解决'collect2:ld返回1退出状态'?

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

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? 如何解决这个问题呢?

terminate is 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, not the gcc executable: 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. 当以g++执行时,链接器会自动链接到C ++标准库(libstdc ++)中。 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, eg: 如果您改为将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

You need to find out which object file or library terminate lives in and include it in your compile/link command. 您需要找出terminate目标文件或库,并将其包含在您的compile / link命令中。

If it's in an object or source file, just give it to your gcc (assuming you're actually using gcc , if not, the method will probably be similar) command as per normal. 如果它在一个对象或源文件中,只需将它提供给你的gcc (假设你实际上正在使用 gcc ,如果没有,该方法可能会类似)命令正常。 If it's in a library, you should look into the -L (library path) and -l (library name) options. 如果它在库中,您应该查看-L (库路径)和-l (库名称)选项。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 linux程序集“collect2:ld返回1退出状态” - linux assembly “collect2: ld returned 1 exit status” collect2:错误:ld 返回 1 个退出状态(-lcudnn) - collect2: error: ld returned 1 exit status (-lcudnn) 多个定义collect2:错误:ld在C中返回了1个退出状态 - Multiple definition collect2: error: ld returned 1 exit status in C java对`main'collect2的未定义引用:ld返回1退出状态 - java undefined reference to `main' collect2: ld returned 1 exit status 无法运行程序:collect2:错误:ld 返回 1 退出状态 - Unable to run program: collect2: error: ld returned 1 exit status Visual Studio 2015 / Linux扩展与Cygwin一起产生“ collect2:错误:ld返回1退出状态” - Visual Studio 2015/Linux extension produces a “collect2 : error : ld returned 1 exit status” with Cygwin MulVAL 中的错误:`mylval' 的多重定义; collect2: 错误: ld 返回 1 个退出状态 - Error in MulVAL: multiple definition of `mylval'; collect2: error: ld returned 1 exit status 编译文件时出错 collect2:错误:ld返回1退出状态 - Error when compiling file | collect2: error: ld returned 1 exit status golang with cgo 抛出错误 collect2: error: ld returns 1 exit status - golang with cgo throws error collect2: error: ld returned 1 exit status /usr/bin/ld: 找不到 -lm collect2: 错误:ld 返回 1 退出状态 g++ 12 Ubuntu 20.4.4 - /usr/bin/ld: cannot find -lm collect2: error: ld returned 1 exit status g++ 12 Ubuntu 20.4.4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM