开发者

undefined reference to `__gxx_personality_sj0`

开发者 https://www.devze.com 2023-04-12 22:22 出处:网络
With gcc 4.6 when trying to execute this code: #include <iostream> using namespace std; #include <bitset>

With gcc 4.6 when trying to execute this code:

   #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

What on hell 开发者_C百科is going on?!


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.


as smallB noted in a comment, you may have used gcc, focused on C programs, but you had a C++ program.

To compile a C++ program, make sure to use the g++ compiler driver instead!

example:

BAD: gcc -o foo.exe foo.cpp

GOOD: g++-o foo.exe foo.cpp


Just in case anyone else has this problem: I changed compilers AFTER creating .o files for my project.

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.

I assume rebuilding from scratch, without the delete, would work the same.


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.

But it won't automatically link to the C++ Standard Library (which for GCC is called 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.

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.

This is documented in the manual: https://gcc.gnu.org/onlinedocs/gcc/Invoking-G_002b_002b.html


Use minGW-g++.exe not gcc.exe See what happens now.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号