Rocking the blogosphere

Archive for the 'D' Category

More adventures with D

A few weeks ago, I messed around with dmd (the original compiler for the D programming language).

Well, here’s a bit of info about gdc, a D language front end for gcc.

I couldn’t find a package in the Ubuntu repositories, so I downloaded a Linux binary and installed it:

$ wget http://umn.dl.sourceforge.net/sourceforge/dgcc/gdc-0.22-i686-linux-gcc-4.1.1.tar.bz2
$ tzx gdc-0.22-i686-linux-gcc-4.1.1.tar.bz2
$ cd gdc
$ cd bin
$ sudo cp * /usr/local/bin/
$ cd ../lib
$ sudo cp -r * /usr/lib
$ cd ../include
$ sudo cp -r d /usr/include
$ cd ../libexec
$ sudo cp -r * /usr/lib
$ gdc -I/usr/include/d/4.1.1 hello.d -o hello
$ ./hello
$ ./hello
hello world
args.length = 1
args[0] = './hello'

Here’s the program I used:

$ cat hello.d
int main(char[][] args)
{
    printf("hello world\n");
    printf("args.length = %d\n", args.length);
    for (int i = 0; i < args.length; i++)
        printf("args[%d] = '%s'\n", i, cast(char *)args[i]);
    return 0;
}

or

marc@tbird:~/sw/gdc$ cat hello.d
import std.stdio;

int main(char[][] args)
{
    printf("hello world\n");
    printf("args.length = %d\n", args.length);
    for (int i = 0; i < args.length; i++)
        writefln("args[%d] = '%s'\n", i, args[i]);
    return 0;
}

but for the latter, I needed to modify the compile command:

$ gdc -I/usr/include/d/4.1.1 -I/usr/include/d/4.1.1/i686-pc-linux-gnu hello.d  -o hello

(It would be nice to build and install from the source code - my guess is that would eliminate those ugly -I options).

Playing with D

I installed the Digital Mars D compiler and ran into a problem initially, but it was easy to fix, though it seems that the installation doc should be beefed up a bit.

~/dmd/samples/d$ dmd hello.d
object.d: module object cannot read file 'object.d'
~/dmd/samples/d$ sudo vim /etc/dmd.conf
...
~/dmd/samples/d$ cat /etc/dmd.conf 

[Environment]

DFLAGS=-I%@P%/../src/phobos -I/home/marc/dmd/src/phobos -L-L%@P%/../lib
~/dmd/samples/d$ dmd hello.d
gcc hello.o -o hello -m32 -lphobos -lpthread -lm -Xlinker -L/etc/../lib

Not too bad. Next up, I’ll play with gdc, another compiler for the D language.