[英]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.