Showing posts with label crosstool-ng. Show all posts
Showing posts with label crosstool-ng. Show all posts

Thursday, March 6, 2014

How to cross compile for Raspberry Pi using Code::Blocks on Linux

In my privious post I described how I managed to build a cross compiler in Ubuntu for a Raspberry Pi. To compile I used a terminal command. But I want to use a IDE. On Linux I use Code::Blocks for now.

So we have to add a custom compiler to Code::Blocks. Go to
Settings --> Compiler ... --> Global compiler settings
Select the GNU CCC Compiler and click Copy. Give your new cross compiler a meaningful name.


Select your created compiler and go to Toolchain executables. Specify under Compiler's installation directory the path where you keep you cross compiler. For me the path looks like this:
/home/christian/Programming/x-tools/arm-unknown-linux-gnueabi

IMPORTANT: Don't specify the path to the bin directory like in the add-to-$PATH tutorial !!!

After that change the compilers and linkers so it looks like this:


Hit OK and create a new project. Select Console application.


Select your language and your directory where you want to save the project ...


Select your just created compiler...


After that you see already a Hello-World-Program. Try to build it. If it works yout get this notification.


Now we want to check if the cross compiler worked. Copy from your project directory the created file in /bin/debug in your home directory on your RPi. On your RPi (I used ssh) open the terminal and type
 ./ FILENAME  
If you see a "Hello World!" then your cross compiler works !

Program versions:
OS: Ubuntu 12.04 LTS
crosstool-ng 1.19.0
Code::Blocks 10.05

How to build a cross compiler for Raspberry Pi using crosstool-ng

Normally I'm working with a Mac and OpenCV. But I want to run OpenCV on a Raspberry Pi for a small preject. Since I don't want to compile on the Raspberry and lose a lot of time, I was looking for a cross compiler. Looks like this is easier from a Linux machine. I had an old Windows laptop left and so I installed Ubuntu 12.04 LTS along with Windows 7 on it.

I found some really good tutorials about this so I won't repeat them and just give you some links:

1. Very clear tutorial. Lightweight for a good overview:
http://www.bootc.net/archives/2012/05/26/how-to-build-a-cross-compiler-for-your-raspberry-pi/
2. More detailed with screenshots:
http://www.kitware.com/blog/home/post/426

I really like the first tutorial but it's missing information about packages you have to install before you can build crosstool-ng. On Ubuntu you do this with this command in the terminal:
 sudo apt-get install PACKAGE_NAME  
The second link already names some packages, but not all of them. I got errors when I tried to build the toolchain, for example subversion is needed to download a package during the build process. So here is a list of what I installed (replace PACKAGE_NAME with the name below to install):
  • libssl-dev 
  • openssh-server 
  • git-core 
  • pkg-config 
  • build-essential 
  • curl
  • gcc 
  • g++
  • bison 
  • flex 
  • gperf 
  • libtool 
  • texinfo 
  • gawk 
  • automake 
  • libncurses5-dev
  • subversion
Lot's of stuff, but finally I got the toolchain built.

The tutorial says that we have to add the compiler to our $PATH. This is done with this command (don't forget editing the path to your compiler):
 export PATH=$PATH:/PATH/TO/x-tools/arm-unknown-linux-gnueabi/bin  
Remember that this is only temporal! The next time you open your terminal you have to add this to your $PATH again

After this we can compile a "Hello World!" program with this command (source code):
 arm-unknown-linux-gnueabi-c++ hello.cpp -o hello  

Program versions:
OS: Ubuntu 12.04 LTS
crosstool-ng 1.19.0
gcc-linaro 4.8-2013.06-1