Node:C++ exceptions, Next:, Previous:NULL redefined, Up:Compiling

8.19 C++ exceptions support

Q: I've written a program that uses C++ exceptions, but instead of catching an exception, the program prints "Abort!" and dies....

Q: When linking C++ programs, I get messages about undefined references to __EH_FRAME_BEGIN__ and such likes. Huh?

Q: I cannot compile C++ programs that include the header math.h: the compiler complains about redefinition of class exception!

A: C++ exceptions were not fully supported in DJGPP before version 2.8.1 of GCC. Either upgrade to the latest version or compile with the -fsjlj-exceptions switch to GCC. GCC support of exceptions before v2.8.0, was minimal, so even this special switch won't work with previous versions. If you still use GCC 2.7.2.1 and cannot upgrade, you need to compile with the -frtti compiler switch and include the typeinfo header in your program.

Beginning with EGCS 1.1.2 and GCC 2.95, C++ exception support requires DJGPP v2.02 or later, and will not work with v2.01 or earlier, so you might need to upgrade your DJGPP library.

Note that exception support with -fsjlj-exceptions is very slow, since it has a significant runtime overhead, even if the exception doesn't occur.

If you already use GCC 2.8.1, these problems could happen if you failed to replace the specs file with the version which comes with the GCC 2.8.1 distribution. Read the file readme.DJGPP in the GCC distribution, for more details. GCC 2.95 and later should work with the specs file from djdev202.zip (or later) or with the specs file that comes with GCC itself.

Exception support in GCC is generally not stable enough yet, so you need to treat with some suspicion code produced by GCC 2.8.1 for programs that use exceptions. Latest versions of GCC support exceptions better, so upgrade to GCC 2.95 or later.

Undefined references to symbols like __EH_FRAME_BEGIN__ are a symptom of using an old linker script djgpp.djl. You should make sure that djgpp.djl in your lib subdirectory is from djdevNNN.zip file that belongs to DJGPP v2.02 or later. (GCC 2.8.1 distribution required to replace djgpp.djl with a version that came with the compiler, but the reason for that is no longer valid with newer GCC versions, and the compiler no longer comes with djgpp.djl. So you must restore djgpp.djl from djdevNNN.zip.) Again, readme.DJGPP in the GCC distribution has more on this.

If GCC complains about "Redefinition of class exception" when you compile C++ programs which include the header math.h, you need to replace that header. GCC 2.8.1 comes with a header exception that conflicts with math.h from DJGPP v2.01, which defines a struct exception. Version 2.02 of DJGPP corrects its math.h, but if you still use v2.01, a corrected version is included in the gcc281b.zip distribution. The corrected math.h is installed into the lib/gcc-lib/djgpp/2.81/include directory, so either delete or rename the old version in the include directory, or copy the corrected version into include. Another solution is to compile with the -posix or -ansi compiler switch, which cause math.h to not define struct exception.