dmd - Windows D Compiler
- dmd for Linux
- dmd for OSX
- dmd for FreeBSD
- Requirements and Downloads
- Compiler Arguments and Switches
- Environment Variables
- sc.ini Initialization File
- Common Installation Problems
- Differences between Windows and Linux versions
- D Interface Files
- Building Libraries
- Compiling dmd
- Compiling Phobos
Requirements and Downloads
- Download D Compiler
- 32 bit Windows (Win32) operating system, such as Windows XP
- Download dmc.zip (C and C++ compiler) for Win32 (not required, but it complements dmd for Windows)
- D runtime library source
- D compiler front end source under dual (GPL and Artistic) license
- Sample D programs
- D compiler executable
- Simple command line shell
- Global compiler settings
- D runtime library
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 \dmd 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
in the \dmd\samples\d directory for several small examples.
Compiler Arguments and Switches
- dmd files... -switches...
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
- 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
- compile only, do not link
- instrument for code coverage analysis
- generate documentation from source
- write documentation file to docdir directory
- write documentation file to filename
- allow deprecated features
- compile in debug code
- compile in debug level <= level
- compile in debug identifier ident
- link in libname as the default library when compiling for symbolic debugging instead of phobos.lib
- link in libname as the default library when not compiling for symbolic debugging instead of phobos.lib
- write module dependencies as text to filename
- add CodeView 4 symbolic debug info with D extensions for debuggers such as Ddbg
- add CodeView 4 symbolic debug info in C format for debuggers such as \dmd\bin\windbg
- always generate standard stack frame
- generate D interface file
- write D interface file to dir directory
- write D interface file to filename
- print brief help to console
- 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 unsupported pragmas
- 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.
- 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.
- pass linkerflag to the linker , for example, -L/ma/li
- 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 can be overridden with the -of switch.
- open default browser on this page
- generate a .map file
- Prevents emission of __fltused reference in object files, even if floating point code is present. Useful for library code.
- Optimize generated code. For fastest executables, compile with the -O -release -inline switches together.
- Suppress generation of object file. Useful in conjuction with -D or -H flags.
- write object files relative to directory objdir instead of to the current directory
- 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.
- normally the path for .d source files is stripped off when generating an object file name. -op will leave it on.
- profile the runtime performance of the generated code
- suppress non-essential compiler messages
- compile release version, which means not generating code for contracts, asserts and array bounds checking
- -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.
- compile in unittest code, turns on asserts, and sets the unittest version identifier
- compile in version level >= level
- compile in version identifier ident
- enable warnings
- enable informational warnings (i.e. compilation still proceeds normally)
- generate JSON file
- write JSON file to filename
Empty switches, i.e. "", are ignored.
Linking is done directly by the dmd compiler after a successful compile. To prevent dmd from running the linker, use the -c switch.
The programs must be linked with the D runtime library phobos.lib, followed by the 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:
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.
Linking is done using the optlink linker.
The D compiler dmd uses the following environment variables:
- The value of DFLAGS is treated as if it were appended to the command line to dmd.exe.
- The linker uses LIB to search for library files. For D, it will
normally be set to:
- dmd normally runs the linker by looking for link.exe
along the PATH. To use a specific linker instead, set the
LINKCMD environment variable to it. For example:
- 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:
- current working directory
- directory specified by the HOME environment variable
- 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" 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:\dmd\bin\sc.ini, then %@P% will be replaced with c:\dmd\bin, and the above sc.ini will be interpreted as:
[Environment] LIB="c:\dmd\bin\..\lib";\dm\lib DFLAGS="-Ic:\dmd\bin\..\src\phobos" LINKCMD="c:\dmd\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
- Using Cygwin's unzip utility has been known to cause strange problems.
- Running the compiler under Cygwin's command shell has been also known to cause problems. Try getting it to work under the regular Windows shell cmd.exe before trying Cygwin's.
- Installing dmd and dmc into directory paths with spaces in them causes problems.
Differences between Windows and Linux versions
- String literals are read-only under Linux. Attempting to write to them will cause a segment violation.
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 are often significantly smaller and much faster to process than the corresponding D source file.
- They can be used to hide the source code, for example, one can ship an object code library along with D interface files rather than the complete source code.
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.
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.
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 bar.obj and existing library def.lib which are all to be combined into a library foo.lib:
- 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.objThis option is typical when using a makefile to avoid compiling modules that have already been compiled.
- 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
- Use dmd to compile and build library in one operation:
dmd -lib foo.d bar.d abc.obj def.libNo 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.
Complete source code is provided to build the compiler. Follow these steps:
cd \dmd\src\dmd make -f win32.mak
Complete source code is provided to build Phobos, the D runtime library. Follow these steps:
cd \dmd\src\phobos make -f win32.mak DMD=\dmd\\bin\dmd