Getting started with the D Programming Language on OS X YosemiteFri, Sep 11, 2015
The D Programming Language is
a language with C-like syntax and static typing. It pragmatically combines efficiency, control, and modelling power, with safety and programmer productivity.
In this post, I’m going to quickly walk through getting a working D compiler setup on OS X Yosemite, using Homebrew.
I’m going to assume you’ve already got Homebrew installed and have a working installation. There are many guides on setting Homebrew up, so I’ll leave that to others.
Installing the D compiler, DMD
DMD is the standard D compiler, though there are others such as GDC (which targets GCC) and LDC (which targets LLVM). Usually it’s advised that you download the DMD package from the download page, but installation view Homebrew makes future upgrades far easier (and automates the installation process). To install DMD, run the following command:
brew install dmd
This may take a short while to complete, but once done you should have a working D compiler.
Testing the DMD compiler
Once you’ve got the DMD compiler installed, you’ll want to test it works. The easiest way is to create a file
hello.d on your desktop with the following content:
Now to compile the file:
This shouldn’t throw any errors, but if it does then something is either wrong with your install or the source code you put in the file. If all went well, you should have an executable
hello on your desktop. Run it as follows to see the output
Installing DUB - the D package manager
DUB is a package manager for the D Programming language, which makes managing dependencies for your programs super easy. If you’re writing any amount of D code, DUB will likely be very useful.
Luckily, installation is a breeze using Homebrew:
brew install dub
Creating a new project
DUB’s command line tool can help you to bootstrap new projects with a simple command. This is extremely useful as it scaffolds all of the required filesystem structure and creates the DUB package description file (
dub.json). To create a new package called
foo, run the following:
dub init foo
This will create a directory
foo, with the following content:
foo └── dub.json └── source └── app.d
dub.json should contain the following as standard:
There are many more entries you can add to this package specification, all of which are detail in the package file format specification.
Compiling DUB based projects
DUB can be used to build packages. To do so, simply run
dub build (or just
dub) from within a package directory. This will create a single statically linked binary that you can run and/or distribute easily.