Undefined Symbol ___gxx_personality_v0 on link

C++CGccG++

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

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 - C++

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 - C++

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 - C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionryan_sView Question on Stackoverflow
Solution 1 - C++ryan_sView Answer on Stackoverflow
Solution 2 - C++pseudosudoView Answer on Stackoverflow
Solution 3 - C++inketView Answer on Stackoverflow
Solution 4 - C++BadPirateView Answer on Stackoverflow