undefined reference to `__gxx_personality_sj0`

前端 未结 5 2276
余生分开走
余生分开走 2020-11-27 18:17

With gcc 4.6 when trying to execute this code:

   #include 

using namespace std;

#include 

int main()
{
   //Int<> a         


        
相关标签:
5条回答
  • 2020-11-27 19:00

    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.

    0 讨论(0)
  • 2020-11-27 19:08

    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

    0 讨论(0)
  • 2020-11-27 19:08

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

    0 讨论(0)
  • 2020-11-27 19:10

    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.

    0 讨论(0)
  • 2020-11-27 19:12

    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

    0 讨论(0)
提交回复
热议问题