Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 36 ยท Getting Started

Compilers, Flags and the Build Process

Learn how a D program goes from source to executable, which dmd, ldc2 and gdc flags matter, how to build a multi-file project with dub, and how to read compiler errors.

Writing code is half the job; turning it into a program is the other half. D's toolchain is gentle: a single command builds most programs, and dub handles larger ones. This lesson explains what the compiler does, the flags you will use daily, how modules map to files, and how dub fits in. The listings are read-only; the C stand-in at the end models the compile-and-link pipeline.

From source to executable

A D build has three stages, the same as C:

flowchart LR
    SRC["main.d, util.d"] --> OBJ["compile: main.o, util.o"]
    OBJ --> LNK["link with Phobos and druntime"]
    LNK --> EXE["executable"]

The compiler turns each module into an object file; the linker joins them with the standard library.

D programs always link against two libraries: druntime (the runtime: garbage collector, arrays, exceptions, threads) and Phobos (the standard library, std.*). dmd links them for you, so you rarely notice.

Everyday dmd flags

Flag Meaning
-run Compile and run immediately
-O Turn on optimizations
-release Remove asserts, contracts and bounds checks in @system code
-inline Inline small functions
-g Add debug information
-unittest Compile unittest blocks and run them at startup
-w Treat warnings as errors you must fix
-c Compile only, do not link
-of=name Name the output file
-i Compile imported modules automatically

Typical development build: dmd -g -unittest -w app.d. Typical release build: ldc2 -O3 -release app.d.

Notice that D checks array bounds in normal builds. -release removes some checks for speed, but only do that after tests pass. The -boundscheck=off option exists, but it is a last resort.

One module, one file

In D, every source file is a module. By default the module name is the file name, and you may declare it at the top:

D
module geometry.shapes;

double circleArea(double r)
{
    import std.math : PI;
    return PI * r * r;
}

The path matters: geometry/shapes.d must contain module geometry.shapes; so that another file can write import geometry.shapes;. You compile everything together:

Terminal
dmd main.d geometry/shapes.d

or let the compiler find them with dmd -i main.d (or just rdmd main.d). There are no header files and no include guards: the compiler reads imported modules directly, which is one reason D compiles fast.

dub: build tool and package manager

dub reads a project description and handles compiling, dependencies and tests. A minimal dub.sdl:

TEXT
name "hello"
description "A tiny demo"
authors "Your Name"
dependency "vibe-d" version="~>0.10.0"

Common commands:

  • dub init myapp creates a skeleton with source/app.d.
  • dub build compiles; dub run compiles and runs.
  • dub test builds with -unittest and runs your tests.
  • dub build --build=release --compiler=ldc2 picks release mode and the LDC compiler.
  • dub add some-package records a dependency from the registry at code.dlang.org.

The default folder layout is source/ for code and dub.json or dub.sdl at the root. Put a source/app.d containing void main() and the project works with no further setup.

Reading compiler errors

D's error messages include the file, line and the construct involved:

TEXT
app.d(7): Error: undefined identifier `cont`, did you mean variable `count`?
app.d(9): Error: cannot implicitly convert expression `3.5` of type `double` to `int`

Fix the first error first: later ones are often consequences. Two helpful habits: compile often (D compiles in a fraction of a second for small files), and use -w so warnings never pile up.

A stand-in for the build pipeline

The pipeline above is the same in C. This C stand-in reproduces "compile to separate units, then link": each "module" is a function that registers a number, and main plays the linker, combining them. It is a model, not a build.

C C stand-in: separate units joined at link time

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The three lines print 49, 12 and 36. In real D each function would live in its own module file and be imported by name; the compiler checks the types across the boundary, something a C linker alone does not do.

Choosing flags for the job

C C stand-in: what -release removes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

With normal flags the assertion is active and would stop the program if n were zero. Building with -DNDEBUG (C) or -release (D) removes the check. The point for D: keep checks on while developing, and measure before turning them off.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the purpose of the -unittest flag?

    -unittest enables the built-in unit tests.
  2. 2. A file geometry/shapes.d should begin with which module declaration?

    The module name mirrors the directory path.
  3. 3. Which tool manages dependencies and builds whole D projects?

    dub is the official build tool and package manager.
  4. 4. Which two libraries does every D program link against?

    druntime is the runtime, Phobos the standard library.

Summary

  • A D build compiles modules to objects and links them with druntime and Phobos.
  • Use dmd for fast development builds and ldc2 for optimized release builds.
  • Each file is a module; there are no headers.
  • dub builds projects, runs tests and downloads packages.
  • Read the first compiler error first.

Next: Program structure: modules, imports and printing