Switching to Digital Mars C++
Digital Mars C++ offers numerous advantages over other compilers, both in programmer productivity and in the speed and robustness of generated code. Switching to Digital Mars C++ is well worth the effort. This chapter outlines general considerations for converting existing code.What's in This Chapter
- Portable Programming Practices
- Object compatibility with other compilers.
- Using third party libraries with Digital Mars C++.
- Recompiling for Digital Mars C++.
- Converting from Microsoft
- Converting from Borland
Portable Programming Practices
Some of the programming practices that help make code portable and reusable also help make it compatible with Digital Mars C++. Your code will be easier to convert if you:- Use ANSI standard library functions wherever possible and avoid compiler-specific extensions.
- Do not rely on the location, alignment, or size of objects in memory. In addition to the inevitable implementation dependencies of memory models, the Digital Mars C++ compiler assigns different widths to some of the standard data types.
- Do not rely on objects of the same size being treated as if they had the same type. Digital Mars C++ enforces stricter type checking than either Microsoft C++ or Borland C++, and will generate warnings or errors in these instances. In addition, loosely typed code will often fail under 32-bit compilation. For example, Digital Mars C++ defines the WORD type as an unsigned short, and the UINT type as an unsigned int. If these types are treated identically in your code, you can use the -Jm compiler option to relax type checking.
- Use manifest constants wherever possible, instead of relying on explicit (and possibly compiler or environment dependent) values.
- Write function prototypes for all your user-defined functions. See Digital Mars C++ Language Implementation for more information.
- Can avoid linking files compiled with different compilers. Before you begin conversion, delete all .obj and .lib files created with your old compiler for which you have the source code. Also, be sure that the INCLUDE and LIB environment variables reference the Digital Mars C++ directories so you do not accidentally compile with one vendor's headers and link with the other's libraries.
Object Level Compatibility
Digital Mars C++ object modules are "broadly compatible" with Microsoft and Borland objects. However, there are some differences:- The Digital Mars C++ compiler's "helper" functions (long multiply, float, and so on) are different than their Borland or Microsoft counterparts. If you do not want to recode calls to these functions, remove the other vendor's helper functions from their libraries and include them in your converted code.
- Digital Mars' floating point libraries are reentrant; our object code is therefore different than Microsoft's or Borland's for functions with C linkage. You can work around this problem by rewriting affected functions to pass a pointer to their return value as a parameter, or by using C++, FORTRAN, or Pascal linkage instead of C linkage.
- The layout of the struct_iob in stdio. h is different for Digital Mars C++. Therefore, you cannot link buffered I/ O functions compiled with Digital Mars' stdio. h with buffered I/O functions compiled with a different stdio. h. To avoid problems, compile all modules in a program with the same version of stdio.h.
- By default, Digital Mars C++ aligns structure members on 16-bit boundaries in 16-bit compilations; Microsoft and Borland C++ do not. For binary compatibility, compile with the -a option to suppress structure member alignment.
Using Third-Party Libraries
If you must use third-party libraries written for Microsoft or Borland C++ with the Digital Mars C++ compiler, there is no simple way to determine which features are compatible. Your code could even link correctly and still contain obscure errors.If you have the source to the third-party library, try recompiling it with Digital Mars C++. A better solution is to obtain the Digital Mars version of the library from the vendor. Recompiling for Digital Mars C++ In many cases, converting programs written for another compiler to Digital Mars C++ can be as easy as recompiling them. Here are some steps you can take to ease recompilation:
- Compile without the global optimizer until your program runs without errors. A working program can occasionally fail when optimized. For more information, see Optimizing Code.
- In Digital Mars C++, character data is signed by default. If your code depends on char data being unsigned, compile with either the -J or -Ju options. -J treats chars as unsigned, not sign extended. -Ju treats chars as unsigned, sign extended.
- If your code does not follow strict ANSI type checking rules, use the -Jm option, which relaxes type checking.
Compile time warnings
By default, the Digital Mars C++ compiler generates warnings in response to more conditions than do most other compilers. If you find these unfamiliar warnings annoying, compile with the -w (warning level) option. Each instance of -w on the SC command line turns off a specific warning. For example, the command:dmc -w2 -w6 myfile.cppturns off warnings number 2 and 6. For information on warnings and their numbers, see Compiling Code.