Take the 2-minute tour ×

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

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

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

share|improve this question
may we plx see the code if it isn't too long? –  jjnguy Oct 15 '08 at 2:38
add comment

4 Answers

up vote 53 down vote accepted

The deal is that you're tired and /or dumb. :)


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.

share|improve this answer
I though gcc was the front end that recognized cpp files and passed it through to the correct compiler. –  paxdiablo Oct 15 '08 at 2:41
@Pax Diablo: Yes, it uses the correct compiler, however, g++ passes libstdc++ to the linker whereas gcc doesn't. :-P –  Chris Jester-Young Oct 15 '08 at 2:43
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. –  ryan_s Oct 15 '08 at 2:49
I've made that same mistake myself, at least twice that I can remember. :-) –  Head Geek Oct 15 '08 at 3:04
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. –  dividebyzero Dec 16 '11 at 3:58
show 2 more comments

the extension .cpp causes gcc to compile your file as a c++ file. According to this page the extension does matter: http://gcc.gnu.org/onlinedocs/gcc-4.4.1/gcc/Overall-Options.html#index-file-name-suffix-71 Try compiling the same file but with the .c extension (or alternatively explicitly specify the language with -x c). If you run nm test.o (where test.o is the object file resulting from running gcc -x c -c test.cpp -o test.o) you'll notice that ___gxx_personality_v0 is not listed as a symbol whereas if you run the same command on an object file generated with gcc -c test.cpp -o test.o the ___gxx_personality_v0 is present.

share|improve this answer
add comment

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

share|improve this answer
add comment

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|improve this answer
add comment

Your Answer


