Writing in D for Raspberry Pi
- Tutorial

Dlang, or simply D, is a young programming language with a long history. Despite the fact that a language with this name appeared a very long time ago, what is now called D2 or just D has appeared recently and weakly resembles its predecessor. Writing in D is very convenient, and the performance is not inferior to C ++, so it is not surprising that he got to ARM and its mobile representatives of Android and iOS. In addition, there is growing interest in the Internet of things and just portable devices.
The article discusses the task of cross-compiling dlang code for Raspberry Pi. This is nothing complicated, and there are no pitfalls. This publication is a simple manual to start using D on different devices in general and Raspberry Pi in particular.
Hello world
We will need:
- Linux computer (in my case, Ubuntu 14.04 on a virtual machine)
- Raspberry Pi with ssh access (used by Raspberry Pi B +)
Despite the fact that you can collect the source directly on the Raspberry Pi, it is better to do this on a separate, more powerful computer. Raspberry itself has too little memory, and cross-compilation for Raspberry Pi is not a big deal. First, we need the dlang compiler for the Raspberry Pi. There are two options: LDC (LLVM based D Compiler) and GDC (GNU D Compiler). It was easier to find a suitable version of GDC. There is LDC for ARM and it can also be used, it just so happens that I use GDC.
We go to the GDC website in the downloads section .
Download the x86_64 assembly for the target platform arm-linux-gnueabihf (more attentive here, the GDC itself will run on x86, and the target is armhf; not to be confused with the GDC for arm, which runs on the device).
wget http://gdcproject.org/downloads/binaries/5.2.0/x86_64-linux-gnu/gdc-5.2.0-arm-linux-gnueabihf+2.066.1.tar.xz
Unpack
tar -xf gdc-5.2.0-arm-linux-gnueabihf+2.066.1.tar.xz
for convenience, I also renamed the folder
mv arm-unknown-linux-gnueabihf gdc-arm
For constant use, I copied the folder with gdc to / usr / bin and made an alias gdc-arm, but for an example to the article we will do everything locally in the folder. And so create a shortcut link:
ln -s gdc-arm/bin/arm-linux-gnueabihf-gdc gdc
Generally speaking, this is already enough to collect D code under Raspberry PI. Let's check on the example of Hello, world. So we write the source:
import std.stdio;
void main() {
writeln("Hello, World!");
}
And save it in hello.d. We compile:
./gdc hello.d -o hello
Now copy to the device, go to it and check. It looked like this for me:
scp hello [email protected]/~/dlang/hello
And on the device:
pi@raspberrypi:~/dlang $ ./hello
Hello, World!
Making the task harder - linking libraries
Everything described above is trivial and works out of the box. Now let's collect something more complicated. For example, “Hello, vibe.d”, that is, a primitive application on the vibe.d web framework . One difference from the usual HelloWorld is that you need to link libraries. Cross-compiling dlang in this issue is no different from C, C ++ and other compiled languages. Therefore, you can use any convenient approach to cross-compilation on the Raspberry Pi.
Libraries are needed for a specific architecture and their best source is the repository installed on the Raspberry distribution. This is usually https://www.raspbian.org/RaspbianRepository. The easiest way to use libraries is directly from the device. Just do not collect on it (a very bad idea, very slowly and forever in a swap), but just use the files. A good idea would be to use sshfs (peeped here: wiki.dlang.org/GDC/Cross_Compiler/Existing_Sysroot ). The main advantage of this approach is absolute versatility and stability. No matter which distribution kit is installed, ideally suitable libraries will be taken. There are no conflicts or discrepancies between distribution versions or libraries.
Create a folder in which we mount the entire Raspberry Pi file system and write it to the environment variable:
mkdir rpi
echo $RPIROOT
export RPIROOT=~/test_pi/rpi/
echo $RPIROOT
/home/user/test_pi/rpi/
Mount via sshfs:
sshfs -o idmap=user,follow_symlinks [email protected]:/ $RPIROOT
Since we need to specify the architecture and search paths for lib, we will create another shortcut to shorten the record. Create a gdc-rpi file and write a script to launch GDC with the necessary flags.
#!/bin/bash
~/test_pi/gdc -march=armv6j -mfpu=vfp -mfloat-abi=hard --sysroot=$RPIROOT -B$RPIROOT/usr/lib/arm-linux-gnueabihf "$@"
Here a little more detail: -march = armv6j -mfpu = vfp -mfloat-abi = hard - these are the flags of the architecture of the Raspberry Pi processor. sysroot - the root of the device, -B - the place to search for lib, we have it on the same device in usr / lib We
add the launch rights and check:
user@ubuntu:~/test_pi$ chmod 777 gdc-rpi
user@ubuntu:~/test_pi$ ./gdc-rpi
gdc: fatal error: no input files
compilation terminated.
Everything works, now we need a DUB on the build machine (this is a build system and for one dependency manager). Download and set it in any convenient way described here .
Create a simple vibe.d project:
dub init -tvibe.d test_vibe_pi
DUB will create a folder with a minimal project inside. Now we collect:
cd test_vibe_pi/
dub build --compiler=../gdc-rpi
At the output, we have the file test_vibe_pi. If there isn’t something like libcurl, then we go to the device and apt-get everything. I already had everything after past experiments.
Copy it to the device and check:
cp test_vibe_pi $RPIROOT/home/pi/dlang
On the Raspberry Pi:
pi@raspberrypi:~/dlang $ ./test_vibe_pi
Listening for requests on http://[::1]:8080/
Listening for requests on http://127.0.0.1:8080/
Please open http://127.0.0.1:8080/ in your browser.
Everything, the minimum site giving "Hello, World!" To any request at 127.0.0.1 : 8080 / is ready.
For those who want to collect their projects directly on the device
Collecting code on such a weak device is a bad idea, but sometimes it’s more convenient to roll out the project to the device in the form of source codes. GDC for Raspberry is ready, you can go to the ARM section of gdcproject.org/downloads and download the armhf version. For assembly, most likely you will need a DUB and now you have to assemble it, because the finished binar from the site does not start on the Raspberry Pi B +.
Download the source from the site https://code.dlang.org/download (you can from the github, that's how you want it). Unpack, put in a folder with a convenient name
user@ubuntu:~/test_pi$ wget https://github.com/rejectedsoftware/dub/archive/v0.9.24.tar.gz
user@ubuntu:~/test_pi$ tar -xf v0.9.24.tar.gz
user@ubuntu:~/test_pi$ mv dub-0.9.24/ dub
user@ubuntu:~/test_pi$ cd dub
There is a separate script for building under Linux using GDC: build-gdc.sh. He expects gdc to be on the system, or the GDC environment variable is set. We use a variable. Just specify the path to our shortcut script and run:
GDC=../gdc-rpi ./build-gdc.sh
Generating version file...
./build-gdc.sh: 15: ./build-gdc.sh: git: not found
Running ../gdc-rpi...
DUB has been built as bin/dub.
You may want to run
sudo ln -s /home/user/test_pi/dub/bin/dub /usr/local/bin
now.
If this conclusion is received and no link errors, then now there is a working DUB for the Raspberry Pi. We copy to the device and check.
user@ubuntu:~/test_pi/dub$ cd ..
user@ubuntu:~/test_pi$ cp dub/bin/dub rpi/home/pi/dlang/dub-test/dub
On the device:
pi@raspberrypi:~/dlang/dub-test $ ./dub
Neither a package description file, nor source/app.d was found in
/home/pi/dlang/dub-test
Please run DUB from the root directory of an existing package, or run
"dub init --help" to get information on creating a new package.
Failed to find a package named ''.
DUB started and rightly noticed the lack of a project in the folder. We really do not have a project, and the main goal has been achieved - DUB runs on the Raspberry Pi.
That's all, we can collect any projects on D under Rapberry Pi. For example, you can start a server for a smart home. There is support for MQTT in the form of a plugin for vibe.d, as well as the ability to use any existing C library.