Welcome to D!

In the past classes, we have used JavaScript and Lua to write code. For this class, we are going to be using the D Programming Language to build simple text-based and 2-d games. D is a compiled language, which is different from languages we have been using so far. A compiled language converts or "compiles" the source code into machine code, which is then run directly by the computer. No translation or interpretation is needed once it is in machine code, which means it can run faster (generally speaking) than interpreted languages.

D also can directly use C compiled code. The C Programming Language is a compiled programming language that has been around since 1972, and still remains the most popular programming language for compiled code in existance. We will use D because it is a much nicer language than C, but because we can call into C libraries with almost no effort, we get access to all the libraries that C developers have written, including some nice 2-d graphics libraries.

To use this, we need to install a compiler for D, and an editor/debugger that supports D. We will be using Visual Studio Code (not to be confused with Visual Studio), along with the code-d extension that adds D support. This document will get you through the process to install all the tools necessary, and to compile and run your very first D program.

Please follow these instructions EXACTLY, and if you need help with anything, let me know on discord.

Install Visual Studio tools

NOTE: If you are using an operating system other than Microsoft Windows, DO NOT follow these instructions, please let me know, and I can help you install it.

Even though we will not be using Visual Studio, we still need to install the C/C++ compiler, and the tools associated with it. D needs a C library to use, and this is the main one on Windows. Luckily, we don't have to install the full version.

Go to the Download Page, and click on the correct item:

Select the C/C++ workload, and then click install:

When the installation is complete, you will see this page, you can just close it:

Install the D compiler

Download the D compiler from the D Download page. Click on the Windows installer and follow the instructions as identified in these images (leave everything as the default):

At this point, make sure you reboot your computer. This step is important, or the compiler will not be found by the editor.

If you are successful, you now have all the tools installed needed to build D programs. However, we also want a nice environment to edit and build our projects.

Install Visual Studio Code

Visual studio code can be found here. Click on the download button as shown in this image:

As before, click on the "Run" button, and accept all defaults. At the end, it asks you to run vscode, you should do this now.

You will see a welcome page that looks like this:

Click on the "Extensions" button as indicated. This will show extensions you can install to edit different types of files. Type in the term dlang as shown here:

The extension we are going to install is called code-d by "WebFreak". There are two options, select the one that says "utility extension package". That actually includes the other one, as well as debugging support.

Now we need to install a debugger. Because D is so closely related to C, we can install the C/C++ extension, which includes Microsoft's excellent debugger software. Do this by searching for C++ in the extension window, and installing the one that just says C/C++ by Microsoft:

Compiling and running a D program

To test that everything is working (we won't be using the debugger yet, but this will at least test your D compiler), let's create a new project and build and run it!

  1. Click on "File" and "Open Folder" in VS code
  2. In the window that comes up, find a good place (maybe your Desktop or in Documents), and click on "New Folder" at the top of the window:
  3. Enter a name for your new program (maybe 'testing'). Then click on "Select Folder"
  4. Now, we are going to initialize a D project. To do this, hold down CTRL and SHIFT while pressing P. This will bring up what is called the "Command Palette". In the command palette, type "code-d", which brings up possible commands to run with the code-d extension. Select "code-d: Create new Prject"
  5. This might take some time, but will eventually give you a list of project types to choose from. Pick the one that says "Empty Console Application":
  6. Now, edit the main source file (it is inside the 'source' directory, and it's called 'app.d'), and add the line writeln("hello, world"); inside the main function. Make sure the line looks exactly the same, including the final semi-colon:
  7. Once the file is finished, you can run the program by using the key sequence CTRL-SHIFT-B. This will bring up a menu choice on how to build. There are 2 build types, a "run" type and a "build" type. The "build" type just compiles your program but doesn't run it, so use the "run" type:
  8. This will build and run your program. If you do not have any errors (and you shouldn't if you wrote the code exactly as I have), you will see in the terminal window below, the program building, and printing the words "hello, world" to the screen:

You now have a program!

Congratulations! you just built your first executable program. You can run it directly from a command line if you want to see it work without Visual Studio Code. Click on the start button and type 'cmd'. Then, run the Command Prompt app. Inside the app, you will be in your home directory. type cd Desktop\testing, and press enter. You are now in the directory of your program. Type dir, to see your program named testing.exe. type testing to see your program run!

©2020 Steven Schveighoffer