92

Possible Duplicate:
Undefined Symbol ___gxx_personality_v0 on link

I have a problem with the following program.

// fkt.cpp

#include "fkt.h"

int add2(int a, int b)
{
    return a+b;
}

And the header:

// fkt.h

int add2(int a, int b);

Now I compile this with:

g++ -c fkt.cpp

Now I run nm and get:

00000000 T _Z6add2ii
         U __gxx_personality_v0

When I want to use the function anywhere I get:

(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'

How can I solve this problem? (I'm using Ubuntu Linux.)

Community
  • 1
  • 1
develhevel
  • 3,161
  • 4
  • 21
  • 27

2 Answers2

138

If g++ still gives error Try using:

g++ file.c -lstdc++

Look at this post: What is __gxx_personality_v0 for?

Make sure -lstdc++ is at the end of the command. If you place it at the beginning (i.e. before file.c), you still can get this same error.

Community
  • 1
  • 1
phoxis
  • 60,131
  • 14
  • 81
  • 117
74

It sounds like you're trying to link with your resulting object file with gcc instead of g++:

Note that programs using C++ object files must always be linked with g++, in order to supply the appropriate C++ libraries. Attempting to link a C++ object file with the C compiler gcc will cause "undefined reference" errors for C++ standard library functions:

$ g++ -Wall -c hello.cc
$ gcc hello.o       (should use g++)
hello.o: In function `main':
hello.o(.text+0x1b): undefined reference to `std::cout'
.....
hello.o(.eh_frame+0x11):
  undefined reference to `__gxx_personality_v0'

Source: An Introduction to GCC - for the GNU compilers gcc and g++

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
onteria_
  • 68,181
  • 7
  • 71
  • 64
  • 1
    Happened to me when using [distcc](https://github.com/distcc/distcc/blob/master/README.md), because it defaults to using gcc, not g++. Solution is to specify the compiler `distcc g++ [...]` – jotrocken Oct 02 '15 at 13:55
  • adding -fno-threadsafe-statics solved the problem for me, when using a Arduino Sketch. – Soundararajan Dec 19 '15 at 08:34