链接错误“对‘__ gxx_person_v0’的未定义引用”和 g + +

可能重复:
未定义的符号 _ _ _ gxx _ 个性 _ v0在链接上

我对下面的程序有问题。

// fkt.cpp


#include "fkt.h"


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

标题是:

// fkt.h


int add2(int a, int b);

现在,我使用:

g++ -c fkt.cpp

现在我运行 nm,得到:

00000000 T _Z6add2ii
U __gxx_personality_v0

当我想在任何地方使用这个函数时:

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

我如何解决这个问题? (我使用的是 Ubuntu Linux。)

177414 次浏览

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

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.