Using VSCode as IDE for D development.
"D is a general-purpose programming language with static typing, systems-level access, and C-like syntax. With the D Programming Language, write fast, read fast, and run fast." This is how they describe D on the dlang.org webpage.
But, what exactly is D? How does it differ from C/C++? The D programming language is developed in-house since 2006 by Digital Mars, a small US software company, also known for producing a C compiler (Digital Mars C++; DMC++). While D originated as a re-engineering of C++ and is predominantly influenced by it, D is not a variant of C++. D has redesigned some C++ features and has been influenced by concepts used in other programming languages, such as Java, C# and Eiffel. As such, D is an evolving open-source system programming language, supporting multiple programming paradigms: procedural, generic, functional and object-oriented. Most notably it provides very powerful, yet simple to use, compile-time meta-programming facilities. You can read further details at the WikiBooks website.
A very detailed comparison is published at the Slant website, where they write: "The most important reason people chose D is: With few exceptions, D will either compile C code to work exactly as when compiled as C, or it won't compile – it won't quietly change the semantics of C code and give unexpected results. This means that D contains an improved C, as it fails compilation where type safety is missing in C. This allows learning the same machine operations available in C and other low-level languages." It seems that D has several major advantages over C/C++. However, D as a language has been poorly adopted, even after many years of existence; it failed at becoming the alternative to C or C++.
This tutorial is about the installation of the D reference implementation DMD (and its usage in VSCode; cf. further down in the text) on Windows 10; the D release that I used is DMD 2.109.0 (the 64-bit version). You can download the Windows installer from the D language download page.
When installing the product, be sure that in the components selection window, the checkbox near "Add to PATH" is selected. Optionally, you can choose to download and install a C compiler (either Visual C, or DMC).
The default installation folder is C:\D. As always in the case where an application is to be installed immediately beneath the root, I changed it to C:\Programs\D.
To build programs targeting the VC runtime, a corresponding component has to be installed. Visual Studio 2019 is selected by default. I suppose that this is not what you want, and that you probably will prefer to do the same that I did, and only install the VC2010 redistributables.
The screenshot below shows the license agreement during the installation of Microsoft Visual C++ 2010 x86 Redistributable. This installation is launched right at this point; installation of DMD will continue when done...
The DMD files are copied, and that's it.
Here is the code of a simple "Hello World" program in D. I named it hello.d and saved it to E:\Work (you may use any directory that you want...).
import std.stdio;
void main(string[] args) {
writeln("Hello World!");
}
To build the program, open Command Prompt, navigate to the directory that contains the source file, and run the command:
dmd hello.d
Using VSCode as IDE for D.
Visual Studio Code (VSCode) is a lightweight but powerful free and cross-platform source code editor by Microsoft. Thanks to extensions, it may be used as IDE for a very large number of programming languages. If you haven't VSCode installed on your computer and need help for doing do, my tutorial Installing VSCode on Windows 10 may be helpful.
You can install the D Programming Language (Code-D) extension from within VSCode, or (as I did) download it from Visual Studio Marketplace and then use the VSCode feature Install from VSIX... You find this feature by choosing Extensions (fifth icon from top) in the left navigation bar, and click the three dots to open a menu with available commands.
When the installation is done, the message serve-d is not installed or cannot be found. This is not an error, it's simply that serve-d is not included with the extension installation file. To install serve-d, simply push the Reinstall serve-d button. It's obvious, that you need to be connected to the Internet to do the update.
The screenshot below shows the description of the Code-D extension.
Creating a D project using VSCode is easy. Select Explorer in the left navigation bar (1st icon from top), and click the Open Folder button; then, browse to the folder that you have created (before) for this project (for my first D example with VSCode, I used D:\Programming\VSCode\hello_d). You'll have to confirm that you trust the author of this folder. Then, in the main tab, click the New File... link. I named my file "hello.d".
Now, enter the source code for hello.d. To build and run the program, choose Run Code (triangle icon, at the upper-right corner of the IDE window). This will create hello.obj, and hello.exe; hello.exe will directly be executed.
Here is the code of a simple program (minmax.d) that calculates the minimum and maximum of an array. I created it in D:\Programming\VSCode\minmax using VSCode.
import std.stdio;
void main() {
int counter;
double min, max;
double[10] values;
counter = 0;
while (counter < values.length) {
write("Value ", counter + 1, ": ");
readf(" %s", &values[counter]);
++counter;
}
min = values[0]; max = values[0];
counter = 1;
while (counter < values.length) {
if (values[counter] < min) {
min = values[counter];
}
if (values[counter] > max) {
max = values[counter];
}
++counter;
}
writeln("Minimum value is ", min);
writeln("Maximum value is ", max);
}
No idea why, but running this code in VSCode didn't produce any program output (?). As you can see on the screenshot below, the build has been done (minmax.obj and minmax.exe have been created). The program just runs and continues running. To stop it, choose Stop Run Code (square icon in the upper-right corner of the IDE window).
This does not mean that you have to use Command Prompt to run the executable. In the "messages" pane, choose the Terminal tab, and enter ./minmax (do not forget the "./" prefix; similar to Linux operating systems, VSCode does not add the current directory to the PATH).
If you find this text helpful, please, support me and this website by signing my guestbook.