Building avr-gcc for C++11 on Mac OS

Yesterday I saw that the support of C++11 has been added in GCC 4.7. Since avr-gcc is just a special configuration of gcc itself, it is then possible to build AVR programs using C++11. Of course, the standard library will not be available, but the language itself has some interesting features.

Since I don’t have the latest avr-gcc on Mac OS, I confirmed the theory by building a test program on the Raspberry Pi (equipped with avr-gcc 4.7.0). Yup, it works.

The latest available version of avr-gcc on Mac OS 10.6 is either 4.5.1 (using CrossPack) or 4.7.0 (using MacPorts). I could use MacPorts, but I don’t like the fact they install everything into /opt/local, it doubles up with the /usr/local directory where I usually install cross-compilers etc. If you want the easy version, go for the MacPorts solution. Otherwise, read on and hold tight..

Building the AVR toolchain from scratch on Mac OS

The complete toolchain is composed of three parts:

  • The compilers (avr-gcc, avr-c++ etc)
  • The binutils (avr-size, avr-objcopy etc)
  • The C library (avr-libc containing all the includes and precompiled libraries

The installation process in described on the avr-libc webpage.


I’m assuming you have the following tools installed on your machine:

  • Xcode development tools (my version is 3.2.6)
  • MacPorts (to fetch the libraries we need to build gcc)

Getting the sources

The gcc sources are hosted on FTP servers, which can be quite slow. I used SVN instead, with the following command:

svn co svn://

The sources for the binutils can be found here, and the sources for the avr-libc can be found here.

Building the binutils

Before building the binutils, you will need to apply a patch to add AVR support to the avr-size tool. Here is the patch released by Atmel, however it failed to build on my machine, and I had to apply another one.

Requirements for the compilers

To build gcc, you will need the following libraries (for floating-point math):

  • gmp
  • mpfr
  • libmpc (be carefull not to install mpc like I did, it’s not the same thing)

That can be done with the following command:

sudo port install gmp mpfr libmpc

You will also need the iconv library, but the MacPorts version did not seem to work, so you’ll have to build it manually, from the sources. You will also need to copy this patch file to the libiconv-1.14 directory (or whatever version you download).

cd libiconv-1.14
mkdir build && cd build
../configure --prefix=/usr/local/temp/iconv
cd ..
patch --input libiconv_patch_gcc_4.7.2.txt
# when prompted for file to patch, enter build/include/iconv.h
cd build
make install

Here I install it into a temporary directory, since I might not need it later.

Building the compilers

To build GCC, we need to tell it where to look for the gmp, mpfr, mpc and iconv libraries. Make sure you don’t have the iconv library installed with MacPorts, as it will override the one you just built in /usr/local/temp/iconv.

cd gcc_4_7_2_release/
mkdir obj-avr && cd obj-avr
../configure --prefix=/usr/local/avr --target=avr --enable-languages=c,c++ --disable-nls --disable-libssp --with-dwarf2 --with-gmp=/opt/local --with-mpfr=/opt/local --with-mpc=/opt/local --with-libiconv-prefix=/usr/local/temp/iconv
make install

Building the C library

If you had an older version of avr-gcc installed, now might be the time to remove it (or at least to remove its path from $PATH), so you can build the avr-libc using your shiny new compiler.

This step is quite straightforward, just follow the instructions and you should be fine.

Building a test program

In order to enable C++11 features, you’ll have to pass the following flag to the compiler:

Now try the following program, demoing the auto variables and range-based for loop:
int main()
    const char foo[] = "Hello, World!";
    char bar[14] = { 0 };
    auto i = 0;

    for (auto baz: foo)
         bar[i++] = baz;
    return 0;

You will notice that we’re returning from main, although it’s considered to be a blasphemy by some embedded developers, what it will do is only continuing executing empty instructions until looping back to the reset point. Anyway, this would not be worth much uploading onto a chip, since there would be no way to check that bar contains “Hello, World!” after execution..

Review of devboards and tools

Boarduino by LadyAda / Adafruit.

My first Arduino-compatible board. It’s the clone of the old Arduino Diecimila (with an ATmega 168P), in a breadboard-friendly format. It’s ideal to start with hardware computing and the breadboard format helps interfacing it with sensors and other circuits.


Sanguino by Zach “Hoeken” Smith

I needed more memory and pins than the usual 168P,  so I started using the 644P in the Sanguino, and made it a basis for most of my projects. It has been my main devboard for quite some time, before I made the Mobius Modular board.


Teensy 2.0 by Paul Stoffregen

Graciously donated by Paul for the purpose of developing the MIDI library on the Teensy, I’ve been using it mainly for this purpose. It has the same chip as the Arduino Leonardo, meaning it has native USB HID support.


Teensy 3.0 by Paul Stoffregen

One of my last acquisitions. ARM M4-powered, this little board can be one of the next big things in the ARM devboard race. I don’t think it might be powerful enough to run an OS “comfortably” like the Rasberry Pi, but a small RTOS should not be a problem.Detailed review here.


Arduino Leonardo

I currently use it as a unit test platform for the MIDI Library. The native USB driver in the ATmega32U4 makes it a good board to experiment with native MIDI USB, to create plug and play MIDI controllers and synths.


Mobius Modular Motherboard

I made this board to simplify the prototyping of MIDI devices and to be the heart and mind of my Mobius project. It’s using an ATmega 644P and features an IO expander for an LCD screen, as well as a hardware switch debouncer. The central connector breaks out access to 10 GPIO pins.


Raspberry Pi

I feel a bit guilty of using it only as a Git/Web home server, but it’s doing the job quite well. It’s running the Wheezy distro without many modifications. I might give a try to the Adafruit Occidentalis distro to play with the GPIO.


Texas Instruments Stellaris LaunchPad

The cheapest ARM board I’ve ever seen ($4.99 including worldwide shipping), but comes with limitations, as it’s only a demo board. Still, for that price, you get two ARM M4 chips.

Review here.



Open Workbench Logic Sniffer by Dangerous Prototypes

Logic Sniffers are useful when debugging digital circuits, but also for firmware debugging: toggle pins when you enter and leave a function to check how much time you spent inside it! It can also analyse most serial transmissions (when setup correctly, took me some time to get it right).

Bus Pirate by Dangerous Prototypes

Very useful little tool to control almost everything that talks a serial language. Scriptable in Python, so with PySide you can make nice interfaces to analyse, control and hack the world. Note: If you plan to use it as an AVR programmer, prefer an USBtiny or something similar, it’s damn too slow.


Raspberry Pi and 16×2 LCD

Got a RasPi and an LCD screen? You can hook it up to display some info (like the IP address here, very handy when using DHCP and carrying your Pi around).
The LCD does not need to be a 3.3V model, mine is a 5V and works like a charm.

Once attached to the GPIO, you can use a python program to send some text to write.

Read more on Adafruit – Drive a 16×2 LCD directly with a Raspberry Pi

The examples show a clock and the IP address but it might be better to display events that only occur from time to time (the clock script runs continuously).

Git on Pi

Git is a great tool for version control. I used SVN in the past but Git is simply more powerful, although a bit harder to understand for the newcomers.

I use GitHub for most of my open-source projects, but sometimes I would like to set up some private Git repositories for non-open source stuff (like private data, files and backups). That’s a job for the Raspberry Pi !

Back in the days of SVN, administrating repositories was a pain in the ***, and it could have been the same with git if it wasn’t for a nifty little tool: gitolite.

Gitolite helps you create repositories, users, manage access, restrictions etc.. And all of this is done.. by using git itself. The configuration file (containing repositories description) is located in an “admin” repository, along with the public keys associated with users allowed to access these repositories.

This way, to add users, you only add their public keys, and edit the config file to choose which repository they’re allowed to access. To create a repository, simply add the name and people granted access to the conf file, and commit/push. It’s that simple.

You can find the instructions on how to setup your own private Git repositories at the Gitolite Readme page, on GitHub.