DMD Compiler for Windows
- 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
- DMD Compiler
- Windows operating system, Windows 7 or later, 32 or 64 bit
- Download dmc.zip (C and C++ compiler) for Win32 (not required, but it complements dmd for Windows)
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
in the \dmd2\samples\d directory for several small examples.
- 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. The file may contain single-line comments starting with the hash symbol (#).
- Generate code for all template instantiations
Adjusts the compiler to implement D as a better C:
- Predefines D_BetterC version.
- Assert Expressions, when they fail, call the C runtime library assert failure function rather than a function in the D runtime.
- Array overflows call the C runtime library assert failure function rather than a function in the D runtime.
- Final switch errors call the C runtime library assert failure function rather than a function in the D runtime.
- Does not automatically link with phobos runtime library.
- Module constructors and destructors are not generated meaning that static and shared static constructors and destructors will not get called.
- ModuleInfo is not generated.
- TypeInfo instances will not be generated for structs.
- -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.
- Compile only, do not link
- -check=[assert|bounds|in|invariant|out|switch ][=[on|off]]
Overrides default, -boundscheck, -release and -unittest options to enable or disable specific checks.
- assert: assertion checking
- bounds: array bounds
- in: in contracts
- invariant: class/struct invariants
- out: out contracts
- switch: finalswitch failure checking
- on or not specified: specified check is enabled.
- off: specified check is disabled.
- -check=[h|help|? ]
- List information on all available checks
- -checkaction=[D|C|halt|context ]
Sets behavior when an assert fails, and array boundscheck fails,
or a final switch errors.
- D: Default behavior, which throws an unrecoverable AssertError.
- C: Calls the C runtime library assert failure function.
- halt: Executes a halt instruction, terminating the program.
- context: Prints the error context as part of the unrecoverable AssertError.
- -checkaction=[h|help|? ]
- List information on all available check actions
- Turn colored console output on
- -color=[on|off|auto ]
Show colored console output. The default depends on terminal capabilities.
- auto: use colored output if a tty is detected (default)
- on: always use colored output.
- off: never use colored output.
- Use config file at filename
- Do code coverage analysis
- Include code executed during CTFE in coverage report
- Perform code coverage analysis and generate .lst file with report.
Generate documentation from source.
Note: mind the security considerations.
- Write documentation file to directory . -op can be used if the original package hierarchy should be retained
- Write documentation file to filename
- Silently allow deprecated features and use of symbols with deprecated attributes.
- Issue an error when deprecated features or symbols are used (halt compilation)
- Issue a message when deprecated features or symbols are used (default)
- 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. If libname is not supplied, then no default library is linked in.
- Link in libname as the default library when not compiling for symbolic debugging instead of phobos.lib. If libname is not supplied, then no default library is linked in.
- Print module dependencies (imports/file/version/debug/lib)
- Without filename, print module dependencies (imports/file/version/debug/lib). With filename, write module dependencies as text to filename (only imports).
Standards supported are:
- c++98: Use C++98 name mangling, Sets __traits(getTargetInfo, "cppStd") to 199711
- c++11 (default): Use C++11 name mangling, Sets __traits(getTargetInfo, "cppStd") to 201103
- c++14: Use C++14 name mangling, Sets __traits(getTargetInfo, "cppStd") to 201402
- c++17: Use C++17 name mangling, Sets __traits(getTargetInfo, "cppStd") to 201703
- c++20: Use C++20 name mangling, Sets __traits(getTargetInfo, "cppStd") to 202002
- -extern-std=[h|help|? ]
- List all supported standards
- Generate position independent code
- Generate position independent executables
- Add CodeView symbolic debug info. See Debugging on Windows.
- The value of version may be 3, 4 or 5, defaulting to 3.
- Symbolic debug info is emitted for all types referenced by the compiled code, even if the definition is in an imported file not currently being compiled.
- Always emit stack frame
- Adds stack stomp code, which overwrites the stack frame memory upon function exit.
- Generate D interface file
- Write D interface file to dir directory. -op can be used if the original package hierarchy should be retained.
- Write 'header' file to filename
- -HC[=[silent|verbose ]]
Generate C++ 'header' files using the given configuration:",
- only list extern(C[++]) declarations (default)
- also add comments for ignored declarations (e.g. extern(D))
- -HC=[?|h|help ]
- List available modes for C++ 'header' file generation
- Write C++ 'header' file to directory
- Write C++ 'header' file to filename
- Print help and exit
- Look for imports also in directory
- -i[=pattern ]
Enables "include imports" mode, where the compiler will include imported modules in the compilation, as if they were given on the command line. By default, when this option is enabled, all imported modules are included except those in druntime/phobos. This behavior can be overriden by providing patterns via -i=<pattern>. A pattern of the form -i=<package> is an "inclusive pattern", whereas a pattern of the form -i=-<package> is an "exclusive pattern". Inclusive patterns will include all module's whose names match the pattern, whereas exclusive patterns will exclude them. For example. all modules in the package foo.bar can be included using -i=foo.bar or excluded using -i=-foo.bar. Note that each component of the fully qualified name must match the pattern completely, so the pattern foo.bar would not match a module named foo.barx.
The default behavior of excluding druntime/phobos is accomplished by internally adding a set of standard exclusions, namely, -i=-std -i=-core -i=-etc -i=-object. Note that these can be overriden with -i=std -i=core -i=etc -i=object.
When a module matches multiple patterns, matches are prioritized by their component length, where a match with more components takes priority (i.e. pattern foo.bar.baz has priority over foo.bar).
By default modules that don't match any pattern will be included. However, if at least one inclusive pattern is given, then modules not matching any pattern will be excluded. This behavior can be overriden by usig -i=. to include by default or -i=-. to exclude by default.
Note that multiple -i=... options are allowed, each one adds a pattern.
- Ignore unsupported pragmas
- Inline 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 optlink.exe , for example, ld
- 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.
- Enable the garbage collector for the compiler, reducing the compiler memory requirements but increasing compile times.
- 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.
- Generate 32 bit code and write MS-COFF object files
- The generated object code is in MS-COFF and is meant to be used with the Microsoft Visual Studio 10 or later compiler.
- 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.
- -makedeps[=filename ]
- Print dependencies in Makefile compatible format. If filename is omitted, it prints to stdout. The emitted targets are the compiled artifacts (executable, object files, libraries). The emitted dependencies are imported modules and imported string files (via -J switch). Special characters in a dependency or target filename are escaped in the GNU Make manner.
- Open default browser on this page
- Generate a .map file
- Set the target architecture for code generation, where:
- -mcpu=[h|help|? ]
- List all architecture options
- Expand and save mixins to file specified by filename
- If building MS-COFF object files with -m64 or -m32mscoff, embed a reference to the given C runtime library libname into the object file containing main, DllMain or WinMain for automatic linking. The default is libcmt (release version with static linkage), the other usual alternatives are libcmtd, msvcrt and msvcrtd. If no Visual C installation is detected, a wrapper for the redistributable VC2010 dynamic runtime library and mingw based platform import libraries will be linked instead using the LLD linker provided by the LLVM project. The detection can be skipped explicitly if msvcrt120 is specified as libname. If libname is empty, no C runtime library is automatically linked in.
- -mv=package.module =
- Use path/filename as the source file for package.module. This is used when the source file path and names are not the same as the package and module hierarchy. The rightmost components of the path/filename and package.module can be omitted if they are the same.
- Turns off all array bounds checking, even for safe functions. Deprecated (use -boundscheck=off instead).
- Optimize generated code. For fastest executables, compile with the -O -release -inline -boundscheck=off 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. -op can be used if the original package hierarchy should be retained
- 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, interface, or Ddoc file name. -op will leave it on.
- Preview an upcoming language change identified by id
- -preview=[h|help|? ]
- List all upcoming language changes
- Profile runtime performance of generated code
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.
- 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.
- Revert language change identified by id
- -revert=[h|help|? ]
- List all revertable language changes
- -run srcfile
- 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.
- Generate DLL library
- Show additional info about language change identified by id
- -transition=[h|help|? ]
- List all language changes
- Compile in unittest code, turns on asserts, and sets the unittest version identifier
- Enable verbose output for each compiler pass
- Print character (column) numbers in diagnostics
- -verror-style=[digitalmars|gnu ]
Set the style for file/line number annotations on compiler messages,
- 'file(line[,column]): message'. This is the default.
- 'file:line[:column]: message', conforming to the GNU standard used by gcc and clang.
- Limit the number of error messages (0 means unlimited)
- Show error messages with the context of the erroring source line
- Show errors from speculative compiles such as __traits(compiles,...)
- Print compiler version and exit
- Compile in version level >= level
- Compile in version identifier ident
- List all gc allocations including hidden ones
- List all variables going into thread local storage
- -vtemplates=[list-instances ]
An optional argument determines extra diagnostics,
- Also shows all instantiation contexts for each template.
- Enable warnings
- Enable informational warnings (i.e. compilation still proceeds normally)
- Generate JSON file
- Write JSON file to filename
- Pass driverflag to the linker driver ($CC or cc)
dmd -cov -unittest myprog.d
Empty switches, i.e. "", are ignored.
- D runtime library source
- D compiler front end source under dual (GPL and Artistic) license
- Sample D programs
- D symbol demangler
- D manual lookup tool
- D compiler executable
- D's package manager
- D source code minimizer
- \dmd2\windows\bin$(LINK2 http://www.digitalmars.com/ctg/make.html, make.exe)
- Digitalmars Make
- Find/replace text in files
- D build tool for script-like D code execution
- Global compiler settings
- \dmd2\windows\bin$(LINK2 http://www.digitalmars.com/ctg/shell.html, shell.exe)
- Simple command line shell
- D runtime library
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:
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 optlink.exe.
Linking 64 bit programs is done using the Microsoft linker by default. If the Microsoft linker is not found, the included LLVM linker LLD will be used.
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 optlink.exe along the PATH. To use a specific linker for 32 bit programs instead, set the LINKCMD environment variable to it. For example:
- dmd normally runs the linker by looking for optlink.exe along the PATH. To use a specific linker for 64 bit programs instead, set the LINKCMD64 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 optlink.exe is found first in the PATH before other link.exes, or use LINKCMD to specifically identify which linker to use.
set LINKCMD64=C:\Program Files (x86)\Microsoft Visual Studio 15.0\VC\bin\amd64\link.exe
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" "-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.
- 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.
- String literals are read-only under Linux. Attempting to write to them will cause a segment violation.
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.
The -i flag can be used to automatically compile imported 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 abc.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 phobos.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 phobos.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 \dmd2\src\dmd make -f win32.mak
Complete source code is provided to build Phobos, the D runtime library. Follow these steps:
cd \dmd2\src\druntime make -f win32.mak cd ..\phobos make -f win32.mak