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

when i create 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?

+7
linux hyperlink
source share
3 answers

terminate defined in the C ++ standard library, so make sure you link this. Assuming you are using gcc to compile, you should use the g++ executable to compile the source code, not the gcc executable:

 g++ source.cc -o output 

When executed as g++ , the linker is automatically linked in the C ++ standard library (libstdc ++) for you. If, instead of gcc, you run the plain gcc command or you directly call the ld linker, you need to add -lstdc++ yourself to communicate in the library, for example:

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

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

If it is in an object or source file, just pass it to gcc (if you really use gcc , if not, then the method will probably be similar) command as normal. If it is in a library, you should examine the options -L (path to the library) and -L (name of the library).

+2
source share

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

-2
source share

All Articles