Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

DMD Compiler for Windows

Requirements and Downloads

  1. Download D Compiler
  2. Windows operating system, Windows 7 or later, 32 or 64 bit
  3. Download dmc.zip (C and C++ compiler) for Win32 (not required, but it complements dmd for Windows)

Files

\dmd2\src\phobos\
D runtime library source
\dmd2\src\dmd\
D compiler front end source under dual (GPL and Artistic) license
\dmd2\html\d\
Documentation
\dmd2\samples\d\
Sample D programs
\dmd2\windows\bin\dmd.exe
D compiler executable
\dmd2\windows\bin\shell.exe
Simple command line shell
\dmd2\windows\bin\sc.ini
Global compiler settings
\dmd2\windows\lib\lib
D runtime library

Installation

Open a console window (for Windows XP this is done by clicking on [Start][Command Prompt]). All the tools are command line tools, which means they are run from a console window. Switch to the root directory. Unzip the files in the root directory. dmd.zip will create a \dmd2 directory with all the files in it. dmc.zip will create a \dm directory with all the files in it.

A typical session might look like:

C:\Documents and Settings\Your Name>cd \
C:\>unzip dmd.zip
C:\>unzip dmc.zip

Example

Run:

\dmd2\windows\bin\shell all.sh

in the \dmd2\samples\d directory for several small examples.

Compiler Arguments and Switches

dmd files... -switches...
files...
File Extensions
Extension File Type
none D source files
.d D source files
.dd Ddoc source files
.di D interface files
.obj Object files to link in
.lib Object code libraries to search
.exe Output executable file
.def module definition file
.res resource file
@cmdfile
If cmdfile is an environment variable, read the compiler arguments and switches from the value of that variable. Otherwise, read compiler arguments and switches from the text file cmdfile. The file may contain single-line comments starting with the hash symbol (#).
-allinst
generate code for all template instantiations
-betterC
omit generating some runtime information and helper functions
-boundscheck=[on|safeonly|off]
controls if bounds checking is enabled.
  • on: Bounds checks are enabled for all code. This is the default.
  • safeonly: Bounds checks are enabled only in @safe code. This is the default for -release builds.
  • off: Bounds checks are disabled completely (even in @safe code). This option should be used with caution and as a last resort to improve performance. Confirm turning off @safe bounds checks is worthwhile by benchmarking.
-c
compile only, do not link
-color=[on|off]
Show colored console output. The default depends on terminal capabilities.
  • on: always use colored output. Same as -color
  • off: never use colored output.
-conf=path use config file at path
-cov

Perform code coverage analysis and generate .lst file with report.

dmd -cov -unittest myprog.d
-cov=nnn
Perform code coverage analysis, generate .lst file with report, and error at runtime if code coverage is less than nnn% of the executable lines. This can be integrated into the build test process to ensure minimum unit test coverage.
-D
generate documentation from source
-Dddocdir
write documentation file to docdir directory. -op can be used if the original package hierarchy should be retained
-Dffilename
write documentation file to filename
-d
Silently allow deprecated features and use of symbols with deprecated attributes.
-dw
treat use of deprecated features and attributes as warnings (this is the default)
-de
treat use of deprecated features and attributes as errors
-debug
compile in debug code
-debug=level
compile in debug level <= level
-debug=ident
compile in debug identifier ident
-debuglib=libname
Link in libname as the default library when compiling for symbolic debugging instead of lib. If libname is not supplied, then no default library is linked in.
-defaultlib=libname
Link in libname as the default library when not compiling for symbolic debugging instead of lib. If libname is not supplied, then no default library is linked in.
-deps[=filename]
Without filename, print module dependencies (imports/file/version/debug/lib). With filename, write module dependencies as text to filename (only imports).
-dip25
implement http://wiki.dlang.org/DIP25 (experimental)
-g
add CodeView symbolic debug info with D extensions for debuggers such as Ddbg
-gc
add CodeView symbolic debug info in C format for debuggers such as \dmd2\bin\windbg
-gs
always generate standard stack frame
-gx
add stack stomp code
-H
generate D interface file
-Hddir
write D interface file to dir directory. -op can be used if the original package hierarchy should be retained
-Hffilename
write D interface file to filename
--help
print brief help to console
-Ipath
where to look for imports. path is a ; separated list of paths. Multiple -I's can be used, and the paths are searched in the same order.
-ignore
ignore unsupported pragmas
-inline
inline expand functions at the discretion of the compiler. This can improve performance, at the expense of making it more difficult to use a debugger on it.
-Jpath
where to look for files for ImportExpressions. This switch is required in order to use ImportExpressions. path is a ; separated list of paths. Multiple -J's can be used, and the paths are searched in the same order.
-Llinkerflag
pass linkerflag to the linker link.exe , for example, -L/ma/li
-lib
generate library file as output instead of object file(s). All compiled source files, as well as object files and library files specified on the command line, are inserted into the output library. Compiled source modules may be partitioned into several object modules to improve granularity. The name of the library is taken from the name of the first source module to be compiled. This name can be overridden with the -of switch.
-m32
Compile a 32 bit executable. This is the default. The generated object code is in OMF and is meant to be used with the Digital Mars C/C++ compiler.
-m32mscoff
generate 32 bit code and write MS-COFF object files
-m64
Compile a 64 bit executable. The generated object code is in MS-COFF and is meant to be used with the Microsoft Visual Studio 10 or later compiler.
-main
Add a default main() function when compiling. This is useful when unittesting a library, as it enables running the unittests in a library without having to manually define an entry-point function.
-man
open default browser on this page
-map
generate a .map file
-noboundscheck
turns off all array bounds checking, even for safe functions. Deprecated (use -boundscheck=off instead).
-O
Optimize generated code. For fastest executables, compile with the -O -release -inline -boundscheck=off switches together.
-o-
Suppress generation of object file. Useful in conjuction with -D or -H flags.
-odobjdir
write object files relative to directory objdir instead of to the current directory. -op can be used if the original package hierarchy should be retained
-offilename
Set output file name to filename in the output directory. The output file can be an object file, executable file, or library file depending on the other switches.
-op
normally the path for .d source files is stripped off when generating an object, interface, or Ddoc file name. -op will leave it on.
-profile[=gc]
profile the runtime performance of the generated code.
  • gc: Instrument calls to memory allocation and write a report to the file profilegc.log upon program termination.
-release
compile release version, which means not emitting run-time checks for contracts and asserts. Array bounds checking is not done for system and trusted functions, and assertion failures are undefined behaviour.
-run srcfile args... compile
link, and run the program srcfile with the rest of the command line, args..., as the arguments to the program. No .obj or executable file is left behind.
-shared
generate DLL library
-transition=id
show additional info about language change identified by id
-transition=?
list all language changes
-unittest
compile in unittest code, turns on asserts, and sets the unittest version identifier
-v
verbose
-vcolumns
print character (column) numbers in diagnostics
-verrors=num
limit the number of error messages (0 means unlimited)
-version=level
compile in version level >= level
-version=ident
compile in version identifier ident
-vgc
list all gc allocations including hidden ones
-vtls
print informational messages identifying variables defaulting to thread local storage. Handy for migrating to shared model.
-w
enable warnings
-wi
enable informational warnings (i.e. compilation still proceeds normally)
-X
generate JSON file
-Xffilename
write JSON file to filename

Empty switches, i.e. "", are ignored.

Linking

Linking is done directly by the dmd compiler after a successful compile. To prevent dmd from running the linker, use the -c switch.

32 bit programs must be linked with the D runtime library phobos.lib, followed by the Digital Mars C runtime library snn.lib. This is done automatically as long as the directories for the libraries are on the LIB environment variable path. A typical way to set LIB would be:

set LIB=\dmd2\lib;\dm\lib

If the dmd command is used to both compile and link to an executable, it will make certain optimizations that are valid only for Windows executable files. Do not use the resulting .obj files in a DLL. To compile modules into .obj files that can be used in an exe or DLL, compile with -c and -shared.

Linking 32 bit programs is done using Digital Mars link.exe.

Linking 64 bit programs is done using the Microsoft linker.

Environment Variables

The D compiler dmd uses the following environment variables:

DFLAGS
The value of DFLAGS is treated as if it were appended to the command line to dmd.exe.
LIB
The linker uses LIB to search for library files. For D, it will normally be set to:
set LIB=\dmd2\lib;\dm\lib
LINKCMD
dmd normally runs the linker by looking for link.exe along the PATH. To use a specific linker for 32 bit programs instead, set the LINKCMD environment variable to it. For example:
set LINKCMD=\dmd\windows\bin\link
LINKCMD64
dmd normally runs the linker by looking for link.exe along the PATH. To use a specific linker for 64 bit programs instead, set the LINKCMD64 environment variable to it. For example:
set LINKCMD64=C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64\link.exe
PATH
If the linker is not found in the same directory as dmd.exe is in, the PATH is searched for it. Note: other linkers named link.exe will likely not work. Make sure the Digital Mars link.exe is found first in the PATH before other link.exe's, or use LINKCMD to specifically identify which linker to use.

sc.ini Initialization File

dmd will look for the initialization file sc.ini in the following sequence of directories:

  1. current working directory
  2. directory specified by the HOME environment variable
  3. directory dmd.exe resides in

If found, environment variable settings in the file will override any existing settings. This is handy to make dmd independent of programs with conflicting use of environment variables.

Initialization File Format

Comments are lines that begin with ; and are ignored.

Environment variables follow the [Environment] section heading, in NAME=value pairs. The NAMEs are treated as upper case. Comments are lines that start with ;. For example:

; sc.ini file for dmd
; Names enclosed by %% are searched for in the existing environment
; and inserted. The special name %@P% is replaced with the path
; to this file.
[Environment]
LIB="%@P%\..\lib";\dm\lib

DFLAGS="-I%@P%\..\src\phobos" "-I%@P%\..\src\druntime\import"
LINKCMD="%@P%\..\..\dm\bin"
DDOCFILE=mysettings.ddoc

Location Independence of sc.ini

The %@P% is replaced with the path to sc.ini. Thus, if the fully qualified file name sc.ini is c:\dmd2\bin\sc.ini, then %@P% will be replaced with c:\dmd2\bin, and the above sc.ini will be interpreted as:

[Environment]
LIB="c:\dmd2\bin\..\lib";\dm\lib

DFLAGS="-Ic:\dmd2\bin\..\src\phobos" "-Ic:\dmd2\bin\..\src\druntime\import"
LINKCMD="c:\dmd2\bin\..\..\dm\bin"
DDOCFILE=mysettings.ddoc

This enables your dmd setup to be moved around without having to re-edit sc.ini.

Common Installation Problems


Differences between Windows and Linux versions


D Interface Files

When an import declaration is processed in a D source file, the compiler searches for the D source file corresponding to the import, and processes that source file to extract the information needed from it. Alternatively, the compiler can instead look for a corresponding D interface file. A D interface file contains only what an import of the module needs, rather than the whole implementation of that module.

The advantages of using a D interface file for imports rather than a D source file are:

D interface files can be created by the compiler from a D source file by using the -H switch to the compiler. D interface files have the .di file extension. When the compiler resolves an import declaration, it first looks for a .di D interface file, then it looks for a D source file.

D interface files bear some analogous similarities to C++ header files. But they are not required in the way that C++ header files are, and they are not part of the D language. They are a feature of the compiler, and serve only as an optimization of the build process.

Building Executables

dmd can build an executable much faster if as many of the source files as possible are put on the command line.

Another advantage to putting multiple source files on the same invocation of dmd is that dmd will be able to do some level of cross-module optimizations, such as function inlining across modules.

Building Libraries

There are three ways to build a library. For example, given foo.d and bar.d which are to be compiled, and existing object file abc.obj and existing library def.lib which are all to be combined into a library foo.lib:

  1. Compile modules separately and then run the librarian on them:
    dmd -c foo.d
    dmd -c bar.d
    lib -c -p32 foo.lib foo.obj bar.obj abc.obj def.lib
    del foo.obj bar.obj
    
    This option is typical when using a makefile to avoid compiling modules that have already been compiled.
  2. Compile modules together and then run the librarian on them:
    dmd -c foo.d bar.d
    lib -c -p32 foo.lib foo.obj bar.obj abc.obj def.lib
    del foo.obj bar.obj
    
  3. Use dmd to compile and build library in one operation:
    dmd -lib foo.d bar.d abc.obj def.lib
    
    No object files are written to disk, it's all done in memory. Using -lib also has the advantage that modules may be compiled into multiple object files rather than exactly one per module. This improves granularity of the library without having to break up the modules.

Compiling dmd

Complete source code is provided to build the compiler. Follow these steps:

cd \dmd2\src\dmd
make -f win32.mak

Compiling Phobos

Complete source code is provided to build Phobos, the D runtime library. Follow these steps:

cd \dmd2\src\druntime
make -f win32.mak DMD=\dmd2\windows\bin\dmd
cd ..\phobos
make -f win32.mak DMD=\dmd2\windows\bin\dmd