Undefined Symbol ___gxx_personality_v0 on link

I've been getting this undefined symbol building with this command line:

$ gcc test.cpp
Undefined symbols:
  "___gxx_personality_v0", referenced from:
  etc...

test.cpp is simple and should build fine. What is the deal?


Solution 1:

Use

g++ test.cpp

instead, since this is c++ code.


Or, if you really want to use gcc, add -lstdc++ to the command line, like so:

gcc test.cpp -lstdc++

Running md5 against the a.out produced under each scenario shows that it's the same output.

But, yeah, g++ probably makes your world a simpler place.

Solution 2:

The .cpp extension causes gcc to compile your file as a C++ file. (See the GCC docs.)

Try compiling the same file, but rename it to have a .c extension:

mv test.cpp
gcc test.c

Alternatively, you can explicitly specify the language by passing -x c to the compiler:

gcc -x c -c test.cpp -o test.o

If you run nm test.o on these C-language versions, you'll notice that ___gxx_personality_v0 is not listed as a symbol.
(And if you run the same command on an object file generated with gcc -c test.cpp -o test.o, the ___gxx_personality_v0 symbol is present.)

Solution 3:

Just in case anyone has the same problem as me: The file extension should be a .c not a .C (gcc is case-sensitive).

Solution 4:

Had the same problem, but a different solution:

C++ code in static library getting linked, and being referenced by a .m file. Renaming the .m file to .mm fixed the issue.