Installing the compiler and writing a "Hello World" program
You're going to create your first D program; a simple "Hello World" program.
How to do it…
Let's execute the following steps to create your first program:
Download the DMD compiler from http://dlang.org/download.html.
If you use a platform installer, it will install. If you use a ZIP file, simply unzip it and use it in place of the platform installer. The binaries for each operating system are found in
dmd2/your_os_name/bin
. You may choose to add this directory to yourPATH
environment variable so you do not need to use the full path each time you run the compiler.Create a file with your favorite text editor with the following content and name it
hello.d
:import std.stdio : writeln;;; void main() { writeln("Hello, world!"); }
Compile the program. In your command prompt, run the following:
dmd hello.d
Run the program as follows:
hello
You should see the following message appear:
Hello, world!
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
How it works…
The DMD compiler is the key tool needed to use D. Although IDEs exist, you won't be using them in this book. So, you'll learn how to use the compiler and be better equipped to handle problems encountered during the build process.
D source files are Unicode text, which are compiled into executable programs. The DMD compiler, by default, generates an executable file with the same base name as the first file passed to it. So here, when you called dmd hello.d
, it created a file named hello.exe
on Windows or hello
on Unix systems. You can change the output file with the dmd –of
option, for example dmd –oftest hello.d
will create a file named test.exe
. You'll learn more about the options of dmd
as and when they'll be required.
Next, let's look at each of the lines of hello.d
, beginning with the following import
statement:
import std.stdio;
A D program is composed of modules. Each module is a file, but unlike C or C++, where you use textual #include
directives, D uses a symbolic import
. When you import a module, its public members become available for use. You can import the same module multiple times without any negative effect, and the order of top-level imports does not matter.
In this case, you're importing the module std.stdio
, which is a part of the standard library that provides input and output functions, including the writeln
function you'll use later in the code. Next, let's discuss the following main()
function:
void main()
D programs typically begin execution at the main()
function. D's main()
function can optionally take command-line arguments of type string[]
, and they may return either void or integer values. All forms are equally valid.
Tip
It is possible to write D programs that start somewhere other than main()
, which allows you to bypass D runtime initialization. You'll see this in Chapter 11, D for Kernel Coding.
Here, you're returning void because you aren't returning any specific value. The runtime will automatically return zero to the operating system upon normal termination, and it will return an error code automatically if the program is terminated by an exception. Now, let's look at the following output function:
writeln("Hello, world!");
Finally, you'll call the function writeln
from the std.stdio
module to say Hello, World!. The writeln
function can take any number of arguments of any type, and it will automatically convert them to string for printing. This function automatically adds a newline character to the end of the output.
There's more…
Here, you used the DMD compiler. There are two other major D compilers available: GDC and LDC. You can learn more about these at http://gdcproject.org/ and http://github.com/ldc-developers/ldc, respectively.