Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
D Cookbook

You're reading from   D Cookbook Discover the advantages of programming in D with over 100 incredibly effective recipes with this book and ebook.

Arrow left icon
Product type Paperback
Published in May 2014
Publisher
ISBN-13 9781783287215
Length 362 pages
Edition Edition
Arrow right icon
Author (1):
Arrow left icon
Adam Ruppe Adam Ruppe
Author Profile Icon Adam Ruppe
Adam Ruppe
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

D Cookbook
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Core Tasks FREE CHAPTER Phobos – The Standard Library Ranges Integration Resource Management Wrapped Types Correctness Checking Reflection Code Generation Multitasking D for Kernel Coding Web and GUI Programming Addendum Index

Compiling D for ARM/Linux Raspberry Pi


Raspberry Pi is a small, low-cost computer that uses an ARM processor and the Linux operating system.

Getting ready

If you don't own a Raspberry Pi, you can emulate the hardware with QEMU. The process is described at http://xecdesign.com/qemu-emulating-raspberry-pi-the-easy-way/.

How to do it…

To compile D for an ARM/Linux Raspberry Pi, we need to execute the following steps:

  1. Download the GNU D compiler (GDC) cross-compiler from http://gdcproject.org/downloads/. The header gives the platform you are on. The table lists the targets. The target for Raspberry Pi is arm-linux-gnueabi.

  2. Compile the program. The command-line arguments of GDC that are based on the gcc compiler differ significantly from dmd in the following manner:

    • The output file will always be called a.out unless you specify a new name with the –o option.

    • You must always include the .d file extension when passing source files to the compiler.

    • Many of D's additional compile options need to be passed with –f and may have different names. For example, -fno-bounds-check works in the same way as –boundscheck=off on dmd. Refer to the GDC documentation for more info, or download the gdmd helper script that converts the flag syntax for you.

  3. Copy the executable to your Raspberry Pi.

  4. Run the program.

    Tip

    You can also download a native compiler for the Pi which runs directly on it and produces programs for it. The bottom of the GDC download page has a native compiler for ARM.

The code is as follows:

import std.stdio;
void main() { writeln("Hello, ARM!"); }
gdc hello.d –ohello
./gdc

The output is as follows:

Hello, ARM!

How it works…

GDC combines the D language portions of dmd (the dmd frontend) with the code generation portions of gcc (the gcc backend) to create a portable compiler with mature code generation. With GDC, D code can be generated for every target gcc supports.

Code generation is only half the story to using D. Using it on a new platform also requires that the runtime library be ported. The GDC team successfully ported the runtime to Linux on ARM with all tests passing in 2013 and 2014, enabling the full language and standard library on that platform. Most D code and libraries will now work on ARM/Linux targets such as Raspberry Pi.

There's more…

Android and iOS systems often have ARM processors too, but they do not run a standard Linux operating system. In the case of Android, it also does not have linker support for thread-local variables which D will have to emulate. Work is in progress towards porting the library to these operating systems and adding support to the language to enable direct access to Objective-C APIs to facilitate better integration on Apple systems.

See also

  • http://wiki.dlang.org/LDC is the LDC compiler, a D compiler based on the LLVM compiler backend. It also has growing support for ARM targets.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image