If you're creating a C++ program that doesn't use Exception-Handling (EH) or Run-Time Type Information (RTTI), you may want to prevent Borland C++ from adding the run-time code that supports these features. After all, the EH code takes up space in the executable file.
However, removing the EH code isn't as simple as setting
a compiler option. In this article, we'll show how you
can remove the EH code from a C++ program created in version 4.0,
4.02, or 4.5 of Borland C++.
There are three basic steps to eliminating EH code from a C++
program: disabling EH code generation, avoiding EH code in other
libraries and object files, and preventing the compiler from linking
the EH run-time code into your program.
The most obvious step in eliminating EH code is to not generate
any of it in your code. To do this, you'll need to specify
the -x- and RT- command-line options or
deselect the Enable Exceptions and the Enable Run-Time Type Information
check boxes on the Exception Handling/RTTI page of the Project
Options dialog box. In addition to telling the compiler not to
generate EH code, these selections also tell the compiler to flag
any use of the EH keywords (try, throw, catch)
as an error.
Once you've purged your own files of EH code, you need
to make sure that you're not depending on classes or functions
that might throw exceptions. The more common sources of EH code
are as follows:
You'll have to remove any occurrences of these classes
and macros before you can proceed with removing the EH support
code.
If you're using version 4.0 or 4.02 of Borland C++, you'll need to do two things in addition to using the -x- and -RT- options: provide a substitute version of the global function _ExceptInit() and call the global function set_new_handler(0) at the beginning of your program.
By the way, calling set_new_handler(0) doesn't
reduce the size of your EXE files. This function forces your program
to respond to a failed memory allocation by returning a null pointer
instead of throwing an exception.
If you're using version 4.5 of Borland C++, you won't have to provide your own version of _ExceptInit(). Instead, version 4.5 contains a library file that's named NOEH?.LIB. (The ? represents the first letter of the memory model, which is either Tiny, Small, Medium, Large, or Huge.) If you add this library to your projects, it will take care of eliminating the EH run-time code.
Additionally, if you perform any dynamic memory allocation, you
can use NOEH?.LIB to keep the size of your EXE small. In
comparison, if you use _ExceptInit() to eliminate EH
code, the size of the EXE will creep back up if you use operator
new() to allocate memory on the application's heap.
However, if you want operator new() to return 0 on a
failed allocation, as it did in early versions of C++, you need
to call set_new_handler(0) at the beginning of your program.
To see how big a difference these changes can make, let's build a simple DOS application with and without the EH support code. To begin, launch version 4.5 of the Borland C++ Integrated Development Environment (IDE).
When the IDE's main window appears, choose New Project... from the Project menu. In the New Project dialog box, enter \NO_EXCPT.IDE in the filename entry field, choose Application from the Target Type list box, choose DOS (Standard) from the Platform combo box, choose Large from the Memory Model combo box, and click OK.
When the new project's window appears, confirm that there isn't a no_excpt [.rc] node or a no_excpt [.def] file in the project, then double-click on the no_excpt [.cpp] node. When the editing window for this file appears, enter the source code from Listing A.
Listing A: NO_EXCPT.CPP
#include <new.h> int main() { set_new_handler(0); return 0; }
When you finish entering the code, choose Project... from the Options menu. In the Project Options dialog box, deselect the following check boxes on the Compiler - Debugging page: Debug Information In OBJs and Browser Reference Information In OBJs.
On the C++ Options - Exception Handling/RTTI page of the Project Options dialog box, deselect all of the check boxes. Finally, deselect the Include Debug Information check box on the Linker General page and click OK.
Now, choose Build from the Debug menu. When the compiler finishes building the application, open a DOS command prompt and check the size of the file. You should see a file size close to 28,954 bytes.
Return to the IDE, right-click on the no_excpt [.exe] node in the Project window, and choose Add Node from the pop-up menu. In the Add To Project List dialog box, enter \bc45\lib\noehs.lib and click OK.
Rebuild the application by again choosing Build from the Debug
menu. When the compiler finishes building the application, reopen
the DOS command prompt and check the size of the file. This time,
you'll notice the file size drops to 6,356 bytes.
Exception-Handling is a powerful C++ feature. However, if your
C++ programs don't use this feature, you can save a considerable
amount of EXE space by linking in the NOEH?.LIB file.
Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.