DMD Compiler for Linux
- Requirements and Downloads
- Compiler Arguments and Switches
- Environment Variables
- dmd.conf Initialization File
- Differences between Windows and Linux versions
- D Interface Files
- Building Libraries
- Compiling dmd
- Compiling Phobos
- Download D Compiler
- 32 bit x86 and 64 bit x86-64 Linux operating system
- Gnu C compiler (gcc)
- D runtime library source
- D compiler front end source under dual (GPL and Artistic) license
- Sample D programs
- D compiler executable
- Elf file dumper
- Elf file disassembler
- Simple command line shell
- Global compiler settings (copy to /etc/dmd.conf)
- D runtime library (copy to /usr/lib/lib)
cp dmd2/linux/bin/dmd.conf /etc
cp dmd2/linux/lib/lib /usr/lib
- dmd files... -switches...
File Extensions Extension File Type none D source files .d D source files .dd Ddoc source files .di D interface files .o Object files to link in .a Object code libraries to search
- 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
- omit generating some runtime information and helper functions
- 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
- 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
Perform code coverage analysis and generate .lst file with report.
dmd -cov -unittest myprog.d
- 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.
- generate documentation from source
- write documentation file to docdir 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.
- treat use of deprecated features and attributes as warnings (this is the default)
- treat use of deprecated features and attributes as errors
- 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 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 lib. If libname is not supplied, then no default library is linked in.
- Without filename, print module dependencies (imports/file/version/debug/lib). With filename, write module dependencies as text to filename (only imports).
- implement http://wiki.dlang.org/DIP25 (experimental)
- generate Position Independent Code (which is used for building shared libraries).
- add symbolic debug info in Dwarf format for debuggers such as gdb
- backwards compatibility option for the the -g switch
- always generate standard stack frame
- add stack stomp code
- generate D interface file
- write D interface file to dir directory. -op can be used if the original package hierarchy should be retained
- 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-M
- 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.
- compile a 32 bit executable. This is the default for the 32 bit dmd.
- compile a 64 bit executable. This is the default for the 64 bit dmd.
- 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.
- open browser specified by the BROWSER environment variable on this page. If BROWSER is undefined, x-www-browser is assumed.
- generate a .map file
- Set the target architecture for code generation, where:
- 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.
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.
- -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 .o or executable file is left behind.
- generate shared library
- show additional info about language change identified by id
- list all language changes
- compile in unittest code, turns on asserts, and sets the unittest version identifier
- print character (column) numbers in diagnostics
- limit the number of error messages (0 means unlimited)
- compile in version level >= level
- compile in version identifier ident
- list all gc allocations including hidden ones
- print informational messages identifying variables defaulting to thread local storage. Handy for migrating to shared model.
- enable warnings
- enable informational warnings (i.e. compilation still proceeds normally)
- generate JSON file
- write JSON file to filename
Linking is done directly by the dmd compiler after a successful compile. To prevent dmd from running the linker, use the -c switch.
The actual linking is done by running gcc. This ensures compatibility with modules compiled with gcc.
The D compiler dmd uses the following environment variables:
- dmd normally runs the linker by looking for gcc along the PATH. To use a specific linker instead, set the CC environment variable to it. For example:
- This sets the browser used to open the manual page with the -man switch. It defaults to x-www-browser.
- The value of DFLAGS is treated as if it were appended to the command line to dmd.
The dmd file dmd.conf is the same as sc.ini for Windows, it's just that the file has a different name, enabling a setup common to both Windows and this system to be created without having to re-edit the file.
dmd will look for the initialization file dmd.conf in the following sequence of directories:
- current working directory
- directory specified by the HOME environment variable
- directory dmd 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.
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:
; dmd.conf 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] DFLAGS=-I%@P%/../src/phobos -I%@P%/../src/druntime/import
- 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.
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.o and existing library def.a which are all to be combined into a library foo.a:
- Compile modules separately and then run the librarian on them:
dmd -c foo.d dmd -c bar.d rm -f foo.a ar -r foo.a foo.o bar.o abc.o def.a rm foo.o bar.oThis 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 rm -f foo.a ar -r foo.a foo.o bar.o abc.o def.a rm foo.o bar.o
- Use dmd to compile and build library in one operation:
dmd -lib foo.d bar.d abc.o def.aNo 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 posix.mak
Complete source code is provided to build Phobos, the D runtime library. Follow these steps:
cd ~/dmd2/src/druntime make -f posix.mak DMD=~/dmd2/linux/bin/dmd cd ../phobos make -f posix.mak DMD=~/dmd2/linux/bin/dmd