Posts: 94
Joined: Sat Feb 04, 2012 8:49 am
Contact: Website

[Howto] Cross compile using Eclipse

Mon Jul 09, 2012 5:20 pm

Today, I manage to cross compile a simple "Hello world" from a virtual machine running Ubuntu 12.04 to the RaspberryPi. It was one of the first things I wanted to do with my Pi. I find it exciting to write some code on a "standard" computer and see it run on a embedded system based on a totally different architecture.

I do not remember all the step I've been through, but here is a simple summary.

It's the first time I achieve this kind to things and I did not find all of this by myself. I got my inspiration by reading this post : http://linuxtortures.blogspot.fr/2012/0 ... ing-c.html. The writer explains how to cross compile for the raspberry pi but he uses another toolchain available in some Debian repositories.

Installing Ubuntu
So, I began by installing Ubuntu 12.04 on a x64 virtual machine (virtual box). Using a virtual machine is not mandatory, it's just that my main OS is Win7 and that I do not have the motivation to install a native linux distro... Virtual machines are so easier!
I installed some base packages (build-essential, git,... and Eclipse-cdt).

Downloading the toolchain
Then, I downloaded the toolchain provided by the foundation (https://github.com/raspberrypi/tools).

Code: Select all

git clone https://github.com/raspberrypi/tools.git 
The repository contains a lot of file and binary. I'm not an expert in Linux, toolchains and cross-compilation things, but I supposed that files in "tools/arm-bcm2708/x86-linux64-cross-arm-linux-hardfp/bin" contain what is necessary to build an executable for the RaspberryPi CPU.
If some documentations exist about the toolchain and its content, I would be very interested.

Cross compile in command line
Now we've got the toolchain, we can try our first cross compilation. Write a simple main.cpp file:

Code: Select all

#include <stdio.h>
int main(int argc, const char* argv[])
	return 0;
I'll see if there are some Belgian people by there...

Then, compile it using the g++ from the toolchain :

Code: Select all

<path to the toolchain>/tools/arm-bcm2708/x86-linux64-cross-arm-linux-hardfp/bin/arm-bcm2708hardfp-linux-gnueabi-g++ -o main.bin main.cpp
Run the file command to check if the binary is an ARM binary:

Code: Select all

file main.bin

main.bin: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.35, not stripped
You can try to run it in the virtual machine, but it won't work. But if you copy it on your RaspberryPi (using the SD card, SSH, FTP, SAMBA, whatever you want...), it should run and print "Oufti!".

Configuring Eclipse
In Eclipse, create a new C++ project. In the "C++ Project" dialog box, select the project type "Cross-compile project".
Add a new file and write some code. Then, go to the project properties.
Go to "C/C++ build -> Settings".
In "Cross settings", set Path to the directory <path to the toolchain>/tools/arm-bcm2708/x86-linux64-cross-arm-linux-hardfp/bin/
In Cross GCC Compiler, rename the command from gcc to arm-bcm2708hardfp-linux-gnueabi-gcc.
In Cross G++ Compiler, rename the command from g++ to arm-bcm2708hardfp-linux-gnueabi-g++.
And the same in cross g++ linker (g++ -> arm-bcm2708hardfp-linux-gnueabi-g++).

Now, build the project, copy the output binary to your Rasbperry and see if it runs correctly.

And next?
I did not try to compile a more complex project with dependencies to other libraries, for example. I don't know how the linker will handle that.
The next thing I want to try is the remote debugging : run gdbserver on the raspberry and debug my software from Eclipse on my computer/virtual machine.
I noticed that an interested plugin exists in eclipse: RSE - Remote System Explorer. Using this plugin, it should be possible to automatically upload the cross-compiled binary on the target (the RaspberryPi), run gdbserver on it and then, connect the local debugger on the Raspberry.

I'll try all of these things as soon as possible ;)
My web site : https://codingfield.com

Posts: 1
Joined: Sun Jan 18, 2015 7:33 pm

Re: [Howto] Cross compile using Eclipse

Sun Jan 18, 2015 7:35 pm

Hello and thank you for your article, here is an another info on setting up Raspberry pi development environment:
http://stackoverflow.com/questions/1916 ... s-compiler

Return to “General programming discussion”