digitalmars.com                        
Last update Mon Mar 8 17:52:43 2010
Compiler & Tools Guide

Tools
BCC
CHMOD
CL
COFF2OMF
COFFIMPLIB
DMC
DIFF
DIFFDIR
DUMP
DUMPOBJ
DUMPEXE
EXE2BIN
FLPYIMG
GREP
HC
IMPLIB
LIB
LIBUNRES
MAKE
MAKEDEP
ME
OBJ2ASM
PATCHOBJ
RC
RCC
SC
SHELL
SMAKE
TOUCH
UNMANGLE
WHEREIS


Compiling
Compiling Code
C Implementation
C++ Implementation
Language Extensions
Mixing Languages
Assembly Language
Inline Assembler
Optimizing Code
Numerics Programming
Regular Expressions
Acrtused
Pragmas
Precompiled Headers
Predefined Macros
Warning Messages
Error Messages
Runtime Messages


Linking
Optlink
Switches
Module Definition Files
Operation and Design
Error Messages


Win32 Programming
Win32 Programming


DOS and Win16
Programming

Memory Models
16 Bit Pointer Types
and Type Modifiers

Handle Pointers
DOS
DOS 32 (DOSX)
Win16
Win16 DLLs
Win16 Prolog/Epilog
Virtual Memory
For 640Kb DOS



C/C++ Extensions
Contract Programming
__debug statement
__debug declaration
Dynamic Profiling
Embedding C in HTML


Porting to DMC++
Switching to DMC++
from Microsoft
from Borland
Porting Guide



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

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.cpp 
turns off warnings number 2 and 6. For information on warnings and their numbers, see Compiling Code.

Converting from Microsoft

This chapter describes differences you should consider when converting code written for Microsoft Visual C++ Version 1.5 to Digital Mars C++.

What's in This Chapter

This chapter discusses compatibility issues between Microsoft Visual C++ and Digital Mars C++ with respect to these issues:
  • Keywords
  • Predefined macros
  • Header files
  • Libraries
  • MFC
  • Memory model support
  • Assembly language interface
  • Compiler options

Keywords

Digital Mars C++ supports most of Microsoft C's nonstandard keywords. The following table lists Microsoft's keyword extensions and their Digital Mars counterparts:

Table 23-1 Support for Microsoft keywords
Unsupported Microsoft keywords
__fastcall
__fortran
__saveregs
__segment
__segname
__self

Predefined Macros

Digital Mars C++ supports most of Microsoft C's nonstandard macros. The following table lists Microsoft's extended macros and their Digital Mars counterparts:

Table 23-2 Support for Microsoft C nonstandard macros
Unsupported Microsoft macros Digital Mars counterparts
_CHAR_UNSIGNED _CHAR_UNSIGNED
_FAST No support
_MSC_VER __DMC__ (works the same, but version numbers are for SC)
_MSDOS Recommended use is as a command line argument
MSDOS Recommended use is as a command line argument
_PCODE No support
_QC No support
M_I286 M_I286
M_I386 M_I386
M_I8086 M_I8086
M_I86 M_I86
M_I86CM M_I86CM
M_I86HM No support
M_I86LM M_I86LM
M_I86MM M_I86MM
M_I86SM M_I86SM
M_I86TM M_I86TM

Header Files

The Digital Mars C++ run-time library includes all the Microsoft nonstandard headers. Many of these headers simply reference the Digital Mars header file that contains equivalent functions. Digital Mars C++ issues a warning whenever your code makes use of such a header file. In general, we recommend switching to ANSI-compliant headers wherever possible.

Library Functions

Digital Mars C++ supports the great majority of Microsoft Visual C++ Version 1.5 library functions. The table below lists the only Microsoft functions that the Digital Mars C++ run-time library does not support:

Table 23-3 Unsupported Microsoft library functions
Unsupported functions Microsoft header Comments
Graphics functions graph.h Digital Mars C++ does not support the DOS graphics system.
Charting functions pgchart.h  
Virtual memory functions vmemory.h Digital Mars C++ no longer supports Virtual Code Management (VCM).
QuickWin functions io.h  
_bexpand malloc.h Use farrealloc instead.
_bfree malloc.h  
_bfreeseg malloc.h  
_bheapadd malloc.h Not needed with Digital Mars C++ heap management.
_bheapchk malloc.h  
_bheapmin malloc.h  
_bheapseg malloc.h  
_bheapset malloc.h  
_bheapwalk malloc.h  
_bmalloc malloc.h  
_bmsize malloc.h  
_brealloc malloc.h  
_dieeetomsbin math.h  
_dmsbintoieee math.h  
_expand malloc.h Use realloc instead.
_fexpand malloc.h Use farrealloc instead.
_fheapchk malloc.h  
_fheapmin malloc.h  
_fheapset malloc.h  
_fheapwalk malloc.h  
_fieeetomsbin math.h  
_fmsbintoieee math.h  
_heapadd malloc.h Not needed with Digital Mars C++ heap management.
_heapchk malloc.h  
_heapmin malloc.h  
_heapset malloc.h  
_heapwalk malloc.h  
_j0, _j1, _jnl math.h  
_j0l, _j1l, _jn math.h  
_nexpand malloc.h Use realloc instead.
_nheapadd malloc.h Not needed with Digital Mars C++ heap management.
_nheapchk malloc.h  
_nheapmin malloc.h  
_nheapset malloc.h  
_nheapwalk malloc.h  
_setbnew_handler new.h  
_sethnew_handler new.h  
_y0, _y1, _ynl math.h  
_y0l, _y1l, _yn math.h  

Microsoft Foundation Classes

Digital Mars C++ for Windows and DOS includes the Microsoft Foundation Classes Version 2.5, for the Large memory model only. Digital Mars C++ for Win32 includes 32-bit MFC 3.0.

Digital Mars' versions of the Microsoft Foundation Class Library are licenced from Microsoft Corporation.

When compiling code that uses the Microsoft Foundation Classes, use the -gf option (see Compiling Code).

Memory Models

The Digital Mars C++ implementation of those 16-bit memory models also supported by Microsoft C (the Small, Medium, Compact, and Large) is similar, but not exactly the same. The significant differences are:
  • In the Digital Mars Compact and Large models, unlike their Microsoft counterparts, the stack segment and data segment are not the same. Near pointers in these models are relative to the data segment and cannot be used to access automatic or parameter variables. Check your assembly language source code to ensure that SS is not assumed to be equal to DS.
  • Microsoft C only creates a far data segment if the amount of data exceeds 32,767 bytes, or exceeds the value specified via the /Gt option to CL (Digital Mars C++ has a corresponding compiler option, -GT). While data is always assigned to the near data segment by default, functions are near by default in the Compact model and far by default in the Large model in Digital Mars C++.
  • Digital Mars' malloc() functions take an unsigned int as an argument for all 16-bit memory models and take an unsigned long for 32-bit models. Therefore, the 16-bit models limit the size of a data object to 64KB blocks. To allocate a block greater than 64KB, call farmalloc, which takes an unsigned long.
  • At program startup, a Microsoft C program's heap management run-time allocates _amblksiz bytes of far heap space. Additional heap is allocated in _amblksiz blocks as needed. In contrast, Digital Mars C++ makes a call to DOS for each request for far heap space.
  • Unlike Microsoft C, Digital Mars C++ does not allocate heap blocks of zero size if a program requests them. NULL is returned instead.
  • Unlike Microsoft C, Digital Mars C++ cleans up free heap space when a program releases a block of memory.
  • Digital Mars C++ does not support the Microsoft C _heapadd, _bheapadd, and _nheapadd functions because its heap manager calls DOS each time a program requests far heap space.
  • Digital Mars C++ does not support the Microsoft _expand, _bexpand, _fexpand, and _nexpand routines. Use realloc in place of _expand or _nexpand calls (or wherever possible), and farrealloc in place of _bexpand or _fexpand. However, be aware that realloc and farrealloc, unlike _expand, can move the block to a new location to satisfy the reallocation request, in which case they will return the new location of the reallocated block.
  • Digital Mars C++ does not support these additional non-standard Microsoft memory management functions: _heapchk, _bheapchk, _fheapchk, _nheapchk, _heapwalk, _bheapwalk, _fheapwalk, _nheapwalk, _heapset, _bheapset, _fheapset, _nheapset, _heapmin, _bheapmin, _fheapmin, _nheapmin.

Huge memory model support

Digital Mars C++ does not support the Huge memory model. That is, a pointer whose type is unspecified cannot be made huge by default. However, the compiler supports huge pointers and huge data (via the __huge modifier).

Digital Mars C++ also supports the Microsoft halloc and hfree functions.

Support for based pointers

Digital Mars C++ does not support a based heap. No support for based pointers is available except for:
__based(__segname("_CODE"));
__based(__segname("_DATA")); 
__based(__segname("_STACK")); 
If your code makes use of based pointers, convert them to far pointers. Based pointers are a nonstandard construct and the code generated using them is not significantly better than for far pointers.

Assembly Language Interface

The Digital Mars C++ assembly language interface is very similar to Microsoft's. The only differences are:
  • DS is not equal to SS for the Compact and Large memory models.
  • For C functions (those declared as __cdecl or extern "C"), double and float values are returned in the registers, rather than in a static memory locaton. For C++, FORTRAN, and Pascal functions, double and float values are returned on the stack in a manner compatible with Microsoft C.
  • In Digital Mars C++, structs, floats, and doubles are returned by allocating a temporary variable on the stack and passing a hidden pointer to it. The called function copies the return values into this variable and returns a pointer to it.

Compiler Options

Digital Mars C++ provides a wide range of compiler command line options, many of which perform operations similar or identical to a Microsoft compiler option.

To convert your old Microsoft compiler command lines to the most similar Digital Mars command lines possible, use the CL utility. CL automatically converts Microsoft command lines to SC command lines, and then runs SC.


Converting from Borland

This chapter describes the differences you should consider when converting code written for Borland C++ Version 4.0 to Digital Mars C++.
  • Keywords
  • Predefined macros
  • Header files
  • Libraries
  • Memory model support
  • Assembly language interface
  • Compiler options

Keywords

Digital Mars C++ supports many of Borland C's nonstandard keywords. The following table lists Borland's extended keywords and their Digital Mars counterparts:

Table 24-1 Support for Borland nonstandard keywords
Unsupported Borland keywords Digital Mars counterparts
_cs __cs
_ds No equivalent
_es No equivalent
_saveregs No equivalent
_seg No equivalent
_ss __ss

Predefined Macros

Digital Mars C++ supports many of Borland C's nonstandard macros. The following table lists Borland's extended macros and their Digital Mars counterparts:

Table 24-2 Support for Borland C++ nonstandard macros
Unsupported Borland macros Digital Mars counterparts comments
__BORLANDC__ __DMC__ works the same, but version numbers are for DMC
__BCPLUSPLUS__ __DMC__ works the same, but version numbers are for DMC
__CDECL__ No equivalent  
__COMPACT__ __COMPACT__  
__DLL__ _WINDLL  
__HUGE__ No equivalent  
__LARGE__ __LARGE__  
__MEDIUM__ __MEDIUM__  
__MSDOS__ No equivalent  
__PASCAL__ No equivalent  
__OVERLAY__ No equivalent  
__SMALL__ __SMALL__  
__TEMPLATES__ No equivalent  
__TINY__ __TINY__  
__TCPLUSPLUS__ __DMC__ works the same, but version numbers are for DMC
__TURBOC__ __DMC__ works the same, but version numbers are for DMC
_Windows _WINDOWS  

Header Files

The Digital Mars C++ run-time library includes all the Borland non-standard headers. Many of these headers simply reference the Digital Mars header file that contains equivalent functions. Digital Mars C++ issues a warning whenever code uses a header file. In general, we recommend switching to ANSI-compliant headers wherever possible.

Library Functions

Digital Mars C++ supports most Borland library functions. The table below lists those Borland functions that the Digital Mars C++ run-time library does not support:

Table 24-3 Unsupported Borland library functions
Unsupported functions Borland header Comments
Graphics functions graphics.h  
Text display functions conio.h  
_dos_getvect dos.h  
_dos_setvect dos.h  
_harderr dos.h  
_hardresume dos.h  
_hardreturn dos.h  
_OvrInitEms dos.h  
_OvrInitExt dos.h  
_setcursortype conio.h Use display package functions
cgets conio.h Use display package functions
cprintf conio.h Use display package functions
cputs conio.h Use display package functions
closedir dirent.h  
cscanf conio.h  
dostounix dos.h  
farheapcheck alloc.h  
farheapcheckfree alloc.h  
farheapchecknode alloc.h  
farheapfillfree alloc.h  
farheapwalk alloc.h  
getpass conio.h  
getvect dos.h  
harderr dos.h  
hardresume dos.h  
hardreturn dos.h  
heapcheck alloc.h  
heapcheckfree alloc.h  
heapchecknode alloc.h  
heapfillfree alloc.h  
heapwalk alloc.h  
import dos.h  
importb dos.h  
intr dos.h  
opendir dirent.h  
outport dos.h  
outportb dos.h  
readdir dirent.h  
rewinddir dirent.h  
setvect dos.h  
unixtodos dos.h  

Some Digital Mars C++ run-time library functions have different types or numbers of arguments from their Borland counterparts, return different values, or support different defined constants. For information on specific functions see the Runtime Library Reference.

Borland Graphics Interface

Digital Mars C++ does not support any of the functions, fonts, or drivers in the Borland Graphics Interface.

Memory Models

The Digital Mars C++ implementations of those 16-bit memory models also supported by Borland C++ (the Tiny, Small, Medium, Compact, and Large models) are similar. Their significant differences include:
  • In the Digital Mars Compact and Large models, unlike their Borland counterparts, the stack and data segments are not the same. Near pointers in these models are relative to the data segment and cannot access automatic or parameter variables. Check your assembly language source code to ensure that SS is not assumed to be equal to DS.
  • Digital Mars' malloc() functions take an unsigned int as an argument for 16-bit memory models and take an unsigned long for 32-bit models. Therefore, the 16-bit models limit the size of a data object to 64KB blocks. To allocate a block greater than 64KB, call farmalloc, which takes an unsigned long.
  • At program startup, Borland's memory-management routine allocates all available DOS memory as one block of far heap space. In contrast, Digital Mars C++ makes a call to DOS for each request for far heap space.
  • Digital Mars C++ does not support these additional non-standard Borland memory-management functions: heapcheck, farheapcheck, heapfillfree, farheapfillfree, heapwalk, farheapwalk, coreleft.

Huge memory model support

Digital Mars C++ does not support the Huge memory model. That is, a pointer whose type is unspecified cannot be made huge by default. However, the compiler supports huge pointers and huge data (via the huge modifier).

Digital Mars C++ does not support the Borland halloc and hfree functions. Convert calls to these functions to the corresponding Digital Mars far calls (which have unsigned long argument types and can, therefore, allocate huge blocks) or store the affected data in a different structure (a huge array, for example).

Assembly Language Interface

The Digital Mars C++ assembly language interface is very similar to Borland's. The only differences are:
  • DS is not equal to SS for the Compact and Large memory models.
  • For C functions (those declared as __cdecl and extern "C"), double and float values are returned in registers, rather than in a static memory location. For C++, FORTRAN, and Pascal functions, double and float values are returned on the stack in a manner compatible with Borland C.
  • In Digital Mars C++, structs, floats, and doubles are returned by allocating a temporary variable on the stack and passing a hidden pointer to it. The called function copies the return values into this variable and returns a pointer to it.
  • Digital Mars C++ does not support the _FLAGS register variable.
  • Digital Mars C++ does not store long double values as 10-byte quantities; they are the same as doubles (8 bytes).

Compiler Options

Digital Mars C++ provides a wide range of compiler command line options, many of which perform operations similar or identical to a Borland C++ compiler option.

To convert Borland compiler command lines to the most similar Digital Mars command lines possible, use the BCC utility. BCC automatically converts Borland C++ command lines to SC command lines and then runs SC.