• Building GCC From Scratch

    by  • December 27, 2011 • AppDev • 5 Comments

    Preparing Linux Development Environment

    This tutorial uses Linux to build and use GCC and the distro flavour used is the Ubuntu distro. This tutorial uses the Xubuntu distro but you can use different kind of Ubuntu distro. If you are not experienced with Linux than it is recommended that you use the same version and distro with this tutorial to save time and pain down the track in troubleshooting problems. The screenshot shows the Linux Kernel and Xubuntu version I’m using for this tutorial, to make it easier I’m outlining the Linux version information here

    Linux ubuntu 3.0.0-12-generic #20-Ubuntu SMP … x86_64 GNU/Linux

    Toolchain Script Preparation

    There are a number of ways you can build GCC from the ground up but most of the scripts that I came across are either out of date, buggy or worse not maintained and out of date. I found the scripts created by Uwe Hermann is the most easiest to use and it is also maintained up to date in github by Piotr Esden-Tempski. Use the latest source from github by checking it out as follows (if you don’t have git installed on your machine follows the instructions in the troubleshooting section)

    git clone https://github.com/esden/summon-arm-toolchain.git

    The next step, which is the most crucial step in the whole process is to install all the necessary libraries that are required by GCC. Execute the following command to install the libraries

    sudo apt-get install flex bison libgmp3-dev libmpfr-dev libncurses5-dev libmpc-dev autoconf texinfo build-essential libftdi-dev zlib1g-dev

    Press Y when it asked Do you want to continue. It will take sometime but you will see few lines of messages coming out to inform you about the installation progress. You will get something similar as below

    Get:1 http://us.archive.ubuntu.com/ubuntu/ oneiric/main autoconf all 2.68-1ubuntu1 [560 kB]
    Get:2 http://us.archive.ubuntu.com/ubuntu/ oneiric/main autotools-dev all 20110511.1 [44.7 kB]
    Get:3 http://us.archive.ubuntu.com/ubuntu/ oneiric/main automake all 1:1.11.1-1ubuntu1 [544 kB]
    Get:4 http://us.archive.ubuntu.com/ubuntu/ oneiric/main libstdc++6-4.6-dev amd64 4.6.1-9ubuntu3 [1,644 kB]
    Get:5 http://us.archive.ubuntu.com/ubuntu/ oneiric/main g++-4.6 amd64 4.6.1-9ubuntu3 [6,970 kB]
    Get:6 http://us.archive.ubuntu.com/ubuntu/ oneiric/main g++ amd64 4:4.6.1-2ubuntu5 [1,444 B]
    Get:7 http://us.archive.ubuntu.com/ubuntu/ oneiric/main libdpkg-perl all 1.16.0.3ubuntu5 [171 kB]
    Get:8 http://us.archive.ubuntu.com/ubuntu/ oneiric/main dpkg-dev all 1.16.0.3ubuntu5 [473 kB]
    Get:9 http://us.archive.ubuntu.com/ubuntu/ oneiric/main build-essential amd64 11.5ubuntu1 [5,928 B]
    Get:10 http://us.archive.ubuntu.com/ubuntu/ oneiric/main fakeroot amd64 1.17-1 [107 kB]
    Get:11 http://us.archive.ubuntu.com/ubuntu/ oneiric/main libalgorithm-diff-perl all 1.19.02-2 [50.7 kB]
    Get:12 http://us.archive.ubuntu.com/ubuntu/ oneiric/main libalgorithm-diff-xs-perl amd64 0.04-1build1 [13.4 kB]
    Get:13 http://us.archive.ubuntu.com/ubuntu/ oneiric/main libalgorithm-merge-perl all 0.08-2 [12.7 kB]
    Get:14 http://us.archive.ubuntu.com/ubuntu/ oneiric/main libgmpxx4ldbl amd64 2:5.0.1+dfsg-7ubuntu2 [10.7 kB]
    Get:15 http://us.archive.ubuntu.com/ubuntu/ oneiric/main libgmp-dev amd64 2:5.0.1+dfsg-7ubuntu2 [367 kB]
    Get:16 http://us.archive.ubuntu.com/ubuntu/ oneiric/main libgmp3-dev amd64 2:5.0.1+dfsg-7ubuntu2 [3,814 B]
    Get:17 http://us.archive.ubuntu.com/ubuntu/ oneiric/main libmpfr-dev amd64 3.0.1-5 [234 kB]
    Get:18 http://us.archive.ubuntu.com/ubuntu/ oneiric/main libtinfo-dev amd64 5.9-1ubuntu5 [71.2 kB]
    Get:19 http://us.archive.ubuntu.com/ubuntu/ oneiric/main libncurses5-dev amd64 5.9-1ubuntu5 [252 kB]
    Get:20 http://us.archive.ubuntu.com/ubuntu/ oneiric/main texinfo amd64 4.13a.dfsg.1-8ubuntu1 [458 kB]
    Get:21 http://us.archive.ubuntu.com/ubuntu/ oneiric/main libmpc-dev amd64 0.9-3 [53.1 kB]
    Fetched 12.0 MB in 1min 17s (156 kB/s)
    Selecting previously deselected package autoconf.
    (Reading database … 148533 files and directories currently installed.)
    Unpacking autoconf (from …/autoconf_2.68-1ubuntu1_all.deb) …
    Selecting previously deselected package autotools-dev.
    Unpacking autotools-dev (from …/autotools-dev_20110511.1_all.deb) …
    Selecting previously deselected package automake.
    Unpacking automake (from …/automake_1%3a1.11.1-1ubuntu1_all.deb) …
    Selecting previously deselected package libstdc++6-4.6-dev.
    Unpacking libstdc++6-4.6-dev (from …/libstdc++6-4.6-dev_4.6.1-9ubuntu3_amd64.deb) …
    Selecting previously deselected package g++-4.6.
    Unpacking g++-4.6 (from …/g++-4.6_4.6.1-9ubuntu3_amd64.deb) …
    Selecting previously deselected package g++.
    Unpacking g++ (from …/g++_4%3a4.6.1-2ubuntu5_amd64.deb) …
    Selecting previously deselected package libdpkg-perl.
    Unpacking libdpkg-perl (from …/libdpkg-perl_1.16.0.3ubuntu5_all.deb) …
    Selecting previously deselected package dpkg-dev.
    Unpacking dpkg-dev (from …/dpkg-dev_1.16.0.3ubuntu5_all.deb) …
    Selecting previously deselected package build-essential.
    Unpacking build-essential (from …/build-essential_11.5ubuntu1_amd64.deb) …
    Selecting previously deselected package fakeroot.
    Unpacking fakeroot (from …/fakeroot_1.17-1_amd64.deb) …
    Selecting previously deselected package libalgorithm-diff-perl.
    Unpacking libalgorithm-diff-perl (from …/libalgorithm-diff-perl_1.19.02-2_all.deb) …
    Selecting previously deselected package libalgorithm-diff-xs-perl.
    Unpacking libalgorithm-diff-xs-perl (from …/libalgorithm-diff-xs-perl_0.04-1build1_amd64.deb) …
    Selecting previously deselected package libalgorithm-merge-perl.
    Unpacking libalgorithm-merge-perl (from …/libalgorithm-merge-perl_0.08-2_all.deb) …
    Selecting previously deselected package libgmpxx4ldbl.
    Unpacking libgmpxx4ldbl (from …/libgmpxx4ldbl_2%3a5.0.1+dfsg-7ubuntu2_amd64.deb) …
    Selecting previously deselected package libgmp-dev.
    Unpacking libgmp-dev (from …/libgmp-dev_2%3a5.0.1+dfsg-7ubuntu2_amd64.deb) …
    Selecting previously deselected package libgmp3-dev.
    Unpacking libgmp3-dev (from …/libgmp3-dev_2%3a5.0.1+dfsg-7ubuntu2_amd64.deb) …
    Selecting previously deselected package libmpfr-dev.
    Unpacking libmpfr-dev (from …/libmpfr-dev_3.0.1-5_amd64.deb) …
    Selecting previously deselected package libtinfo-dev.
    Unpacking libtinfo-dev (from …/libtinfo-dev_5.9-1ubuntu5_amd64.deb) …
    Selecting previously deselected package libncurses5-dev.
    Unpacking libncurses5-dev (from …/libncurses5-dev_5.9-1ubuntu5_amd64.deb) …
    Selecting previously deselected package texinfo.
    Unpacking texinfo (from …/texinfo_4.13a.dfsg.1-8ubuntu1_amd64.deb) …
    Selecting previously deselected package libmpc-dev.
    Unpacking libmpc-dev (from …/libmpc-dev_0.9-3_amd64.deb) …
    Processing triggers for man-db …
    Processing triggers for doc-base …
    Processing 1 added doc-base file…
    Registering documents with scrollkeeper…
    Processing triggers for install-info …
    Setting up autoconf (2.68-1ubuntu1) …
    Setting up autotools-dev (20110511.1) …
    Setting up automake (1:1.11.1-1ubuntu1) …
    update-alternatives: using /usr/bin/automake-1.11 to provide /usr/bin/automake (automake) in auto mode.
    Setting up libdpkg-perl (1.16.0.3ubuntu5) …
    Setting up dpkg-dev (1.16.0.3ubuntu5) …
    Setting up fakeroot (1.17-1) …
    update-alternatives: using /usr/bin/fakeroot-sysv to provide /usr/bin/fakeroot (fakeroot) in auto mode.
    Setting up libalgorithm-diff-perl (1.19.02-2) …
    Setting up libalgorithm-diff-xs-perl (0.04-1build1) …
    Setting up libalgorithm-merge-perl (0.08-2) …
    Setting up libgmpxx4ldbl (2:5.0.1+dfsg-7ubuntu2) …
    Setting up libgmp-dev (2:5.0.1+dfsg-7ubuntu2) …
    Setting up libgmp3-dev (2:5.0.1+dfsg-7ubuntu2) …
    Setting up libmpfr-dev (3.0.1-5) …
    Setting up libtinfo-dev (5.9-1ubuntu5) …
    Setting up libncurses5-dev (5.9-1ubuntu5) …
    Setting up texinfo (4.13a.dfsg.1-8ubuntu1) …
    Setting up libmpc-dev (0.9-3) …
    Setting up libstdc++6-4.6-dev (4.6.1-9ubuntu3) …
    Setting up g++-4.6 (4.6.1-9ubuntu3) …
    Setting up g++ (4:4.6.1-2ubuntu5) …
    update-alternatives: using /usr/bin/g++ to provide /usr/bin/c++ (c++) in auto mode.
    Setting up build-essential (11.5ubuntu1) …
    Processing triggers for libc-bin …
    ldconfig deferred processing now taking place

    Building Toolchain

    This step will guide you on how to build GCC. Change to the directory that contains the checked out scripts and execute the command as follow

    You will see the script will start downloading all the necessary dependencies that are required to build GCC. The first dependency is the binutils

    Next, the script will download GCC. In this case the script is downloading GCC from Linaro website. This step will take a bit longer than the other steps as this is the core GCC source codes that are being downloaded.

    Next, it will download newlib library

    Next, it will download gdb debugger

    Next, it will download openocd

    After completing the download process the script will unpack the source code one by one. The first one will be the binutils and you will see long list of files being extracted out looking something like the following

    On completion of the extraction process the compilation process for binutils will kick in and will look as the following

    Compilation process will take sometime to complete as it involved quite a lot of files. After completing the binutils compilation the script will move to extract the GCC and newlib source codes followed by the compilation process.

    On completion of the GCC and newlib source code the GCC have been compiled and ready to use. The next step after this the script will compile the openocd and gdb source code too.

    It is advisable to let the whole script complete before doing anything. At any given point of time if the script stops because of errors check the Appendix section for troubleshooting tips and fixes, once you have done the fix you can re-run the script again.

    Checking The New GCC

    Once GCC has been compiled successfully you need to test to make sure that it is working, the first and easiest step is to execute it. If you haven’t changed anything from the summon-arm-toolchain script the GCC binaries would have been installed in the /sat directory in your home folder, in my case it is installed in /home/nanik/sat

    In order to execute the newly created GCC you need to add it to your PATH environment variable as follows

    PATH=$PATH:~/sat/bin

    to print out the help menu of GCC type in (Note: help option is with double dash)

    arm-none-eabi-gcc –help

    you will the output as shown below. The output shown is a printout of the available options in GCC

    the other way to check is to print out the supported platform/architecture that GCC can compile the source code to. Execute the following commmand (Note: target-help option is with double dash)

    arm-none-eabi-gcc –target-help

    if you scroll down the output on your screen you will see something like the following screen

    What you are seeing on the output is the ARM CPUs that are supported by the GCC and also the ARM architectures (instruction sets) that are supported.

    If you have reached this part of the instruction that means you have successfully compiled and run GCC. Now you can use the GCC as part of your build process.

    Troubleshooting

    Installing git

    If you don’t have git installed on your machine execute the following command

    sudo apt-get install git

    You will get somewhat similar kind of messages, and once the download and installtion is done you can use git

    zlib compilation error

    During compilation process if you come across problem such as

    fatal error: zlib.h: No such file or directory

    install the zlib by using the following command

    sudo apt-get install zlib1g-dev

    openocd compilation error

    During openocd compilation process if you come across error

    error: Cannot build & run test program using libftdi

    install the libftdi library by using the following command

    sudo apt-get install libftdi-dev

    Enter Y to continue the installation process, once completed you will something like below

    About

    5 Responses to Building GCC From Scratch

    1. Pingback: Building GCC From Scratch For Cortex-M3

    2. December 27, 2011 at 10:55 pm

      http://en.wikipedia.org/wiki/ARM_Cortex-M#Development_tools

      http://en.wikipedia.org/wiki/STM32#Development_tools

      (by the way, your web page has some type of problem, because the fields don’t have “field names” next to them.

      • admin
        December 28, 2011 at 8:05 am

        Sbmeirow,

        Thanks for the comment, can you kindly let me know which part of the page is showing problem ? as I can’t seem to see any error message ?

    3. December 27, 2011 at 11:04 pm

      #1 I recommend that you list the “version information” above your top photo as actual text, even though it’s listed in the photo (once a person clicks on the photo).

      #2 Please list the exact hardware (and link to its web page) that you are using for target demo.

      http://en.wikipedia.org/wiki/User:Sbmeirow

      • admin
        December 28, 2011 at 8:56 am

        As for #1 I’ve added the Linux version information outside the picture and as #2 the article is meant for GCC to be used with any board that support Cortex-M3 processor regardless of what other accessories the board comes with.

    Leave a Reply

    Your email address will not be published. Required fields are marked *


    × two = 18