Undefined Symbol ___gxx_personality_v0 on link

32,298

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.

Share:
32,298
ryan_s
Author by

ryan_s

Updated on July 09, 2022

Comments

  • ryan_s
    ryan_s almost 2 years

    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?

  • paxdiablo
    paxdiablo over 15 years
    I though gcc was the front end that recognized cpp files and passed it through to the correct compiler.
  • C. K. Young
    C. K. Young over 15 years
    @Pax Diablo: Yes, it uses the correct compiler, however, g++ passes libstdc++ to the linker whereas gcc doesn't. :-P
  • ryan_s
    ryan_s over 15 years
    Right, it's a linker problem, not a compilation one. I normally don't build individual files from the command line like this, so I didn't even think and just typed gcc thinking it would work.
  • Head Geek
    Head Geek over 15 years
    I've made that same mistake myself, at least twice that I can remember. :-)
  • dividebyzero
    dividebyzero over 12 years
    This is not necessarily weariness or dumbness. I see lots of people answering to use g++ instead of gcc, but this is bad answer. The people asking and searching may not have that choice. I am working with libraries that have C and C++ versions, and I am writing a C program. Linking to some of these libraries made me have to link to stdc++, and I even had to specify with -L where to find it inside /usr/lib/gcc.
  • SSH This
    SSH This over 11 years
    Couldn't agree more with dividebyzero, combining C++ and C is not unheard of. Maybe your the dumb one.
  • daminetreg
    daminetreg about 10 years
    This is because .C files are recognized by cpp. That's the same in CMake for instance.
  • renedet
    renedet over 6 years
    Works to compile PHP 5.3+fpm with EXTRA_CFLAGS = -lstdc++ in Makefile