Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

How to solve this problem?

like image 903
sudha Avatar asked Nov 19 '10 06:11

sudha


People also ask

What is the meaning of ID returned 1 exit status?

The C++ [Error]: Id returned 1 exit status is not a common error. It usually means that the program crashed, and it will be challenging to determine why without looking at the stack trace. The C++ compiler will display this message when an error occurs in your code.

What is Collect2 EXE?

Collect2.exe is considered a type of Windows Executable file. It is most-commonly used in C-Free 5.0 Pro developed by Program Arts. It uses the EXE file extension and is considered a Win32 EXE (Windows Executable) file.


2 Answers

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:

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.:

gcc source.cc -o output -lstdc++  # Compile directly from source
ld source1.o source2.o -o output -lstdc++  # Link together object files
like image 89
Adam Rosenfield Avatar answered Oct 17 '22 20:10

Adam Rosenfield


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

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. If it's in a library, you should look into the -L (library path) and -l (library name) options.

like image 40
paxdiablo Avatar answered Oct 17 '22 21:10

paxdiablo