mingw编译程序出现_Unwind_SjLj_Register报错

⌚Time: 2025-04-26 21:58:00

👨‍💻Author: Jack Ge

我从一个电脑上拷贝项目到另一个电脑编译时候出现了几个undefined reference的问题,这种问题就是链接阶段出现了找不到符号的情况。

我尝试加了-lgdi32-lcomctl32参数链接了一些关键库,解决了很多这些报错,但是最后有几条始终出现,就是:

undefined reference to `__gxx_personality_sj0'
undefined reference to `_Unwind_SjLj_Register'
undefined reference to `_Unwind_SjLj_Resume'
undefined reference to `_Unwind_SjLj_Unregister'

我的mingw版本是32位dwarf版。一开始我问AI,AI说我用的不是SjLj版本的mingw,这些是SjLj异常处理的符号,我的编译器是DWARF异常处理。让我使用兼容的mingw版本。

我又在网上搜索stackoverflow上面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).

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.

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.

提醒我是使用了sjlj版本mingw编译的库而在dwarf版本mingw上使用。我发现自己直接拷贝项目,连里面的目标文件.o文件一起拷贝了,在编译链接时直接使用旧电脑上sjlj版本的mingw编译的.o文件进行链接。

之后清理项目就行了。重新编译目标文件成功了。