Getting Started with d2c

This section explains the structure of a simple Dylan program, and shows how to use d2c

A simple Hello World

Usually, a Dylan program consists of multiple libraries, which contain multiple modules, which again can be spread across multiple files. While this is powerful, it's a little overkill for something small and simple like a "Hello, World" application.

When you need just one module, in a single source file, with no complex module import and export, you can use d2c's single file mode. For instance, you can put the following into a file called hello.dylan:

module: hello

format-out("Hello, World!\n");

The structure should be obvious: the first line is a header line, specifying the module this code belongs to. Since we are in single file mode, the library and executable name is derived from the module name too. Single file mode will generate proper module and library definitions for you.

After the empty line terminating the header is a line of code, which simply prints the message "Hello World!" on the screen, followed by a newline. There's no entry point, all the code lines are executed in order, just like in a perl or shell script.

To compile the program, just pass the name as an argument to d2c:

	$ d2c hello.dylan
	$ ./hello
	Hello, World!
        $
	

Structure of a Dylan Program

A Dylan program according to the Dylan Interchange Format consists of several files. These are the Library Interchange Definition (or LID) file, the inport/exports file, and one or more source files.

The LID file instructs the compiler how to build a Dylan library or application. It includes a list of source files, compiler options and other descriptive information. It can be identified by the file name extenion lid.

A second file lists all the function and variable names imported and exported by the program. This generally ends in -exports.dylan or -library.dylan. Technically, this file consists of Dylan source code, but it almost never contains any declarations but define library or define module.

Other files end in the extension dylan and contain the actual Dylan source code.

Using make-dylan-app

Because d2c requires such a complex set of files for even a simple program, it's usually best to take advantage of the program make-dylan-app to create a new application. To use this, cd to an appropriate directory and type make-dylan-app program-name. This will create a new directory containing a simple program which uses a set of libraries useful for a command line utility.

A Complete Hello World

Create a new program called hello by running the command make-dylan-app hello. This will create a new directory named hello. Go there by typing cd hello and take a look at the files.

Open up the file hello/hello.dylan and fill in reasonable values for the author: and copyright: keywords. You will notice that make-dylan-app already filled in a statement printing "Hello, World!" for us, as a placeholder for the code we want to add to the program.

module: hello
synopsis: Print the string "Hello, World!"
author: J. Random Hacker <jrandom@randomhacks.com>
copyright: Copyright 1998, J. Random Hacker

define function main(name, arguments)
  format-out("Hello world!\n");
  exit-application(0);
end function main;

// Invoke our main() function.
main(application-name(), application-arguments());

The three other files in this directory are a Makefile for compiling your program (it is not strictly needed, as you could call d2c directly), an export declaration file named hello-exports.dylan, which declares the libraries and modules that are used by your program, and a LID file listing all of your Dylan source files (two in this case) plus the name of the resulting executable.

To compile the program, simply type make. Alternatively, call d2c, passing it the LID file as a parameter, as in d2c hello.lid. You can now run your program by typing ./hello.

Note that, when using shared libraries, the file hello is just a script that calls the real program .libs/hello. The reason for that is rather intricate and has to do with the rpath to shared libraries which are part of your project. In other words, don't worry about it now, and read the libtool documentation when you need more advanced features.