image not found!!!!!
Currency: GBP | 0 items in basket

Introduction to the GNU toolchain

TUTORIAL0001 - Revision

Overview

This is a basic introduction the GNU toolchain. We will look at the parts that make up the toolchain and learn about their functions. This tutorial is an excellent starting point for anybody looking at starting to program in C/C++ or moving from a different development system.

What is a toolchain?

A toolchain is a set of programming tools that are used to build an executable program from source code (human readable text). The process of building an executable has a number of steps. It's called a toolchain because the output from one tool is used as the input of another.

What is the GNU toolchain?

The GCC (GNU Compiler Collection) is an open-source and 100% free toolchain. It supports a number of programming languages: C, C++, Objective-C, Fortran, Java, Ada and Go. It is a very high quality and regularly maintained and updated package.

The open-source nature of GCC has seen the compiler ported to a number of different processor architectures, for example x86 PCs and ARM processors. This allows you to use familiar tools and portable code across a number of different processors and platforms.

GCC is a pure toolchain, and does not provide an integrated development environment (IDE). It simply takes files that could be written in any text editor and compiles them into an executable program. At first this can seem daunting to beginners and users accustomed to an IDE, but you will see the power and flexibility this gives you. If you wish for an IDE to manage your projects then Eclipse integrates with GCC very well and provides an excellent platform for development.

Parts of the toolchain

  1. The GCC (GNU complier collection) is used to convert human readable source code into instructions that a computer can process called machine code.
  2. The linker is used to link together the bits of machine code outputted from the compiler and parts from pre-compiled libraries to create an executable program. The linker is part of the GNU binutils; a suit of binary utility tools.
  3. Make is a program for automating, compiling and building applications. You can learn more about make here.
  4. GDB is a debugger that can be used to view and interact with running programmes to help you find bugs by viewing the program flow and examining and modifying program variables.

Installing GCC

GCC is included with all Unix/Linux installations and is ready and waiting to be used.

For Windows users you have two options, either MinGW or Cygwin. Both options have benefits: MinGW is simple, light-weight and provides just what is required for producing applications. Cygwin provides a greater depth of tools and functionality.

MinGW

MinGW (Minimalist GNU for Windows) is a port of GCC and Binutils to Windows. This is a collection of executables that are installed on your computer and can be run from the command line.

Follow the following steps to install MinGW:

  1. Visit http://www.mingw.org/wiki/Getting_Started and download the installer from the link “mingw-get-setup.exe” part way down the page. This page provides some useful information regarding MinGW and should be read if you have time.
  2. Run the downloaded installation program. Make sure you choose an installation directory without spaces, for example “C:\mingw”.
  3. The mingw-get program manages your installation. When it starts up you will be shown a list of available packages. Select mingw32-base to install the C packages. If you are interested in c++, select the mingw32-gcc-c++ options.
  4. Once you have selected your packages, click the "Installation" menu in the upper left and select "Apply Changes". This will install the required files.
  5. You will need to setup the system PATH to pick up the bin directory of your MinGW installation. This simply means that Windows will know to look in your installation directory for applications that are called. Google for how to add directories to the system PATH if you are unaware how to do this.
  6. Once you have added the installation directory to the PATH, verify the installation by checking the GCC version as follows:
    gcc --version

    If you receive a message that “gcc is not recognised as an internal or external command” you may need to restart your computer or double check your PATH variable.

Cygwin

Cygwin is a Unix-like environment with a command-line / terminal interface for Windows. Cygwin is a comprehensive collection of tools and libraries that with the included Bash shell provides a near Unix/Linux environment in your Windows PC.

Follow the following steps to install MinGW:

  1. Download the correct installation program for your version of Windows from www.cygwin.com.
  2. Follow the instructions given by the installer. The default options should be fine. This can take some time to download and install.
  3. Once the installation has completed, run the Cygwin program and it will open a terminal application that you can build your programs from.

Hello World example

OK, you have the toolchain installed and ready to use, so let's make a simple test application; the classic “Hello World” program.

Fire up a text editor (if you're using Windows then notepad will do). Create a new file called “hello.c” in an empty directory and input the following:

// hello.c
#include <stdio.h>
int main()
{
	printf(“Hello World!”);
	return(0);
}		

To compile your program, open a terminal / command prompt and navigate to the directory with your “hello.c” file and enter the following:

> gcc hello.c

The default output name is “a.exe” under Windows. On some versions of Linux the output is “a.out” but look in the directory to see what has been generated by default.

Let's see your application in action:

Linux / Cygwin

$ ./a.out 

Note that Linux requires that you provide a full path to the application that you wish to run.

MinGW

> a

You should see "Hello World!" outputted to the command line.

If you have any problems double check your “hello.c” source code file for any typo's.

We have our first program but the output name is not very useful. Try recompiling your program with the following command:

> gcc -o hello.exe hello.c

This should give you a “hello.exe” program.

Summing up

We have gone through a very quick introduction to the GCC toolchain. We looked at it's components, how to install it and even took it for a test drive.

When building more complex programs you will want to look at using GNU Make to help automate the build process. We will have a tutorial available soon that will give an overview of Make.

There is a lot more to learn whether you're new to programming or just new to the GCC toolchain. Check out the following tutorial to learn more:


Feedback

If you have any feedback, would like more information or would like to see a tutorial that we do not currently have then please contact us at info@blackramelectronics.com.

Or if you prefer you can contact us directly using the form below.

An e-mail address is required by this form, although if you do not wish for a reply then feel free to input noreply@yoursite.com.