Building ghdl from source for Ubuntu 16.04 – mcode version

Based on input from Patrick Lehmann, I decided to re-install ghdl, now using the official GitHub site.

I learned from the README file how to build the mcode variant. I tried, and it worked fine (and it was much simpler than the gcc-based approach I tried earlier)!

Here are the commands that I used.

I downloaded and installed an Ada compiler, as instructed, and I proceeded with the build and install of ghdl, by first setting the PATH to include my newly installed Ada compiler, as

export PATH=/usr/gnat/bin:$PATH

I cloned the repo, using

git clone

I configured and built ghdl, using

cd ghdl
./configure --prefix=/usr/local/ghdl_mcode

The installation was then done, as

ghdl:-$ sudo -i
# cd 
# PATH=/usr/gnat/bin/:$PATH make install
# exit

That was all!

I could now build and run my hello world example, from my work-in-progress book about building a computer, as

vhdl:-$ export PATH=/usr/local/ghdl_mcode/bin/:$PATH
vhdl:-$ ghdl -a hello.vhdl
vhdl:-$ ghdl -e hello_world
vhdl:-$ ghdl -r hello_world
Hello, world

Installing ghdl from source on Ubuntu 16.04

NOTE – this page is outdated – please use updated page instead.

I wanted to reinstall ghdl on my new Ubuntu 16.04. My first idea was to use the method I used for the previous Ubuntu version, where I downloaded a ghdl package from this page with Debian packages.

But when I looked at the page now, I did not find any package that I could get to work on Ubuntu 16.04.

I decided to try an installation from source.

I found out that the source code could be downloaded from this ghdl-updates sourceforge page.

From the information about branches, I could see the version numbers for stable versions. I chose to download the 0.33 version.

The source code download was done using git clone, as

git clone git:// ghdl-updates-ghdl-updates.git

and the 0.33 branch was checked out as

cd ghdl-updates-ghdl-updates.git
git branch
git checkout ghdl-0.33

I looked in the README file and learned that it was possible to build ghdl with gcc as backend. I decided to try this alternative. Another alternative was called mcode, but it was only available for 32-bit Ubuntu, and a third alternative was to use llvm.

The first step was to obtain an Ada compiler.

I installed gnat by doing

sudo apt install gnat

From the README file I then learned that I should download the gcc 4.9 source. I did this by doing

tar xvjf gcc-4.9.4.tar.bz2

I then configured gcc, as instructed in the ghdl source code README file, via the ghdl source configure script, using the path to my downloaded gcc source as argument, by doing

cd ghdl-updates-ghdl-updates.git/
./configure --with-gcc=/home/ola/ghdl/gcc/gcc-4.9.4/
make copy-sources
cd ..

It was now required to download and build some auxiliary packages. I did this, for the package gmp, by doing

tar xvjf gmp-4.3.2.tar.bz2
mkdir gmp-4.3.2/gmp-objs/
cd gmp-4.3.2/gmp-objs/
../configure --prefix=/usr/local --disable-shared
sudo make install
cd ../..

followed by the package mpfr, as

tar xvjf mpfr-2.4.2.tar.bz2
mkdir mpfr-2.4.2/mpfr-objs/
cd mpfr-2.4.2/mpfr-objs/
../configure --prefix=/usr/local --disable-shared --with-gmp=/usr/local
sudo make install
cd ../..

and finally, the third package mpc, as

tar xvzf mpc-0.8.1.tar.gz
mkdir mpc-0.8.1/mpc-objs/
cd mpc-0.8.1/mpc-objs/
../configure --prefix=/usr/local --disable-shared --with-gmp=/usr/local
sudo make install
cd ../..

It was now time to build the actual ghdl. Since we had decided to use gcc as backend, we accomplish the task of building ghdl by building gcc. So we go back to our directory where we unpacked the gcc sources.

In that directory, we do configuration as

cd gcc-4.9.4
mkdir gcc-objs
cd gcc-objs/
../configure --prefix=/opt/ghdl-updates --enable-languages=c,vhdl --disable-bootstrap --with-gmp=/usr/local --disable-lto --disable-multilib

In the above configuration step, we have selected the prefix as /opt/ghdl-updates. The reason for this is to avoid a collision with an already existing gcc compiler (which we already have on our system – and which we need for the build of ghdl).

For the actual build step, we need to do make. Here, I ran into a problem since the program gnat1 was assumed to be used (instead of gnat). The error was seen when doing a plain


and it showed up, after a while, as

gnatbind -Lgrt_ -o run-bind.adb -n ghdl_main.ali
gcc -c -O2 -g -gnatec../../../gcc/vhdl/grt/grt.adc -gnat05 -o run-bind.o run-bind.adb
gcc: error trying to exec 'gnat1': execvp: No such file or directory
Makefile:586: recipe for target 'run-bind.o' failed

I found this searchcode page, where the problem, and a solution, were described.

Using this newfound piece of information, I located gnat1 by doing

$ find /usr -name gnat1

I then used this directory as a part of the PATH when doing make, as

PATH=/usr/lib/gcc/x86_64-linux-gnu/4.9:$PATH make

This resulted in a successful build.

Assuming that the PATH used for make was needed also when doing make install, I started a root shell as

sudo -i

and in that shell, after having done cd to the gcc-objs directory, I did

PATH=/usr/lib/gcc/x86_64-linux-gnu/4.9:$PATH make install MAKEINFO=true

This completed the installation of ghdl!

I could use ghdl, on the first example from the book Into Computers, after having set the PATH as

export PATH=/opt/ghdl-updates/bin/:$PATH

as described in the book, by doing

$ ghdl -a hello.vhdl
$ ghdl -e hello_world
$ ghdl -r hello_world
Hello, world

A bare-metal x86-cross-compiler on Mountain Lion

I wanted to create a bare-metal program for an Intel-x86 processor. The program is used in a chapter called The Bare Metal in the book Into Embedded.

On an Ubuntu Linux host I could use gcc, together with a linker script, to accomplish the task. I could then run the program using Bochs.

On a Mac Mountain Lion host, the above method did not work out of the box, due to ld not being the GNU linker.

In addition, gcc was not GNU gcc either.

I searched the net and found this interesting page by M3 Operating System Development where it was described how to build an i386 cross-compiler. I thought that this could be a way to create a compiler for bare-metal programs with no OS-dependencies – which was desirable since the bare-metal program was to be evolved into an OS by itself.

Problem arose, however, since the pre-installed gcc on my Mac could not build the i386-cross-gcc.

I searched again, and found this eminent page by Solarian Programmer where it is described how to build a GNU gcc compiler.

I downloaded the gcc 4.7.2 sources from the GNU ftp repository.

Then, following the instructions, however leaving out the choice of building a compiler for Fortran and also changing the gcc version from 4.7.1 to gcc 4.7.2, I was able to build a native GNU gcc compiler.

The newly built native gcc could then be set to the default gcc by issuing the command

export PATH=/usr/gcc-4.7.2/bin:$PATH

Then, returning to the instructions for building an i386-cross-gcc, I started with downloading


from the GNU binutils repository.

I created a destination directory for the cross compiler, by doing

sudo mkdir /usr/local/i386elfgcc/

The binutils could then be unpacked, configured, and built, using the commands

tar zxvf binutils-2.23.tar.gz
mkdir build-binutils
cd build-binutils/
../binutils-2.23/configure --target=i386-elf --prefix=/usr/local/i386elfgcc
sudo make install

I then built gcc, using inspiration also from this OS development wiki, by issuing the commands

tar xvjf gcc-4.7.2.tar.bz2
mkdir build-gcc
cd build-gcc
../gcc-4.7.2/configure --target=i386-elf --prefix=/usr/local/i386elfgcc --with-gnu-as --with-gnu-ld --disable-libssp --enable-languages=c --without-headers
make all-gcc
sudo make install-gcc

After having added /usr/local/i386elfgcc/bin to the PATH environment variable I was able to compile and link a bare metal program.

The program consists of three C-files, which I could compile using the commands

i386-elf-gcc -Wall -c screen_output.c
i386-elf-gcc -c -Wall -DBUILD_X86_FD_TARGET src/bare_metal.c -o obj/bare_metal_x86_fd_target.o
i386-elf-gcc -c -Wall -DBUILD_X86_FD_TARGET src/console.c -o obj/console_x86_fd_target.o

The program could then be linked, using a command where also startup code, written in assembly and assembled using NASM resulting in the object file start_code.o, as

i386-elf-ld -T arch/x86_fd_target/link.ld --oformat=elf32-i386 -melf_i386 arch/x86_fd_target/start_code.o arch/x86_fd_target/screen_output.o -o prog_x86_fd_target.elf obj/console_x86_fd_target.o obj/bare_metal_x86_fd_target.o

Finally, the file prog_x86_fd_target.elf needs to be converted from ELF format to raw binary format. This can be done using the command

i386-elf-objcopy -O binary prog_x86_fd_target.elf prog_x86_fd_target.bin

As a last step, a binary bootable image for a floppy-disc drive can be created, by concatenating a FAT12 boot sector, a defined number of empty FAT12-sectors, and the binary file created by the i386-elf-objcopy command.

The concatenation is done as

cat arch/x86_fd_target/boot.bin arch/x86_fd_target/b_32_512.bin prog_x86_fd_target.bin > arch/x86_fd_target/a.img

The program can now be run, using Bochs, by giving the command

bochs -f arch/x86_fd_target/bochsrc.txt -q

Installing an ARM cross-compiler on Mac Mountain Lion

Here I will describe my installation of an ARM cross compiler, on a MacBook Air with Mac OS Mountain Lion.

After some searching on the net I decided to try the YAGARTO toolchain.

Navigating to the download place for Mac, I downloaded the file


Using this file however led to an error, saying

selected processor does not support requested special purpose register -- `mrs r0,cpsr'

I reverted to an older version, found at YAGARTO’s Sourceforge page. This led to download of the file


Double-clicking on this downloaded file, I was able to install the toolchain.

I then added the following changes

export ARM_GCC_LOCATION=/Users/oladahl/yagarto/yagarto-4.6.2/bin

to my setup script, where I also set up other environment variables.

Now I can start programming for ARM!

As an example, I can compile, link, and run the example described in Chapter The Bare Metal in the book Into Embedded.

Installing an ARM cross compiler on Ubuntu

Here I will describe my installation of an ARM cross compiler, on an x86 machine with Ubuntu Linux.

Updates to this post

  • July 12, 2013 – changed to a later version of the Sourcery ARM compiler – now using version 2013.05-23
  • March 1, 2013 – changed to a later version of the Sourcery ARM compiler – now using version 2012.09-63

I had decided to use the Sourcery ARM compiler, formerly from CodeSourcery and now from Mentor.

I go to the page

at which I decide to use the EABI release for ARM processors.

Then, I continue to the installation page for the ARM EABI version.

After having created an account (this was my first time here) and then logging in, I get an e-mail with a download link from which I can proceed to a page where I can download the IA32 GNU/Linux Installer. Doing this results in download of a file named arm-2013.05-23-arm-none-eabi.bin.

I make the file executable by doing

chmod +x arm-2013.05-23-arm-none-eabi.bin

and then I run the file, using the command


This command results in an error message, and I am instructed to issue the command

sudo dpkg-reconfigure -plow dash

and then answer the question that comes up as instructed by the error message.

Now, again running the command


results in the installation being started. After having gone through steps involving reading and accepting license agreements, followed by a decision to not create any symbolic links, and to not send anonymous information about usage of the ARM compiler, the installation is complete.

As a last step, I modify the PATH environment variable using the commands (where /home/ola is my home directory)

export ARM_GCC_LOCATION=/home/ola/CodeSourcery/Sourcery_CodeBench_Lite_for_ARM_EABI/bin

I put the above two lines in a setup file, called, that I run every time I want to use the ARM toolchain.

Now I can start programming for ARM!

As an example, I can compile, link, and run the example described in Chapter The Bare Metal in the book Into Embedded.