Getting to grips with Eclipse: cross compiling

Yesterday, when I discussed with my office mate on the issues of compiling the epuck using the linux laptop…he mentioned something brilliant i guess. Previously, I just compiled my code using the linuxboard in epuck. Jenny inform me that she never tested it in compiling the codes using laptop, but she only tested using Hello World program. I have tested with a simple hello world program and its work.But when i tested with the epuck codes it does not work as few library configuration is needed.

Googling and try to find an example that using eclipse to cross-compile and it works…

Here it goes:

Setting the path for the toolchain

Usually you will have installed a cross-toolchain into /usr/local, /opt or your home directory. In my case I have an Ångström armv5te eabi toolchain (created using “bitbake meta-toolchain” if you are curious – full details outside the scope of this tutorial) which is installed into /usr/local/angstrom/arm and the tools have the prefix arm-angstrom-linux-gnueabi-. Whether you are compiling from the command line or Eclipse, you will need to include the path to the toolchain binaries. I generally write a script file that sets the path and then loads Eclipse like this:

$HOME/eclipse/eclipse -vm $HOME/jre1.6.0_12/bin/java

Creating a project

Start Eclipse and select File->New->C Project or File->New->C++ Project. You will have a choice of four project types: Executable, Shared Library, Static Library and Makefile project. With the first three Eclipse will look after building the program using its own internally-generated makefile: this tutorial is about how to set up Eclipse to use a cross-compiler in these cases. With a Makefile project you will provide the Makefile yourself and so you select the compiler and build options outside the Eclipse environment.

For the proposes of this tutorial, create a “Hello World ANSI C Project”. You will find that Eclipse compiles it automatically to produce an executable for your PC – which is not what you want.

Setting the cross compiler

For all project types except Makefile project, you need to tell Eclipse to use the cross compiler rather than the native gcc.

  • Select the project in the Eclipse Project Explorer windows and then go to Project->Properties
  • Expand the section C/C++ Build and select Settings
  • If it is a C++ project, there will be four items in the Tool settings tab: GCC C++ Compiler, GCC C Compiler, GCC C++ Linker and GCC Assembler. If it is a C project, there will be three items: GCC C Compiler, GCC C Linker and GCC Assembler
  • In each case you need to add the toolchain prefix (arm-angstrom-linux-gnueabi-) to the the tool, for example gcc becomes arm-angstrom-linux-gnueabi-gcc as shown below:

If you are using multiple configurations, Debug and Release for example, you need to do this for each of them.

Now, click on Project->Clean to force Eclipse to rebuild and you should find an executable in the Binaries folder of the type of your toolchain, arm/le in my case.

Setting the include parser

This next bit is optional since it only affects the way Eclipse parses #include directives but not the way the program is compiled. Select Project->Properties. Under C/C++ General, select Paths and Symbols. On the Includes tab, select GNU C and add the path as shown in the screen shot below:

Now, if you right-click a header file (stdio.h for example) and select Open Declaration, it will display the header from the cross toolchain, rather than the native one. This may not matter much for headers from libc, which are pretty much the same for every one, but it does make a difference for headers from other libraries that may be totally different or not even installed on your development PC.

Unfortunately it is not possible to remove the paths to the native headers, but normally it won’t be a problem because the indexer will look in the cross toolchain includes first.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s