C++ 链接错误“对`__gxx_personality_v0'的未定义引用”和g++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6045809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:23:48  来源:igfitidea点击:

Link error "undefined reference to `__gxx_personality_v0'" and g++

c++g++

提问by develhevel

Possible Duplicate:
Undefined Symbol ___gxx_personality_v0 on link

可能重复:
链接上的未定义符号 ___gxx_personality_v0

I have a problem with the following program.

我对以下程序有问题。

// fkt.cpp

#include "fkt.h"

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

And the header:

和标题:

// fkt.h

int add2(int a, int b);

Now I compile this with:

现在我编译这个:

g++ -c fkt.cpp

Now I run nmand get:

现在我运行nm并得到:

00000000 T _Z6add2ii
         U __gxx_personality_v0

When I want to use the function anywhere I get:

当我想在任何地方使用该功能时:

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

How can I solve this problem? (I'm using UbuntuLinux.)

我怎么解决这个问题?(我使用的是UbuntuLinux。)

回答by phoxis

If g++still gives error Try using:

如果g++仍然给出错误尝试使用:

g++ file.c -lstdc++

Look at this post: What is __gxx_personality_v0 for?

看看这个帖子:__gxx_personality_v0 是什么?

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.

确保-lstdc++在命令的末尾。如果你把它放在开头(即在 file.c 之前),你仍然会得到同样的错误。

回答by onteria_

It sounds like you're trying to link with your resulting object file with gccinstead of g++:

听起来您正在尝试使用gcc而不是与生成的目标文件链接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:

请注意,使用 C++ 目标文件的程序必须始终与 g++ 链接,以便提供适当的 C++ 库。尝试将 C++ 对象文件与 C 编译器 gcc 链接将导致 C++ 标准库函数的“未定义引用”错误:

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

来源:GCC 简介 - 用于 GNU 编译器 gcc 和 g++