官术网_书友最值得收藏!

  • D Cookbook
  • Adam D. Ruppe
  • 658字
  • 2021-07-16 11:50:43

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:

  1. Download the DMD compiler from http://dlang.org/download.html.
  2. 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 your PATH environment variable so you do not need to use the full path each time you run the compiler.
  3. 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!");
    }
  4. Compile the program. In your command prompt, run the following:
    dmd hello.d
    
  5. 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.

主站蜘蛛池模板: 镇巴县| 灵丘县| 塔城市| 瑞昌市| 福建省| 婺源县| 揭西县| 咸宁市| 确山县| 金阳县| 西和县| 会同县| 通榆县| 平乐县| 宁城县| 通榆县| 连江县| 台前县| 张家口市| 佛学| 资源县| 合肥市| 江安县| 罗甸县| 中山市| 淳安县| 报价| 竹山县| 龙川县| 武鸣县| 嵩明县| 全南县| 冷水江市| 息烽县| 铜梁县| 来凤县| 辽源市| 大同县| 宝清县| 兴和县| 万盛区|