C++ 对 `__gxx_personality_sj0` 的未定义引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7751640/
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
undefined reference to `__gxx_personality_sj0`
提问by smallB
With gcc 4.6 when trying to execute this code:
尝试执行此代码时使用 gcc 4.6:
#include <iostream>
using namespace std;
#include <bitset>
int main()
{
//Int<> a;
long long min = std::numeric_limits<int>::min();
unsigned long long max = std::numeric_limits<int>::max();
cout << "min: " << min << '\n';
cout << "max: " << max << '\n';
cout << (min <= max);
std::bitset<64> minimal(min);
cout << "minimal: " << minimal;
return 0;
}
I'm getting the following error:
1. undefined reference to __gxx_personality_sj
2. undefined reference to _Unwind_SjLj_Register
3. undefined reference to _Unwind_SjLj_Unregister
4. undefined reference to _Unwind_SjLj_Resume
我收到以下错误:
1. undefined reference to __gxx_personality_sj
2. undefined reference to _Unwind_SjLj_Register
3. undefined reference to _Unwind_SjLj_Unregister
4. undefined reference to_Unwind_SjLj_Resume
What on hell is going on?!
到底是怎么回事?!
回答by Mankarse
These functions are part of the C++ exception handling support for GCC. GCC supports two kinds of exception handling, one which is based on calls to setjmp and longjmp (sjlj exception handling), and another which is based on the DWARF debugging info format (DW2 exception handling).
这些函数是 GCC 的 C++ 异常处理支持的一部分。GCC 支持两种异常处理,一种基于调用 setjmp 和 longjmp(sjlj 异常处理),另一种基于 DWARF 调试信息格式(DW2 异常处理)。
These sorts of linker errors will occur is you try to mix objects that were compiled with different exception handling implementations in a single executable. It appears that you are using a DW2 GCC, but some library that you are attempting to use was compiled with a sjlj version of GCC, leading to these errors.
如果您尝试在单个可执行文件中混合使用不同异常处理实现编译的对象,则会发生这些类型的链接器错误。您似乎正在使用 DW2 GCC,但是您尝试使用的某些库是使用 sjlj 版本的 GCC 编译的,从而导致了这些错误。
The short answer is that these sorts of problems are caused by ABI incompatibilities between different compilers, and so occur when you mix libraries compiled with different compilers, or use incompatible versions of the same compiler.
简而言之,这类问题是由不同编译器之间的 ABI 不兼容引起的,因此当您混合使用不同编译器编译的库或使用同一编译器的不兼容版本时,就会发生此类问题。
回答by n611x007
as smallB notedin a comment, you may have used gcc
, focused on C
programs, but you had a C++
program.
正如 smallB在评论中指出的那样,您可能已经使用gcc
, 专注于C
程序,但是您有一个C++
程序。
To compile a C++
program, make sure to use the g++
compiler driver instead!
要编译C++
程序,请确保改用g++
编译器驱动程序!
example:
例子:
BAD: gcc -o foo.exe foo.cpp
坏的: gcc -o foo.exe foo.cpp
GOOD: g++
-o foo.exe foo.cpp
好的: g++
-o foo.exe foo.cpp
回答by Ben
Just in case anyone else has this problem: I changed compilers AFTER creating .o
files for my project.
以防万一其他人遇到这个问题:我在.o
为我的项目创建文件后更改了编译器。
When I rebuilt the same project, the new compiler didn't build new .o
files, so they were missing some key info. After deleting the old files and rebuilding, the error was fixed.
当我重新构建同一个项目时,新编译器没有构建新.o
文件,因此它们缺少一些关键信息。删除旧文件并重建后,错误已修复。
I assume rebuilding from scratch, without the delete, would work the same.
我假设从头开始重建,没有删除,也会一样。
回答by Jonathan Wakely
The gcc
command is just a driver program that runs the right compiler for the source file you give it, (or run the linker if you give it object files). For a C++ source file with a known file extension it will run the C++ compiler (which for GCC is called cc1plus
) and compile the code correctly.
该gcc
命令只是一个驱动程序,它为您提供的源文件运行正确的编译器(或者,如果您提供目标文件,则运行链接器)。对于具有已知文件扩展名的 C++ 源文件,它将运行 C++ 编译器(对于 GCC 称为cc1plus
)并正确编译代码。
Butit won't automatically link to the C++ Standard Library (which for GCC is called libstdc++
).
但它不会自动链接到 C++ 标准库(GCC 称为libstdc++
)。
To solve the problem you either need to link to that library explicitly by adding -lstdc++
to the options when linking, or alternatively just compile and link with the g++
driver instead, which automatically adds -lstdc++
to the linker options.
要解决此问题,您需要在链接时通过添加-lstdc++
选项来显式链接到该库,或者只编译并链接g++
驱动程序,这会自动添加-lstdc++
到链接器选项。
So you can use gcc
to compile C++ programs, but if you use any features of the standard library or C++ runtime (including exception handling) then you need to link to the C++ runtime with -lstdc++
(or -lsupc++
for just the runtime). Using g++
does that for you automatically.
因此您可以使用gcc
来编译 C++ 程序,但是如果您使用标准库或 C++ 运行时的任何功能(包括异常处理),那么您需要使用-lstdc++
(或-lsupc++
仅用于运行时)链接到 C++ 运行时。使用g++
会自动为您做到这一点。
This is documented in the manual: https://gcc.gnu.org/onlinedocs/gcc/Invoking-G_002b_002b.html
这在手册中有记录:https: //gcc.gnu.org/onlinedocs/gcc/Invoking-G_002b_002b.html
回答by Misha Taylor
Use minGW-g++.exe not gcc.exe See what happens now.
使用 minGW-g++.exe 而不是 gcc.exe 看看现在会发生什么。