BEV GDC Newbie Page
Contact   BEV Current Month   Software   Show/Hide Feedback   Show/Hide Blog  


What Is This

Before going any further, you should know that this page is hopelessly past its sell-by date. GDC is now much further advanced then when I made my attempt. It does now offer a current version of the D programming language. To get up-to-date information you'd be better off visiting the D language newsgroups. The current starting pojnt being dlang.org. If you have questions, feel free to email me throught the contact link at the top if the page. I'll do my best to help.

Otherwise, this was a help page and blog about the GDC (GCC D Compiler) for the D programming language. It is aimed at beginners with GDC, not necessarily at beginners with D. It consists of: This is a preliminary version of the page. If you have comments, please feel free to contact me by email or just click here.
Introduction.

If you are a beginner with D you will probably already have visited the Digital Mars page. If you haven't, then go there right now.

Otherwise, let's first look at the state of GDC, and what it is. GDC has been around for some time (when needed), but for a variety of reasons, it fell into a state of dormancy, which is a pity. Why? Well because GDC represents one of the best options for people to learn the D programming language - which I believe all programmers in the year 2010 should be aware of, and enjoy. It is available in Ubuntu, the most popular free operating system, and there's a decent IDE for it that is also available in Ubuntu. You can also run it under Windows - same comments.

The good news is that GDC now seems to have emerged from its dormancy, and is the subject of active development again - so active in fact that it is difficult to determine what the current version is ;=)

What Is GDC?

So, what is it. Well GDC is a D compiler based on GCC (the Gnu Compiler Collection). It uses the front-end code of the DMD (Digital Mars) D compiler, and GCC's back end. It is however, not yet an official part of the GCC. The advantage of using the GCC back end is that GDC gets to use a lot of facilities like the GCC linker and loader, and the GCC code that actually creates object files. All of the latter code is the subject of constant development and refinement.

Digital Mars D, in contrast, uses a now rather old back end and object file format, which rather limit its development in certain directions, notably in its ease of creation and use of shared libraries (DLLs or .so files). On the other hand, DMD is miles ahead in terms of its current version - its extent of development. That situation though might now be changing.

Players

The players in the GDC world that I know about are as follows (if I've left you out please get in touch):

Walter Bright

Walter is the originator and primary developer of the D programming language, who has generously made the DMD compiler front end code available to projects such as GDC, and is constantly developing it.

David Friedman

David first grafted the DMD front end onto the GCC back end around 2006.

Anders F Björklund

In Anders' own words:

I have been doing some GDC/GDB installers:
  • http://gdcwin.sourceforge.net/ (MinGW)
  • http://gdcgnu.sourceforge.net/ ("FSF")
  • http://gdcmac.sourceforge.net/ (Apple)
Mostly for use with wxD and with Code::Blocks, and to make it easier to get started with D... either the old GDC 0.24 release, or the "stable" DMD 1.030 based. The GDB is with the D patches.

Michael P

Right now, I mainly work on merging in D1 DMD front ends for GDC. I am also trying to do some others to get GDC a bit more organized, such as a folder for testing out pieces of D code to make sure GDC is working properly, and a folder that will document files specific to GDC and possibly some in the DMD front end and Phobos sources.

Vincenzo Ampolo

I’ve been working with ldc for a long time. I enjoyed it, i helped the development with tickets and hacking ldc a bit. I even tried to compile Tango as a shared library but i got an error, probably caused by the compiler since people reported that in gdc they were able to do it. I then started guessing if llvm is so stable as it seems and if it can substitute the gnu compiler collection. Then i came to these llvm vs gcc benchmarks. Well the result is that llvm is not so fast and reliable as promised. In the same days, a message posted on the D newsgroup got me. I started thinking that resurrection gdc is the only way to make a stable and reliable compiler accepted in the FOSS community for the D programming language. But it should resurrect it should implement the D2 specs: there are no other compilers than dmd which support D2 around. So, me and Michael joined to the challenge to work on gdc.

We got the latest working branch of gdc, the one who relies on gcc 4.3.1. We then copied in our repository in bitbucket and applied some changes, after two days of works, gdc for D2.014 worked under gcc 4.3.4.

Arthur Loiret

Maintains the GDC package for Ubuntu.

Getting Started.

The easiest way to get started that I have found is with Ubuntu, where you can do:
sudo apt-get install gdc
Then if you type gdc -v in a terminal, you should get:
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,d --prefix=/usr --enable-shared
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--enable-nls --with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2
--enable-clocale=gnu --disable-libmudflap --enable-targets=all --enable-checking=release
--build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.2.4 20080705 (prerelease gdc 0.25 20080312, using dmd 1.024) (Ubuntu 0.25-4.2.4-3.1)
If you create a file:
import std.stdio;

void main()
{
   writefln("Hello World - D");
}
and save it as hello.d in your home directory, then do
gdc hello.d
./a.out
You should see the conventional result.

For initial installation in other environments, see below.

Which IDE

I know that many users will not be happy developing D programs using the command line tools, so the next major question is "what IDE can I use". There are a number of reasonable answers to this question, but for now, since I am also a newbie in this field, I'll talk about what I know.

You can also conveniently install Code::Blocks in Ubuntu:
sudo apt-get install codeblocks
When you have done so, I hope you will be pleasantly surprised to find that GNU D Compiler appears in the list shown from Settings -> Compiler and Debugger. For now, select it as the default compiler.

Delete files hello.o and a.out from your home directory, then do File -> Open, and select hello.d. Do Build -> Build, and you should see a success message in the panel at the bottom. Then do Build -> Run. This will hopefully bring a rosy glow of satisfaction.

Hold your horses. Modify hello.d as follows:
import std.stdio;
import std.string;

void main()
{
   writefln("Hello World - D");
   int n = atoi("42");
   writefln("n = %d", n);
}
Then go into Settings -> Compiler and Debugger, and select the option "Enable all compiler warnings". Do Build -> Build again and now the bottom panel should show an error with some strange features:
  • File: warning - hello.d
  • Line: 7
  • Message: Error: implicit conversion of expression (atoi("42")) of type long to int can cause loss of data
  • Outcome: 1 errors, 0 warnings
This is nonsense. It isn't an error, it's a warning, and the file name is wrong. Because of that, the line in your program where the supposed error occurred is not correctly highlighted with a red bar.

To get this corrected you have to go to a somewhat scary dialog. Settings -> Compiler and Debugger -> Other Settings -> Advanced options -> Output parsing. Select Parsing expressions -> Compiler warning. There, change the regular expression to:
warning - ([][{}() \t#%$~A-Za-z0-9_:+/\.-]+):([0-9]+):[ \t](Error:[ \t])?(.*)
Close the settings dialog, then build again. Now you should see:
  • File: hello.d
  • Line: 7
  • Message: implicit conversion of expression (atoi("42")) of type long to int can cause loss of data
  • Outcome: 0 errors, 1 warnings
This is correct.

The next stumbling block will occur when you try to write a more complicated program that uses a module you have created. Modify your hello world again:
import std.stdio;
import std.string;
import me.thingie;   // where me is your user - home directory - name

void main()
{
   writefln(thingie());
   int n = atoi("42");
   writefln("n = %d", n);
}
Now build, and you will see the dreaded message "module thingie cannot read file 'me/thingie.d'" - aargh!

In the first instance this is because CB is looking in the wrong place - the place where the Phobos library sources are located. Go to Settings -> Compiler and Debugger -> Search directories, and add a search path "../" (without the quotes). Remember that when you tell D to import me.thingie, you are asking it to find a directory/file combination of me/thingie.d, so you have to give it the path "../", or equivalently "/home". The required path is the path to the directory immediately above where you are working.

So now create file thingie.d in your home directory:
module me.thingie;      // me should be your user name

string thingie()
{
   return "Hello World - D";
}
hello.d should compile now. Leaving this path in CBs global compiler settings is probably harmless, but as a general practice, use Projects, and add the appropriate search paths to the project.

Debugging

The next step toward programming as you know it is to be able to debug. Fortunately, Code::Blocks knows how to do this to some extent for D as well. To start with this we'd better move to a project.

In CB, do File -> Close all files, the start a new project.
  • Project title: hello
  • Folder to create project in: /home/me
  • Project filename: hello.cbp
  • Resulting filename: /home/me/hello/hello.cbp
Take the defaults on the last pane of the wizard. Copy files hello.d and thingie.d to the folder /home/me/hello, then add file thingie.d to the project - hello.d will already be there. Open hello.d, and comment out the line with atoi, and the following line (there's a strangeness about the way CB handles warnings), then build and run.

If that is OK, put a breakpoint on the writefln line (click just to the right of the line number), then do Debug -> Start. A console window will flash up and down, then you should see the debug current line marker on the writeln line, and you should be in business. I'll add more about debugging as I find out.

Installing in Windows/MinGW.

If you don't already have it, you will first need to install MinGW. This is a port of GCC to the Windows environment. For me, the most convenient way to do this is to visit the Twilight Dragon Media page, and download their bundled installer tdm-mingw-1.908.0-4.4.1-2.exe. Run the installer. You'll find it wants to install MinGW on your C drive - go with it.

Then download GDC 0.24 for MinGW32 and GDB-D0.2 for MinGW32 and run those installers to plaster GDC and a version of GDB patched to work with D into the MinGW installation.

Make the following adjustments:
  • Copy crtbegin.o and crtend.o from c:\MinGW\lib\gcc\mingw32\4.4.1 to c:\MinGW\lib (I don't think you should have to do this, but this was the way I made it work - command line and Code::Blocks)
  • Copy libgcc.a from c:\MinGW\lib\gcc\mingw32\4.4.1 to c:\MinGW\lib (ditto)
Add c:\MinGW\bin to the system path in Settings -> Control Panel -> System ->Advanced

Then download Code::Blocks from its web site, and install that. Once it is working skip back up this page to the Code::Blocks section, and generally follow the instructions there, with the following significant differences:
  • Change the warning RegEx to: ^warning - ([{}() \t#%$~A-Za-z0-9_+/\.-]+):([0-9]+):[ \t]Error: (.+)
  • Change the error RegEx to: ([][{}() \t#%$~A-Za-z0-9_+/\.-]+):([0-9]+):[ \t](Error:[ \t])?(.+)
That's not all. Also move Compiler warning and Compiler error to the top of the parsing expressions list with warning first, and in the case of Compiler error, adjust the Sub-expression indices so that message is 4. You can read the reasoning for this on the BEV Software Page.

You should then be good to go in Code::Blocks, or at the command prompt.

Binary Installs in Other Environments.

Since I don't have the alternate systems to go through other OS installs in detail, I can't do any better that refer you to the existing Sourceforge page on this topic.

Once you can do gdc hello.d at a command prompt, you can switch to my discussion of Code::Blocks above.

Building GDC D2 from Source.

OK, I can do this now more or less as per the instructions at bitbucket/goshawk. Don't start this unless you have a few hours to spare - it takes some time.

As I said before, there are some preliminaries that you must get through first - various pieces of Gnu software, namely M4, GMP, and MPFR, in that order, and the build tools autoconf, automake, and patch. If you are going to persevere with further builds of D, you will eventually need the diff utility as well.

Installing M4

Go to the M4 page, and download the latest stable version. Expand it into some convenient folder. If you do it in your home folder you will get a new folder called something like m4-1.4.13. Open a terminal and do:
steve@Ubuntu:~$ cd m4-1.4.13
steve@Ubuntu:~/m4-1.4.13$ ./configure
steve@Ubuntu:~/m4-1.4.13$ make
steve@Ubuntu:~/m4-1.4.13$ make install
- standard Linux build voodoo stuff!

Installing GMP

Go to the GMP page, and download the version 4.3.2. Expand it into some convenient folder. If you do it in your home folder you will get a new folder called gmp-4.3.2. Open a terminal and do:
steve@Ubuntu:~$ cd gmp-4.3.2
steve@Ubuntu:~/gmp-4.3.2$ ./configure
steve@Ubuntu:~/gmp-4.3.2$ make
steve@Ubuntu:~/gmp-4.3.2$ make check
If make check has no errors, continue as below, otherwise - follow the instructions on the GMP page.
steve@Ubuntu:~/gmp-4.3.2$ make install
Installing MPFR

Go to the MPFR download page page, and download the current source, e.g. mpfr-2.4.2.tar.bz2. Expand it into some convenient folder. If you do it in your home folder you will get a new folder called something like mpfr-2.4.2. Open a terminal and do:
steve@Ubuntu:~$ cd mpfr-2.4.2
steve@Ubuntu:~/mpfr-2.4.2$ ./configure
steve@Ubuntu:~/mpfr-2.4.2$ make
steve@Ubuntu:~/mpfr-2.4.2$ make install
Once you've installed these, you can delete the expanded folders, and just keep the downloaded files in case you need them again.

Installing Build Utilities

You will need further standard Linux stuff to support the build process - automake, autoconf, and patch. You can build and install these from source, but if you are using Ubuntu, then
sudo apt-get install patch
and so on will do the trick. The absence of patch was why my build did not work in the first place. You may as well install diff the same way now.

OK, now possibly one more thing. The repository that you will pull the D2 build files from runs the Mercurial source control management tool. If you don't have it, this is another required install. You can do this with apt-get install also.

Nitty Gritty

Now you can get down to Vincenzo's instructions - repeated here to make the story complete. First two more downloads - the D stuff, and a reasonably current version of the GCC source. Open a regular terminal so you are in your home user directory. If you already have a folder called gdc, best rename it for now. Then get the D stuff from the bitbucket repository, and make a folder for the GCC stuff:
    hg clone https://goshawk@bitbucket.org/goshawk/gdc
    mkdir gdc/dev     
Download the GCC 4.3.4 source code from your mirror of choice, and expand it into the gdc/dev folder you just created. Now you should have everything you need to go. You should have a directory gdc/dev/gcc-4.3.4. Run the following commands:
    cd gdc/dev/gcc-4.3.4
    ln -s ../../../d gcc/d
    ./gcc/d/setup-gcc.sh
    mkdir objdir
    cd objdir 
    ../configure --enable-languages=d --disable-multilib --disable-shared
    make
The ln command creates a link that makes th D stuff effectively a part of the GCC source tree. The shell script patches a number of GCC source files to allow for peculiarities of D, and patches configuration and make files to specify that the D compiler and Phobos library are to be built.

After you've run make, go for several cups of coffee, or mow the lawn or something, then hopefully you will find that make has terminated without any errors mantioned in the last few lines of screen output. Switch to a root prompt (sudo -s), then you can then run Vincenzo's two final instructions:
    DESTDIR=/opt make install
    export PATH=/opt/usr/local/bin/:$PATH
You will notice that this is not quite the same as Vincenzo's instructions. If you append /opt... to the path, then run gdc, you will get your currently installed version. To test the new version it needs to be stuck on the front of the path. You can then test the new gdc and gcc as Vincenzo suggests:
goshawk@jupiter:/$ gdc -v
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../configure --enable-languages=d --disable-multilib --disable-shared
Thread model: posix
gcc version 4.3.4 (GCC)
goshawk@jupiter:/$ gcc -v
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../configure --enable-languages=d --disable-multilib --disable-shared
Thread model: posix
gcc version 4.3.4 (GCC)