Development Environment for the Raspberry Pi using a Cross Compiling Toolchain and Eclipse

In this blog  entry the setup of  a cross-compiling development environment for the Raspberry Pi will be demonstrated. This will include the

We will finally write a simple Hello World program on our Kubuntu virtual machine (VM), compile it using the cross compiler and then deploy it onto our Raspberry Pi board to run it.

I’m going to assume that you have already installed a Raspbian Wheezy image on your RPi board and that you have Ubuntu / Kubuntu Linux installed either has a host OS or guest OS on your desktop PC.

A remote debugging tutorial; which I consider to be the continuation of this tutorial, can be found here.

So what is a cross compiling toolchain and why use one ?

A native compiler such as the default gcc tool on Kubuntu  is a compiler that runs on an Intel machine, as well as creates binaries intended to be run on an Intel machine. i.e it creates binaries for the same type of machine that it runs on. Similarly the GCC tool in the RPi’s Raspbian Linux OS is intended to run on an ARM machine as well as creates binaries for an ARM machine.

A cross compiler such as the “arm-linux-gnueabihf-gcc” that we will use is able to run on an Intel machine but creates binaries for an ARM machine. In other words, it runs on one architecture and creates binaries for another. This allows us to develop and compile our programs on our Desktop PC but when it comes to deploying the binaries we deploy them and run them on the Raspberry Pi.

So why use a Cross-compiler instead of  developing our code and compiling it natively on the Raspberry Pi itself? After all, the Raspberry Pi has a native GCC compiler. We can also use code editors such as nano or vi from the command line (remotely over SSH) or GUI programs such as Geany (remotely over VNC).

The main case for cross-compilation over native compilation (develop and compile on the RPi itself) is that it tends to be faster. Remember the RPi board may be fast compared to a microcontroller…but its still has limited RAM resources and is pretty slow compared to an average desktop computer….

Also you have a myriad of development tools that you can use on your desktop PC that you simply can’t use on the Raspberry Pi; such as the Eclipse IDE.

Now that I’ve explained the why…let’s get started!

Downloading and Setting Up the Cross Compiling Toolchain

  • Open a terminal window on your desktop side Kubuntu/Ubuntu Linux OS and type the following command: “sudo apt-get install git“. This will install the GIT tool on your desktop linux box which in turn will enable us to download / clone the cross compiling toolchain from GitHub.com.
  • If you’re running a 64-bit Ubuntu/Kubuntu Linux OS you will probably also want to install the 32-bit library/header files. Since the cross compiler will need them to properly run. You can do this by typing “sudo apt-get install ia32-libs“. Again this step is only required if you are running a 64-bit Linux OS.
  • Once git is installed, create a “raspberrypi” directory in your home directory by typing “mkdir raspberrypi“.
  • Go to the raspberrypi directory by “cd raspberrypi” and then type: “sudo git clone git://github.com/raspberrypi/tools.git
  • This command will download (clone) Raspbian’s official cross compiling toolchain from Github. The command will take a few minutes to complete its task.
  • When the previous command completes, navigate to “/home/halherta/raspberrypi/tools/arm-bcm2708″ using “cd ~/raspberrypi/tools/arm-bcm2708” on your Deskop Linux OS.
  • In the arm-bcm2708 folder you’ll see three other folders each containing a separate toolchain:
  1.  arm-bcm2708-linux-gnueabi
  2.  arm-bcm2708hardfp-linux-gnueabi
  3.  gcc-linaro-arm-linux-gnueabihf-raspbian

Of those three we will use the third one. The next step is to add the directory containing the binary files of the third toolchain “gcc-linaro-arm-linux-gnueabihf-raspbian” to the PATH environment variable in linux. This way we can access the toolchain’s binary files from anywhere on our computer. We will do this by adding an “export PATH”  command to the bottom of the .bashrc and .profile files in the home directory.

  • In a terminal window (on your Desktop Linux OS) type “cd ~/“  to point to the home directory, then type “nano .bashrc“. This will open the .bashrc file in a command line based editor called nano. Go to the bottom of the .bashrc file using the down arrow key. Then type the following command:

“export PATH=$PATH:$HOME/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin”

  • Then hit Ctrl+x to exit. You will be prompted to save changes. Say yes and hit “Enter”.
  • Similarly in our home directory type “nano .profile“. This will open the .profile file in the command line editor nano. Go to the bottom of the .profile file using the down arrow key. Then type the same command again:

“export PATH=$PATH:$HOME/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin”

  • Then hit Ctrl+x to exit. You will be prompted to save changes. Say yes and hit “Enter”.

Now restart your machine. (You probably could just log out and log in again)

Ideally you need to export the toolchain’s directory path to the PATH variable in either the .bashrc or the .profile files. This didn’t work for me. If it works for you thats great.

When you log-in  into your desktop Linux OS, open a new console window and type: ” arm-linux-gnueabihf-gcc -v”. If you were successful you should see output similar to that in Figure 1. Congratulations! you just installed Raspbian’s official cross compiling toolchain on your Desktop PC / Virtual Machine!

Figure 1. Cross Compiling Toolchain successfully installed and accessible from anywhere within your Desktop side Linux OS!!!

Downloading and Setting Up Eclipse

The Next step is to download and install Eclipse. Unfortunately as of this writing the apt-get repositories have yet to contain the latest Eclipse build…Eclipse Juno. So we will not be using the apt-get package manager. Instead we will download the latest Eclipse IDE from the web using a web browser.

  • Before we can run eclipse, we need to ensure that we have a Java runtime environment installed; since eclipse relies heavily on Java. We can do this with the following command: “sudo apt-get install openjdk-7-jre
  • Go to the link provided below and download the linux version of the Eclipse IDE (Juno) for C/C++. Download the 32-bit version of the IDE if you’re running a 32-bit Linux OS or download the 64-bit version of the IDE if you’re running a 64-bit Linux OS.

Link: http://www.eclipse.org/downloads/packages/eclipse-ide-cc-developers/junor

  • If you’re not sure whether you’re running a 64-bit or 32-bit Linux OS, open a terminal window and type” uname -a“. If  the output of that command contains “i686″ or “i386″, you’re running a 32-bit Linux OS. If in the output you see “x64″ then you’re running a 64-bit Linux OS.
  • The next step is to extract the “eclipse” folder into our home directory from the eclipse-cpp-juno-linux-gtk.tar.gz compressed file. This can be easily done using the Dolphin File Manager. Navigate to the directory in which “eclipse-cpp-juno-linux-gtk.tar.gz” was downloaded. Click on the file. This will open a decompression tool. Click on extract and choose to extract to the /home/halherta directory. Alternatively open a new console and navigate to the directory containing the .gz file then type: “tar -xvzf eclipse-cpp-juno-linux-gtk.tar.gz -C ~/“. Either way you will end up having  the eclipse directory created in your home directory. To start eclipse, go to the home directory “cd ~/” then ‘cd’ your way into the eclipse folder “cd eclipse” then type “./eclipse &“.
  • If you have been following my series of tutorials from scratch and/or are running Kubuntu, you can enable starting eclipse from the start menu right click on the blue KDE start menu button and click on  “Edit Applications”. This will open the KDE Menu Editor.
  • Select on the “Development” tab in the right of the window and then click on the “new item” button. A message box will pop-up. Enter under “item name”: “Eclipse Juno for C/C++ Developers”. Then click on the “open file dialog” adjacent to the “command field” and select the eclipse binary which should be “/home/halherta/eclipse/eclipse”. Furthermore, click on the icon box. This will open an icon source box select the “other icon” option  and then click on the browse button. Browse to the “/home/halherta/eclipse/” folder and then select the “icon.xpm” file.

Figure 2. Choose “Other Icons” and hit “Browse” button to select “icon.xpm” file in the eclipse directory

  • After these changes the KDE Menu Editor window should look like the illustration shown in Figure 3.

Figure 3. Adding Eclipse to the Start Menu

  • Finally click on the “Save” button and then close the KDE Menu Editor. If you now navigate the KDE  menu, you should find eclipse under “Applications->Development”. If you click on it the Eclipse IDE should start.
  • Note if you’re running Ubuntu (without Unity…just gnome classic) you can right click on the “Applications Menu” and select “Edit Menu” to edit your gnome-based Ubuntu start menu and add to it an entry for eclipse in a similar manner as demonstrated above. And don’t ask me about Ubuntu Unity integration..I hate it.

Creating a New Project in Eclipse

When Eclipse runs, it will first launch the workspace launcher. You will be prompted to choose a workspace directory. Accept the default. Feel free to check the “Use this as the default and do not ask again” option if you feel inclined to do so. (You can always switch/change the workspace directory by going to “File->Switch Workspace” ).

Figure 4. Workspace launcher

  • You should then see a Welcome Tab (Figure 5). Click on the ‘x’ on the Welcome tab to close it

Figure 5. Welcome Tab. Click on the ‘x’ in the tab to close it

  • Now go to File->New->C++ Project. This will open the “C++ Project” window shown in Figure 6.

Figure 6. C++ Project Window

  • Type “HelloWorld” in the “Project Name” field. Under Project type select “Executable->Empty Project” Under “Toolchains” select “Linux GCC”. Then click on “Next” This should take you to the “Select Configurations” Window. Accept the defaults and click “Finish”.
  • This should be the end of the basic project configuration. Your Eclipse Environment should look as shown in Figure 7.

Figure 7. New Hello World Project initial setup

  • Notice how this project does not have any source files so lets add one. Click on “File->New Source File”. This should open the “New Source File” window.

Figure 8. New source File Window.

  • Ensure that the “Source folder” and “Source file” fields include “HelloWorld” and “HelloWorld.cpp” respectively as shown in Figure 8 and click Finish. This will add a HelloWorld.cpp source file to our project. In the source file type the following basic C++ code:
/*
 * HellWorld.cpp
 *
 *  Created on: 2012-09-27
 *      Author: halherta
 */

#include <iostream>

using namespace std;

int main (void)
{
	cout << "Hello RPi Development World !"<< endl;
	return 0;

}
  • Save your project by clicking on “File->Save”.
  • Now we will need to access the Project properties to ensure that the correct compiler (the Raspbian cross compiler)  and its header and library files are being used. To do this select the “HelloWorld” folder/project in the project explorer and then click on project properties. This will open the “Properties for HelloWorld” window.

Figure 9. Include directories for the cross compiler

  • First go to “C/C++ General -> Paths and Symbols” from the left hand side navigation bar. In the “Includes” Tab make sure that all the include paths included in Figure 9 are added.
  • Then go to the “Library Paths” and make sure that all the library paths in Figure 10 are added.

Figure 10. Library Path for the Cross Compiler

  • Finally go to “C/C++ Build -> Settings” (Figure 11) from the left hand side navigation bar. Under the “Tool Settings” Tab select “GCC C++ Compiler” for that Tab’s Navigation bar and ensure that the command field contains: “arm-linux-gnueabihf-g++”
  • Similarly the command field for the “GCC C Compiler” should contain “arm-linux-gnueabihf-gcc”
  •  Similarly the command field for the “GCC C++ Linker” should contain “arm-linux-gnueabihf-g++”
  • And finally the command field for the “GCC Assembler” should contain “arm-linux-gnueabihf-as.

Figure 11. C/C++ Build->Settings Make sure you select cross compiler in command fields

Then click on the Apply and OK buttons. At this point the Eclipse IDE is configured to utilize the Raspbian cross compiler.

To build the project click on “Project->Build Project”. To clean the project click on “Project->Clean”. Figure 12 shows the Eclipse workspace with the HelloWorld program successfully cross compiled for the Raspberry Pi!!!

Figure 12. HelloWorld program successfully compiled for Raspberry Pi.!!!

The binary file for our HelloWorld application resides in  the “~/workspace/HelloWorld/Debug”. Lets go there via a console window with cd “cd ~/workspace/HelloWorld/Debug/“. If we  type in “ls -al” we’ll find that our Binary file is highlighted in green and is called HelloWorld. If I try to run it with “./HelloWorld” I get the following error message “cannot execute binary file“. Now if I type “file HelloWorld“, I get the following output: “HelloWorld: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.26, BuildID[sha1]=0xb37e6eaa466429d015cc334d55f5d46c97c9fc6c, not stripped“. This indicates that the HelloWorld Binary File was compiled for a 32-bit ARM machine and cannot run on an Intel Machine.

Deploying the Binary file onto the Raspberry Pi

To deploy the binary to the Raspberry Pi, make sure that the RPi board is powered and connected to your network. The next step is click on “Window->Show View->Other”. This will open a show view window. In this window select the “Remote Systems” folder and then select “Remote Systems Details” and press OK (Figure 13).

Figure 13.

  • If the “Remote Systems Details” shows up in the bottom of the workspace, drag it such that its tab shares  the same region as the project explorer tab as shown in Figure 14.

Figure 14.

  • Now right click in the “Remote Systems Detail” area and click on “New Connection”. In the “Select Remote System Type” window (Figure 15), select “SSH only” then click “Next”

Figure 15.

  • In the “Remote SSH Only System Connection” type in the IP address of the RPi in the “Host name” field and give the connection a valid name in the “Connection name” field. I chose “RaspberryPi”.  Then click “Finish”.

Figure 16.

  • At this point a second device (Raspberry Pi) shows up in the “Remote Systems Detail” tab. Right click on the Raspberry Pi resource and click connect. You will be prompted to enter the username and password. Make sure that you utilize “username:pi” and “password:raspberry” (if you haven’t changed the default password). You may get a warning windows asking you if you are sure that you want to accept this connection. Affirm that you want to proceed with the connection. At this point you should be connected to your RPi from eclipse

Figure 17.

  • Right click on the Raspberry Pi resource again  in the “Remote Systems Detail” tab and click on the “Show in Remote Systems View”. This will open a “Remote Systems” tab in the same region of the workspace. In this tab, one can navigate the root file systems of both the Kubuntu (local) OS and that of the Raspbian/Raspberry Pi.
  • Locate the HelloWorld Binary file on our local desktop PC and drag it to the home directory on the Raspberry Pi. This effectively copies the Binary file to the home directory of the Raspberry Pi.

Figure 18.

  • Now right click on the SSH terminal icon under the Raspberry Pi icon in the  “Remote Systems” and select “Launch Terminal” . This should launch a terminal window in the bottom of the Eclipse workspace (Figure 19). This is basically a console / terminal window connected to the Raspberry Pi via SSH.

Figure 19.

  • In the terminal window type “ls -al” to confirm that the “HelloWorld” binary indeed got copied into the home directory of the Raspberry Pi’s root file system. Then change the “HelloWorld” binary’s permission to make it executable: “chmod +x HelloWorld
  • Finally type “./HelloWorld“. You should see the output shown in Figure 19. :“Hello RPi Development World !”
  • Congratulations!!! You just developed and ran your first cross compiled application for the Raspberry Pi.!!!!
  • To remotely debug your applications on the Raspberry Pi from Eclipse, check out this blog entry!


This entry was posted in Raspberry Pi. Bookmark the permalink.

113 Responses to Development Environment for the Raspberry Pi using a Cross Compiling Toolchain and Eclipse

  1. Kieron Quin says:

    This looks a really good simple guide, thinks for this.
    I guess this process should also work using the mac version of Eclipse too, albeit with some differences.
    Anyway, I’m going to give it a go and see what happens. What could possibly go wrong ;)

    Thanks again.

    Kieron

  2. Timon says:

    Perfect work!!!

    Thank you! :-)

  3. Nolan says:

    You, my friend, are a life saver! This saved me a ton of time. I don’t know how this tutorial was not the number 1 result on google for “raspberry pi cross compile”, but it should be.

    Cheers!

  4. mapi says:

    Simply great!
    Thanks!

  5. Steve Hobley says:

    Hi, I followed your directions with Kubuntu in a VM image. I can build the example application correctly but Eclipse is giving me a few errors regarding the code. It’s failing to recognise the include and use of ‘cout’ – and yet the compilation itself is fine. I posted images here : http://www.flickr.com/photos/stevehobley/8124559636/in/photostream
    http://www.flickr.com/photos/stevehobley/8124559612/in/photostream
    It looks like it is still using standard g++ environment for highlighting ‘bugs’, but the correct compilation environment when building the code. Is this something I can configure somewhere?

    • halherta says:

      Steve,
      you can disable the Eclipse errors by going to Window/Preferences – C/C++ / Code Analysis and disable “Syntax and Semantic Errors”.

      You shouldn’t have to do this however. Are you sure that you’ve included all the necessary library and include paths as per figures 10 and 11 ?

      UPDATE: Yes you need to install build-essentials if you haven’t already. I mentioned this in the “Setting up a Kubuntu virtual machine using VMware Player” article

      I will add this info to the blog entry thank you for the feedback and I’m glad that its working for you!

      • Steve Hobley says:

        Thanks – yes that was it.

      • Anders says:

        I had the exact same problem, but installing build-essential was not enough.
        I also had to add the following to Paths and Symbols (adjust base path to you RPi directory)

        /home/ubuntu/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/arm-linux-gnueabihf/include/c++/4.7.2
        /home/ubuntu/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/arm-linux-gnueabihf/include/c++/4.7.2/arm-linux-gnueabihf
        /home/ubuntu/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/arm-linux-gnueabihf/libc/usr/include/arm-linux-gnueabihf

        Cheers

  6. Miki says:

    Slightly off-topic. Can you please tell me why are there different toolchains around? Most distros have an ARM targeted gcc and binutils. I use Arch and there is arm-elf-gcc-base. Why go through the trouble of getting other custom gcc’s?

    • halherta says:

      Miki you raise a great question. Theoretically any arm based toolchain could be used to compile code for the raspberry pi ..an ARM Machine. If the Kernel is compiled using the “hard float” option however, one should use a “hard float” (hf) compiler to get optimized binaries. Last time I checked the GIT repo for the Raspberry Pi tools there were three toolchains. The arm-bcm2708hardfp-linux-gnueabi & the gcc-linaro-arm-linux-gnueabihf-raspbian are both “hard float” and should both work on Raspbian which is itself (the Raspbian’s Linux Kernel) compiled with “hard float”. The other toolchain arm-bcm2708-linux-gnueabi is not “hf” and may or may not work well with Raspbian. Even if it does compile… it will not create lean fast optimized code…especially if one is doing a lot of numerical computing.

      I noticed that Ubuntu has recently added an armhf (with hard float) compiler to its repository which might very well compile code for the raspberry pi but I didn’t test it yet. Installing it is a simple matter of typing something like “sudo apt-get install gcc-arm-linux-gnueabihf” in your Kubuntu/Ubuntu Box.

      You can also create your own custom toolchain using the crosstool ng scripts. The process is simple and documented on many other blogs such as this. Make sure you select hardfp instead of softfp however in the menuconfig….

  7. Alco says:

    HI, I followed yor instructions, but…. now i’m stuck. I added the includes and the library paths and checked the command fields in the C/C++ Build -> Settings. The command fields seemed to still have the default commands. I entered the commands they should have, with no result. Now, when I try to build, the console tells me there is nothing to build. Can you give me a hint where i went wrong?

    • halherta says:

      Alco, You can try cleaning the project and then rebuilding. If you’ve already built the project and try to rebuild it again, it will say that there is nothing to rebuild.

  8. Daniel says:

    Hi! Excellent post!
    Can you tell me which tool chain you recommend when the OS in Raspberry PI is Arch Linux?
    I need to use another one or the one you choose on this post also fine?

    • halherta says:

      Daniel,
      I believe the toolchain that I’m using for this tutorial will be fine…..But I haven’t checked it myself. You can always try to create your own custom toolchain using the crosstools-ng scripts

    • Nate says:

      I just did this tutorial using Arch Linux and it worked just fine :)

  9. Martijn says:

    Hi, Thanks for the detailed instructions.
    The only problem I had was the missing of the make file. The cause of this problem is, I didn’t follow your insturctions about installing a virtual machine, while I already had installed Kubuntu in a virtualbox.
    In my virtualbox the build time is 8s.629 while on your system it take only 667ms. So I will follow your insturctions also to setup a virtualbox to see if it will compile much faster.

    • halherta says:

      Martijin,
      You do not need to use makefiles at all. Eclipse takes care of the entire build process for you. The build time will also depend on your machine. I’m using an Intel-I7 Core with 8GB of RAM. A slower machine might exhibit slower build speeds. Though if it takes you more than 8 seconds to build such a simple program, you maybe better off using the Raspberry Pi’s (Raspbian’s) native compiler and do all of your development on the Raspberry Pi itself.

      • Martijn says:

        Hi, thanks for the reply. While I used the virtualbox virtualmachine, in Eclipe C/C++ build Tool Chain Eiditor, the Current builder was set to Gnu Make Builder. If you change this to CDT Internal Builder than it is working, otherwise you have to install make.

        I installed a second virtual machine in VMPlayer according to your instructions. After this the build time is reduced to 2.22 sec. I’m using an Intel Core 2 Duo @ 2.2 GHz with 8GB of RAM. In the VMPlayer I select the option to use 2 cores.

  10. Alco says:

    A little late, but still, Thanks for your reply! I have struggled a lot, and failed to get it working on 64-bit ubuntu. Therefor I tried it on a Kubuntu VM last evening, and I finally got it working! Only now my Pi seems to have died, yet another challenge.

  11. Shiju P K says:

    Hi, I followed all the steps, But when I built the code i am getting following error

    **** Build of configuration Debug for project HelloRPi ****

    make all
    Building file: ../HelloRPi.cpp
    Invoking: GCC C++ Compiler
    arm-linux-gnueabihf-g++ -I/home/shiju/rasberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/arm-linux-gnueabihf/include -I/home/shiju/rasberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/arm-linux-gnueabihf/libc/usr/include -I/home/shiju/rasberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/lib/gcc/arm-linux-gnueabihf/4.7.2/include-fixed -I/home/shiju/rasberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/lib/gcc/arm-linux-gnueabihf/4.7.2/include -I/home/shiju/rasberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/lib/gcc/arm-linux-gnueabihf/4.7.2/finclude -O0 -g -Wall -c -fmessage-length=0 -MMD -MP -MF”HelloRPi.d” -MT”HelloRPi.d” -o “HelloRPi.o” “../HelloRPi.cpp”
    /bin/sh: 1: arm-linux-gnueabihf-g++: not found
    make: *** [HelloRPi.o] Error 127

    **** Build Finished ****

    any help??
    PS: I can run arm-linux-gnueabihf-g++ from terminal (i added bin to PATH)
    Thanks,
    -Shiju

    • halherta says:

      Shiju, If you are using Ubuntu / Kubuntu, you must add the toolchain binary directory to the PATH variable in both .bashrc and the .profile files in order to enable eclipse to see the toolchain binaries. This might be implemented differently in other distros….

  12. Andrea says:

    Hello, do you know how you can create a GUI application for raspberry using eclipse?

    • halherta says:

      Andrea,
      You will have to use an additional set of GUI Libraries such as QT and/or GTK.

      You would also have to cross compile these libraries first (using the cross compiler) before you can use them.

      If you want to use QT, checkout QTCreator its a great IDE that makes QT development easy.

      If you’re not concerned about speed, try developing QT applications natively on the Pi. just install QTCreator + QT Library on the Raspbian ..something like “sudo apt-get install qt-sdk”. This should work ok, especially if you have the 512MB Pi.

      Another way to achieve “cross compilation” (sort of) without a cross compiler is to simulate a Raspberry Pi environment using QEMU and then “chroot” into it. Check out the link below. You will still need to set the X-server to display GUI from the chrooted environment.

      http://superpiadventures.wordpress.com/

      By simulating Raspbian (ARM Linux) on your PC you will have little need to cross compile since you can compile using the native ARM GCC compiler in the simulated chrooted environment.

      I had some success with the chrooted setup but I found compilation to be relatively slow

      Also google QT5 for Raspberry Pi there’s lots of information on that on the web

  13. Mark Roberts says:

    I’m sure I’ve missed something… I’m stuck at building the HelloWorld project with an error “make:***[HelloWorld.o] Error 127″

    When I went back to default settings for the compilers, etc. it built o.k.

    • Mark Roberts says:

      Installing from scratch again cleared up my problem…
      -mark.

      • halherta says:

        Mark,
        Glad you were able to get it going! Were you able to figure out what was missing the first time round?

        • Paul Gullett says:

          I saw this when setting up on 64 bit Fedora 18. It was related to a missing 32 bit version of the zlib library. This was flagged in the log file for the compilation, found under the C/C++ Build section.

          As an aside the following are the required packages on 64 bit Fedora 18 to make this work.

          glibc.i686
          libstdc++-4.7.2-8.fc18.i686
          zlib-1.2.7-9.fc18.i686

  14. Garry Smith. says:

    Thanks for your tutorial.
    Unfortunately the compiled binary, when run on the Pi, creates a segmentation fault. I’m new to these gnu tools and I’m not sure where to start looking for the cause of this problem. Could you give me a starting point to find the cause.
    Thanks in advance, Garry

    • halherta says:

      You got a segmentation fault for a simple hello world program? That is very weird indeed. I will look at the procedure again and report back if I find something.

      In the mean time if you could try repeating the tutorial to see if you can fix this (preferably in a new virtual machine). One thing that I didn’t mention in this tutorial (I’m assuming you already have it) is that you may need to install the build-essentials package on your linux PC/VM (“sudo apt-get install build-essentials” in debian/ubuntu based distros). This package has primarily the native intel toolchain for the PC but it seems that eclipse requires some parts of it….(I doubt that was the problem but you can try it anyways if you haven’t already)

      • Garry Smith. says:

        Thanks for having a look at this and I’ll make sure that I install the build-essentials as you mention.
        Taking my lack of inexperience with GCC tools into account, is it likely that the libraries on the RPi are different to the ones that I built the binary on, would that be a problem. I did go back an start a new VM and followed the tutorial again with the same result. I did try a different tutorial as well (before I found yours’) and it produced the same result. I’m sure that there is something minor I’ve missed that is causing the problem. I’m setting up a Qemu Raspberry Pi emulator to see if the binary will run on that. In a divide and conquer manner. I could also sent you the binary to try on your RPi. This technique should tell us if it is the compiler setup or my RPi, or you could send me your Hello World binary to try on mine.
        (BTW I noticed that I had to tick the “Use Project Build options” (or similar) before the build would work.)
        Thanks again,
        Garry.

        • Garry Smith. says:

          I tried the HelloWorld binary in the Qemu RPi emulator and it ran as expected.
          The “file” information does say that it is using dynamically linked libraries. So I’ll try to work out which ones (somehow) and make sure that Qemu and RPi libraries match ( I recall that I had some problems apt-get library something or other while installing something on my pi. I could also burn a clean image on my RPi and try that.
          Thanks again.
          Garry.

          • Garry Smith. says:

            mmmm. Found the problem. and you probably not going to guess where.
            VM Debian Filezilla file upload. The file is 62,958 on the PC and is 63,023 on RPi. If I use Filezilla in windows the file is the same size and runs correctly. Noticed that ldd had an error of Non-executable binary(or similar) and no listing of used libraries.mmmm.
            Anyway problem found and a work around exists(be it painful to samba onto PC/c:/temp. WinFilezilla to RPi chmod ./HelloWorld). Hope this helps some one. Continuing on with your excellent Tutorial.
            Best regard,
            Garry.

        • halherta says:

          Garry, To build a simple program as HelloWorld, the libraries that you need are the stanard C/C++ libraries. These libraries must be compiled for ARM as well. Luckily these libraries are already compiled for arm and included in the toolchain so this should not be a problem. If however you decide at somepoint to cross compile code that uses other libraries, such as QT, GTK+, Libusb e.t.c then yes you would have to either compile these libraries from source for arm (using the arm cross-compiler) or find pre-built packages built for arm (with an toolchain that creates arm binaries)

  15. Garry Smith. says:

    or I could have just continued with your tut to realize I was dill. now removing egg from face. The rest of the tut works like a dream. Thanks – Garry.

  16. Maria says:

    Hi,
    Thanks for an easy-to-follow tutorial! it helps me a lot :)
    But now I need to cross compiling OpenNI to my Raspbian n don’t know where to start. Can I just add its library n headers from eclipse? or maybe build a correct toolchain?

    Thanks in advance,
    Maria

    • halherta says:

      Maria,
      Since you already have a cross-compiler for ARM (Raspberry Pi) installed on your PC, you should theoretically be able to compile the OpenNI library from source on your linux PC using the RPi (ARM) cross compiler. This will create the necessary library binaries (for ARM) which you can then link into your applications that will also be compiled by the cross compiler. If you have access to the source code for the openNI libraries this (theoretically) should not be a problem.

  17. luan says:

    I am having the same probrem that Garry Smith sad above. When I run on the Pi creates a segmentation fault. I didn’t understand how he solved this problem, could you help me?

    • halherta says:

      luan, are you building the helloworld program in eclipse ? Did you follow all of the instructions? I highly recommend that you use the RSE plugin (eclipse built-in SSH) to transfer the binary from your linux PC/VM to the Raspberry Pi before running the program.

    • Garry Smith. says:

      Hi Luan,
      The problem that I had was that I didn’t read the remainder of the Tut, which demonstrates how to use eclipse to download the binary exe file to the RPi. and boldly went ahead to use filezilla on my Debian Virtual Machine on a winXP pc. Filezilla changed the binary somehow (probably used text mode coping ) while downloading to the RPi to a point that the file would not run (segment fault). When I followed the remaining section of the Tutorial it was all good.
      What I found, apart from the file download prob, was the command line library usage information utility “lld” (lowercase LLD) and “file” these give you information about the libraries used and the the type of binary executable. ( Google them until you find an explanation that make sense to you.
      Let us know what you find. and if you need more help. ( I’m learning linux as I go).
      Garry.

      • luan says:

        Sorry for the late reply, I was traveling. Thank you for your replies.
        So, I follow all steps of this tutorial. I am using Eclipse Juno Service Release 1 and Ubuntu 12.10 in a host PC. The code compile and the binary is created very well. I use the RSE to transfer the binary for RPI. But, when I execute the program on RPI, the segmantation fault is created.
        I am new on linux. I will install other linux distro on my PC and try again. If I solve the problem I tell you here.
        Thanks.
        Luan

        • Josh says:

          I was wondering if this issue was ever resolved? I am running Ubuntu 12.04 LTS (64 bit) with Eclipse Juno Service Release 1 inside VMware Workstation 9. I followed all steps in the tutorial, but get a Segmentation Fault when I execute on the RPI.

          I’m also learning linux as I go, so any help would be greatly appreciated. My next step is to play with the Qemu emulator like Gary Smith suggested above. If that doesn’t work I could try another linux distro, but it seems I’m probably just missing something that someone with more experience could point out.

          This is a wonderful tutorial! I’m looking forward to starting my first RPI project, so getting a debugging environment setup would be a good first step.

          • halherta says:

            Josh, Did you transfer the binary over to the RPi using the RSE plugin ? Can you please provide me with the output of the following command: “file HelloWorld” (or “file [name of binary]“) when executed on ubuntu (in the directory in which the binary exists…..

          • Josh says:

            I did transfer the binary over to the RPi using the RSE plugin. The result of “file HelloWorld is below”.

            pi@raspberrypi ~ $ file HelloWorld
            HelloWorld: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.31, BuildID[sha1]=0x208ca613cce3f01ecb00705b772022e000ceb830, not stri
            pped

            I am curious if there is anything else that needs to be done since I am running a 64 bit host OS? I did install the iar32-libs as you mentioned above. I’m running Raspbian Wheezy on the RPi:
            http://downloads.raspberrypi.org/images/raspbian/2012-12-16-wheezy-raspbian/

            Thank you for your quick response. I appreciate the help!

          • Josh says:

            I created a new VM with Ubuntu 12.10 (32 bit) and everything worked beautifully after following Anders recommendation for the additional Paths and Symbols.

            I did notice that the file size is smaller on this build than Ubuntu 12.04 LTS (64 bit). The “file HelloWorld” command now returns:

            pi@ubuntu:~/workspace/HelloWorld/Debug$ file HelloWorld
            HelloWorld: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.26, BuildID[sha1]=0x3ad90103daea30331ca7d5dadf5f275b7a877a32, not stripped

            It looks like the GNU/Linux version is different (2.6.26 vs. 2.6.31). Being new to the tool chain, I would like to understand the difference between these and why it resulted in a Segmentation Fault when running on the RPi. However, I am happy that I have a working environment.

            Thanks again for this great tutorial!

  18. Francesco says:

    Thank you very much for the guide. I installed eclipse and toolchain on Ubuntu 10.10 in a VM. I followed your guide, built the example HelloWorld. Works fine, now beginning to fun

  19. Wael.Ch says:

    Thanks very very mush for the tutorial..
    I used to do all the instructions and every things goes right.
    BUT, in the end when i put “./firsttest” (firsttest is the name of my application), I got:

    “-bash: ./firsttest: No such file or directory”

    Please i need your help as soon as possible!!
    thank you so mush.

    • halherta says:

      Make sure that you’ve successfully transfered the file to the correct directory by running an “ls” command on the RPI via the SSH session. When transfering the file use the RSE plugin by dragging it from the Linux PC to the RPi home directory, sometimes the file is transfered to another folder within the home directory, Also make sure that you make the file executable with the chmod command. If the programs attempts to access certain resources such as GPIO then you will need to run it as root

  20. neil says:

    I found this a very usefull tutorial and hello world worked for me.
    However not being very familiar with the IDE I was wondering if there is an easy way to setup the project settings without having to carefully re-type the paths and compiler tool names. ie is there way of having a “template” of just these that can be imported or something like that. I did a google on adding new toolchains but this started to look very involved.

    • halherta says:

      neil,
      There most likely is a better way of setting up all of the paths but I’m not aware of it. If you this better way, I’d appreciate it if you could let me know.

  21. Steve says:

    Amazing tutorial worked great for Hello World App. I am working on a socket based app (in C) and Eclipse cannot seem to find defines such as AF_INET, SOCK_STREAM. I am having the same issue with the signal.h file as well. Are there other include paths needed? Sorry I am a complete noob to Linux programming.

  22. Steve says:

    Figured it out! Needed to add the following include path to the build

    /home/steve/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/arm-linux-gnueabihf/libc/usr/include/arm-linux-gnueabihf

  23. John says:

    Thanks for the tutorial, I found this extremely useful. I just wanted to know how one would link commonly used libraries. For instance, I want to use the pthread library – would it be correct to link by augmenting the compile commands by -lpthread (would this automatically use the pthread library in gcc-linaro-arm-linux-gnueabihf-raspbian directory?). Thanks

    • halherta says:

      John I haven’t used Eclipse in a while now, but you would need to specify the directory containing the libraries in the window in figure 10. You then have to specify the library name in the same window in figure 10 but under the “libraries” tab. From the command line you need to use the “-L” parameter to specify library directories and “-l” parameter to specify the library file in those directories to be linked to the project.

  24. Neil says:

    In addition to the above I have also found I needed to add two extra paths for c++ to get around eclipse bugging me with “unresolved inclusion” on #include

    My full list of include paths now looks like this :

    ${HOME}/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/arm-linux-gnueabihf/include

    ${HOME}/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/arm-linux-gnueabihf/libc/usr/include

    ${HOME}/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/arm-linux-gnueabihf/libc/usr/include/arm-linux-gnueabihf

    ${HOME}/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/lib/gcc/arm-linux-gnueabihf/4.7.2/include-fixed

    ${HOME}/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/lib/gcc/arm-linux-gnueabihf/4.7.2/include

    ${HOME}/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/lib/gcc/arm-linux-gnueabihf/4.7.2/finclude

    ${HOME}/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/arm-linux-gnueabihf/include/c++/4.7.2

    ${HOME}/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/arm-linux-gnueabihf/include/c++/4.7.2/arm-linux-gnueabihf

    • Charlie says:

      I too find that I had to include “/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/arm-linux-gnueabihf/libc/usr/include/arm-linux-gnueabihf” among the paths.

      The very important “sys” and “bits” directors are in there. Almost any real project is going to need these. Took me forever to figure this out too.

      I feel he really should at least add this to the tutorial.

  25. Neil says:

    What would be the correct approach to installing packages/librarys for use by the cross compiler on the host so they don’t get mixed up with the regular librarys.
    For example I added the jpeg library on the RPi using :

    sudo apt-get install libjpeg8-dev

    Now my Ubuntu host m/c already has this installed, well the i386 one atleast. So how does one install the armhf version without blowing holes in the existing installation ?

    • halherta says:

      Neil You would have to build the libjpeg8 binaries/library from source using the arm-linux-gnueabihf-gcc cross compiler on the Linux PC/VM. Then save it in its separate folder structure on the Linux PC/VM. Then reference the location of the library/include files in eclipse as you did with standard libraries/headers in figure 9 & 10. You will also need to specify the specific library to be linked under the “library” tab in properties window (see figure 9). This is how I think one would do it..at least theoretically. The problem with this approach is that you will also have to build any other dependencies that the libjpeg8 library might have in the same way.

      • Neil says:

        I did in the end sucessfully transfer the OpenGL ES2.0 library installed on the RPi to my KubuntuVM host by simply copying /opt/vc/* from one m/c to the other. I did have some problems with the compiler options before I could get something to compile and run. See http://www.raspberrypi.org/phpBB3/viewtopic.php?f=67&t=14863&sid=a4349da8a84b7f69138792145a388f63&p=259128#p259128

        However I felt my approch to this was something of “hack” rather than the proper way to do it, hence my question. However I do have an OpenVG example compiling on the host under the Eclipse IDE. I am about to have a go at you gdbserver tutorial as well.

        After a bit of google research I discovered that it might be possible to use a command such as “sudo apt-get install libjpeg8-dev:armhf” which I thought might install an already cross-compiled version of the library to my Kubuntu. I thought better of trying it for fear it would just just overlay on top of my i386 version of the same library.

        To re-build cross compiled librarys such as “libjpeg8-dev” how would I get the source installed on my Kubuntu ready for a “make” ?

        • halherta says:

          Neil, You should be able to download the libjpeg library from here . and installation instructions are here . Once you get the code, untar it and in its main directory type something like ” CC=arm-linux-gnueabihf-gcc ./configure –host=arm-linux –prefix=/opt/libjpeg” where the prefix determines where to place the compiled library files and –host determines which system will we be compiling for….I’m not sure for host if we need to specify “arm-linux” exactly but it ought to be something similar. Then “make” and “make install”.

          I cannot confirm that what’s above will work but hopefully it will give you a good start.

  26. Neil says:

    I have been able to make the libjpeg8 !
    Thanks for your guidance.

    I did it this way :
    a) Download source (I found that lastest source to be libjpeg-8d rather than the 6d you found) …
    wget http://ftp.de.debian.org/debian/pool/main/libj/libjpeg8/libjpeg8_8d.orig.tar.gz
    b) Unpack …
    tar -xvf libjpeg8_8d.orig.tar.gz
    c) Configure …
    cd jpeg-8d
    ./configure CC=arm-linux-gnueabihf-gcc –host=arm-linux –prefix=/opt/arm-linux-gnueabihf/libjpeg
    d) Build …
    make
    make install

    Lets see if I can build and run a test prog using the lib !

  27. Neil says:

    One curious thing I have noticed. The library I have built on the Kubuntu host is vastly larger than the same version of the library install on the RPi. Is there something seriously wrong, should I have added some kind of optimisation switch to the configure command ?

    On the RPi:
    pi@raspberrypi ~ $ ls -l /usr/lib/arm-linux-gnueabihf/libjpeg*
    -rw-r–r– 1 root root 267046 Apr 23 2012 /usr/lib/arm-linux-gnueabihf/libjpeg.a
    lrwxrwxrwx 1 root root 16 Apr 23 2012 /usr/lib/arm-linux-gnueabihf/libjpeg.so -> libjpeg.so.8.4.0
    lrwxrwxrwx 1 root root 16 Apr 23 2012 /usr/lib/arm-linux-gnueabihf/libjpeg.so.8 -> libjpeg.so.8.4.0
    -rw-r–r– 1 root root 209436 Apr 23 2012 /usr/lib/arm-linux-gnueabihf/libjpeg.so.8.4.0
    pi@raspberrypi ~ $

    On Kubuntu :
    nurquhar@KubuntuVM:/opt/arm-linux-gnueabihf/libjpeg$ ls -l lib
    total 2552
    -rw-r–r– 1 nurquhar nurquhar 1567384 Jan 15 11:13 libjpeg.a
    -rwxr-xr-x 1 nurquhar nurquhar 940 Jan 15 11:13 libjpeg.la
    lrwxrwxrwx 1 nurquhar nurquhar 16 Jan 15 11:13 libjpeg.so -> libjpeg.so.8.4.0
    lrwxrwxrwx 1 nurquhar nurquhar 16 Jan 15 11:13 libjpeg.so.8 -> libjpeg.so.8.4.0
    -rwxr-xr-x 1 nurquhar nurquhar 1037464 Jan 15 11:13 libjpeg.so.8.4.0
    nurquhar@KubuntuVM:/opt/arm-linux-gnueabihf/libjpeg$

    • halherta says:

      Neil, I’m not really sure why that would be the case. Optimization might be the culprit. Basically to optimize for size you need to use the -Os flag with the arm-linux-gnueabihf-gcc….But I’m not sure exactly how I would do that from the configure file…there’s probably a way

  28. Neil says:

    Using the -Os flag makes a significant differance. Roughly 1/4 the size !
    nurquhar@KubuntuVM:/opt/arm-linux-gnueabihf/libjpeg.small/lib$ ls -l
    total 440
    -rw-r–r– 1 root root 240516 Jan 18 16:24 libjpeg.a
    -rwxr-xr-x 1 root root 946 Jan 18 16:24 libjpeg.la
    lrwxrwxrwx 1 root root 16 Jan 18 16:24 libjpeg.so -> libjpeg.so.8.4.0
    lrwxrwxrwx 1 root root 16 Jan 18 16:24 libjpeg.so.8 -> libjpeg.so.8.4.0
    -rwxr-xr-x 1 root root 201505 Jan 18 16:24 libjpeg.so.8.4.0
    nurquhar@KubuntuVM:/opt/arm-linux-gnueabihf/libjpeg.small/lib$

    I added the -Os flag like this :
    ./configure CC=arm-linux-gnueabihf-gcc CFLAGS=-Os –host=arm-linux –prefix=/opt/arm-linux-gnueabihf/libjpeg.small

    I did try building the lib on the RPi as well but the size was much the same as on the Kubuntu (without the -Os flag).

    • halherta says:

      Neil, good to hear that cross compiling with the -Os flag made such a difference on libjpeg8. Not sure why this wasn’t the case on the RPi (native compilation) though

  29. anon says:

    You do not need to sudo freaking everything. Especially editing files in your home directory. That is a bad habit to encourage.

    You also do not need to restart to make .profile changes take effect. Just type \. ~/.profile

    You also do not need to put it in both files. It tends to vary by distro. It’s .profile on Ubuntu and .bashrc on Centos.

    • halherta says:

      Anon,
      you are correct. There are a few places where i used sudo when i didn’t need to. I’ve edited this.

      When I tried adding the toolchains directory to the PATH in either the .bashrc or .profile file, Eclipse was unable to to detect the toolchain. When modifying the PATH variable in both files however it worked….still trying to understand why.

      I will try the \. ~/.profile command for reloading the .profile settings and then make an edit. Thanks.

  30. Sravan says:

    Hi thanks a lot !!! but when i run the same code i came up with these 2 errors as i am new to raspberry pi can you suggest me which are the things to be corrected for my code to run on raspberry
    http://www.flickr.com/photos/92715793@N05/8430587634/in/photostream

    • halherta says:

      Sravan,
      Are you sure that you set up the “GCC C++ linker” (Figure 11) properly ? The reason I’m asking this is that the error message seems to imply that there is a problem with the linking.

  31. chgian says:

    Excellent tutorial. Some problem that arose, were because of my mistakes. You cannot
    imagine how much did you help me. Thanks a lot

  32. Ryler says:

    Thanks a lot! It worked like a charm!
    I will post a link to your tutorial in my blog!

  33. darkladen says:

    I’m very very glad with you “Good TUTORIAL”. Is the best that I found for the web. Now I need to use qt5 and WebKit and make a little program, this prog have to show a web browser and a navbar.

    Well, Thanks a lot !!!!

    • halherta says:

      darkladen glad to you found this valuable. For QT5 one would need to compile the QT5 source code using the croos compiler. I’ve been trying to do this for sometime but with no avail. If you are successful, I’d appreciate it if you can refer me to the instructions that you followed! thanks

      • darkladen says:

        halherta, my news is I’ve been trying but always fail in this task (sorry for my english). The only one way that I found to install qt5 on pi, is by way of this repo (http://twolife.be/raspbian/). Here you found packages of qt5.

        I have tried with a lot of guides in internet, but nothing worked. I hope find some guide soon and make cross-compile can work 100% in raspbian.

        Greeting.

  34. Arne says:

    after connecting to the pi I can expand the ‘Sftp Files’ folder, but trying to expand ‘My Home’ or ‘Root’, it just displays ‘pending..’ and a window pops up asking for a password. After giving it the raspberry password the popup closes, only to open again after a couple of seconds. Nothing else happens, can’t get past this..

    • halherta says:

      Arne, is the RSE Plugin SSH setup configured correctly ? I’d say that is the culprit..Sometimes the IP address of the RPI will change if its on a DHCP enabled home network (all Home networks ought to be DHCP enabled).

  35. Stephan says:

    Hi,
    Thank you very much for the tutorial. I have the same problem than Wael.Ch.

    I make sure the file is executable (chmod +x) and I get the error “-bash: ./HelloCross: No such file or directory”

    When I recompile the code directly on the Raspi, it execute correctly. It looks like the binary is not in a good format. I load the soft float wheezy on the Raspi. Would Would it work the same on the SF and HF?

    Thanks

    • Stephan says:

      Ok, I found the solution myself. My Wheezy image was soft-float. I changed the config of the build with the appropriate tools and it worked fine now.

      • halherta says:

        Stephan sorry for the delay in replying. The toolchain being used in this tutorial is “hf” based and so you would need to have an “hf” based OS for it to work. Raspbian OS Wheezy or Arch Linux should be OK

  36. Stephan says:

    I don’t know if you can help, it is probably a newby question. I was able to cross compile the Hello World project, but I am stuck with the cross compilation of the wiringPi library from Gordon. I would like to remote debug on the Rasberry Pi and use wiringPi and i2c-dev library. When I try to compile, I am missing references. I create a new C library project and add all the source and header files of the wiringPi library. I am getting the following errors when I build the library:

    gnueabi/sysroot/usr/lib/crt1.o: In function `_start’:
    init.c:(.text+0×34): undefined reference to `main’
    wiringPiI2C.o: In function `wiringPiI2CRead’:
    /home/stephan/workspace/wiringPi/Debug/../wiringPiI2C.c:43: undefined reference to `i2c_smbus_read_byte’
    wiringPiI2C.o: In function `wiringPiI2CReadReg8′:
    /home/stephan/workspace/wiringPi/Debug/../wiringPiI2C.c:55: undefined reference to `i2c_smbus_read_byte_data’
    wiringPiI2C.o: In function `wiringPiI2CReadReg16′:
    /home/stephan/workspace/wiringPi/Debug/../wiringPiI2C.c:60: undefined reference to `i2c_smbus_read_word_data’
    wiringPiI2C.o: In function `wiringPiI2CWrite’:
    /home/stephan/workspace/wiringPi/Debug/../wiringPiI2C.c:72: undefined reference to `i2c_smbus_write_byte’
    wiringPiI2C.o: In function `wiringPiI2CWriteReg8′:
    /home/stephan/workspace/wiringPi/Debug/../wiringPiI2C.c:84: undefined reference to `i2c_smbus_write_byte_data’
    wiringPiI2C.o: In function `wiringPiI2CWriteReg16′:
    /home/stephan/workspace/wiringPi/Debug/../wiringPiI2C.c:89: undefined reference to `i2c_smbus_write_word_data’
    wiringPi.o: In function `wiringPiISR’:
    /home/stephan/workspace/wiringPi/Debug/../wiringPi.c:1144: undefined reference to `pthread_create’
    softTone.o: In function `softToneCreate’:
    /home/stephan/workspace/wiringPi/Debug/../softTone.c:115: undefined reference to `piThreadCreate’
    softServo.o: In function `softServoSetup’:
    /home/stephan/workspace/wiringPi/Debug/../softServo.c:210: undefined reference to `piThreadCreate’
    softPwm.o: In function `softPwmCreate’:
    /home/stephan/workspace/wiringPi/Debug/../softPwm.c:124: undefined reference to `piThreadCreate’

    Thanks for any help

    • halherta says:

      looks like eclipse is unable to find the pthreads library (used by the PiThreadCreate function) in the toolchain nor is it able to find the i2c-dev header required for the i2c calls.

      If you look under the …/tools / arm-bcm2708 / gcc-linaro-arm-linux-gnueabihf-raspbian / arm-linux-gnueabihf / libc / lib / arm-linux-gnueabihf/ and …/tools / arm-bcm2708 / gcc-linaro-arm-linux-gnueabihf-raspbian / arm-linux-gnueabihf / libc / usr / lib / arm-linux-gnueabihf /, you’ll find the pthreads library there. You need to include these direcctories (specifically the second one) in your library paths (fig10). In the same window in fig10 but under the “libraries: tab make sure to add “pthread”……to include this library (pthread) in the build.

      also include the following in ‘includes’ (fig 9) ../https://github.com/raspberrypi/tools/tree/master/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/arm-linux-gnueabihf/libc/usr/include this has the pthreads.h include file

      for i2c-dev.h add the following directory to your include paths (fig 9)
      …../tools / arm-bcm2708 / gcc-linaro-arm-linux-gnueabihf-raspbian / arm-linux-gnueabihf / libc / usr / include / linux

      I think this should cover it. Try it and let me know.

  37. Stephan says:

    I am getting the following errors, pretty sure it is about the I2C-DEV.

    gnueabi/sysroot/usr/lib/crt1.o: In function `_start’:
    init.c:(.text+0×34): undefined reference to `main’
    wiringPiI2C.o: In function `wiringPiI2CRead’:
    /home/stephan/workspace/wiringPi/Debug/../wiringPiI2C.c:43: undefined reference to `i2c_smbus_read_byte’
    wiringPiI2C.o: In function `wiringPiI2CReadReg8′:
    /home/stephan/workspace/wiringPi/Debug/../wiringPiI2C.c:55: undefined reference to `i2c_smbus_read_byte_data’
    wiringPiI2C.o: In function `wiringPiI2CReadReg16′:
    /home/stephan/workspace/wiringPi/Debug/../wiringPiI2C.c:60: undefined reference to `i2c_smbus_read_word_data’
    wiringPiI2C.o: In function `wiringPiI2CWrite’:
    /home/stephan/workspace/wiringPi/Debug/../wiringPiI2C.c:72: undefined reference to `i2c_smbus_write_byte’
    wiringPiI2C.o: In function `wiringPiI2CWriteReg8′:
    /home/stephan/workspace/wiringPi/Debug/../wiringPiI2C.c:84: undefined reference to `i2c_smbus_write_byte_data’
    wiringPiI2C.o: In function `wiringPiI2CWriteReg16′:
    /home/stephan/workspace/wiringPi/Debug/../wiringPiI2C.c:89: undefined reference to `i2c_smbus_write_word_data’
    softTone.o: In function `softToneCreate’:
    /home/stephan/workspace/wiringPi/Debug/../softTone.c:115: undefined reference to `piThreadCreate’
    softServo.o: In function `softServoSetup’:
    /home/stephan/workspace/wiringPi/Debug/../softServo.c:210: undefined reference to `piThreadCreate’
    softPwm.o: In function `softPwmCreate’:
    /home/stephan/workspace/wiringPi/Debug/../softPwm.c:124: undefined reference to `piThreadCreate’
    collect2: error: ld returned 1 exit status
    Build error occurred, build is stopped
    Time consumed: 19005 ms.

    The goal of cross recompiling the wiringPi library is to be able to use it in another project. The wiringPi library is compiled and installed on the Rasberry Pi. I mount a DFS folder to the wirinPi source and library and add the path to Eclipse. I copied both files libwiringPi.so.1 and libwiringPi.so.1.0 to the dfs folder.

    When I build, I am getting the following error:

    /home/stephan/raspberrypi/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin/../lib/gcc/arm-bcm2708-linux-gnueabi/4.7.1/../../../../arm-bcm2708-linux-gnueabi/bin/ld: cannot find -lwiringPi

    I am using teh soft-float wheezy image, thats why I use arm-bcm2708-linux-gnueabi tools.

    Thanks for your help.

    • halherta says:

      When creating a new project use the “CROSS GCC” option instead of the “LINUX GCC” option. This will then take you to a window where you get to specify the toolchain directory. By doing this, Eclipse will automatically know the paths of all the libraries that are available with the toolchain ( primarily libc)…(I will be modifying the instructions in the tutorial to reflect this soon). At this point under project properties you just need to specify pthread to explicitly ask the gcc to build the pthread library along with your project. Ofcourse make sure that you include all WiringPi headers/source files in the project (if you choose to organize the library in separate directories i.e. put the headers in an “include” directory, You will need to specifically let eclipse know about the path for that include directory as well.

      I was able to build an application with WiringPi (from source with no I2C stuff) using this technique (used the “hf” toolchain since I’m running the “hf” toolchain).

      For the I2C, if you basically want to use the core sysfs i2c driver as shown here, I think you (I haven’t actually tried this however) only need to include the i2c headers (#include linux/i2c.h & linux/i2c-dev.h ) into your code.

      Also check this link.

      If however you want to use the i2c_smbus_writeXX functions for doing I2C you will need to cross compile the “libi2c-dev” library and include it in your library path…I do not think that this is included with the toolchain.

    • Stephan says:

      Today, I success to include the wiringPi shared library into my cross compilie project. I just create a symbolic link called libwiringPi.so pointing to libwiringPi.so.1.

      • Valdir says:

        How I can do this? I have the same error messages.

        Thanks

        • Stephan says:

          First, I export the /Home/pi folder on the Raspberry and I mount the folder to my Kubuntu /home/stephan/pi. That way, I have access to the raspi folders directly from Kubuntu and I can refer to those folder in Eclipse.

          If you are talking about wiringPi, you need to install the library on the raspi following the instructions from Gordon’s web site. The installation of the wiringPi lib copy 2 files under /usr/local/lib, libwiringPi.so.1 and libwiringPi.so.1.0. I copied those 2 files under /home/pi/wiringPi folder. In that same folder, I created a symbolic link libwiringPi.so pointing to the file /home/pi/wiringPi/libwiringPi.so.1.

          In your project settings GCC C++ Compiler->Inculdes, I added the path /home/stephan/pi/wiringPi/wiringPi/wiringPi (NFS mounted folder from teh raspi)
          In your project settings GCC C++ Linker->Libraries I add the path /home/stephan/pi/wiringPi, (NFS mounted folder from the raspi)
          Also specified a -l wiringPi

          Not sure if it is necessary, but I also add the library path in the Eclipse settings C/C++ General ->Paths and Symbols->Library Paths the path where I created the symbolic link “/home/stephan/pi/wiringPi”

          Any time you want to use a library that is not supported by the toolchain, you need to compile it first on the Raspberry and make the files available to your Eclipse environnement. I where able to make de SQLITE3 worked the same way.

          Hope this will help.

  38. altella says:

    Thank you for the detailed tutorial, after following it step by step, I obtain the following error:
    make:***[HelloRaspberryPi.o] Error 127

    I do not have a clue of it, I have followed all the steps until figure 11.. I am very interested in being able to compile things in Eclipse, but with this error I am lost… any help?

    Thank you very much !!

    • halherta says:

      It could very well be that Eclipse is unable to find the cross compiler. You maybe also missing a few packages. Which Linux Distro are you using ? are you using 32-bit or 64-bit version?

      • altella says:

        I am using Xubuntu 12.04 64 bit version. My eclipse is 64 bit either. I have followed everything and I have obtained good results until trying to compile. In Xubuntu there is not a .profile, so the path has only been redefined in .bashrc…

        • halherta says:

          When creating a new project use the “CROSS GCC” option instead of the “LINUX GCC” option. This will then take you to a window where you get to specify the toolchain directory. By doing this, Eclipse will automatically know the paths of all the libraries that are available with the toolchain ( primarily libc)…(I will be modifying the instructions in the tutorial to reflect this soon). You will not need to do the steps shown in Fig 9 & Fig 10 since all of this stuff will be taken care of by Eclipse automatically. You also should not have any problems with eclipse recognizing your toolchain with this setup.

  39. Pingback: Problems cross compiling with Kubuntu & eclipse & gcc for the Raspberry Pi.

  40. Pingback: Cross-compiling & debugging for RaspberryPi using CMake and gdbserver/gdb | La plaga Tux

  41. Worked Like a Charm !! Thanks for this excellent guide..!

  42. Christian Fuchs says:

    Is there a possibility to configure Eclipse such that on compile, both Intel and ARM binaries are generated? Or alternatively, is it possible – via single mouse click – to select the target?
    I ask because I develop under Intel Linux and want to transfer binaries to the Raspberry only when I want to use the Raspberry for testing.

  43. Pingback: Hello, kernel! Part 2 | Raspberry Pi Kernel Development

  44. Pijus chanda says:

    Thank you for this great tutorial. But I face a problem when using includes of packages installed on the pi. In my case I have installed the mysql package on the pi. In my C++ program I need to include statements like #include “my_sql.h”. Those include files are located on the pi in the directory “\usr\include\mysql”. Naturally the cross compiler on the Ubuntu cannot find the include file as it is not in one of the specified include directories.
    How to solve this Problem? Same issue will occure when linking I guess. The libraries are on the pi and not on the Ubuntu host system.

    I tried the RSE plugin to solve this problem.
    With the eclipse RSE plugin however, I can browse through the pi’s remote file system. But including a remote path in the include directories of the project (e.g. rse://192.168.1.77/home/pi?raspberry) sort of does not work. I can see the additional include during the compile step -I../rse://192.168.1.77/home/pi?raspberry. My header file I want to include in the C++ file (e.g. test.h) is located in the home directory on the pi (/home/pi/test.h). But the compiler says, it cannot locate “test.h”

    Does anyone know how to solve this problem. I don’t want to install the mysql package on the Ubuntu. The header files might be the same but the libraries are for x86 and not for the ARM. So this won’t work. Another way would be to copy all include and libraries form the pi to the Ubuntu filesystem. But then I might have other dependency problems.

    • halherta says:

      Pijus you could create an image file of your Raspbian SD card (using dd command), mount it to your Linux VM and include the libraries/include paths into your Eclipse project. I haven’t played with this yet but many others have. Also tak a look at this link and this one

  45. pgallagher69 says:

    I’ve checked and double checked everything… I’m getting Description Resource Path Location Type
    “Program “g++” not found in PATH Preferences, C++/Build/Settings/Discovery, [CDT GCC Builtin Compiler Settings] options C/C++ Scanner Discovery Problem”…

    Any ideas?

    • halherta says:

      Pgallagher69, Eclipse interprets PATHS very differently than most other applications. Try starting eclipse from the command line with a “eclipse &” command.

      • pgallagher69 says:

        Thanks for the Super Fast reply Halherta! I ran the command, and it said eclipse wasn’t installed. So I installed it…. The command then ran the java version of eclipse annoyingly… But, I then ran the Juno c++ version again and it now builds! Brill! Thanks!

  46. WDB says:

    I hope someone can help me!!
    I’m new to the cross compile information

    I have read the “how to” cross compile on a windows machine
    here

    http://www.raspberry-projects.com/pi/programming-in-c/compilers-and-ides/eclipse/programming-the-rpi-in-windows-using-eclipse

    and manage to get it right to compile “Hello World” and copy it to the PI and run it there
    I however fail to get the debugging right but this is not the question just background information

    So i decided to try the linaro toolchain since this is the official one

    I went through this tutorial but only did the Eclipse stuff as i have a windows machine
    which is the same as the previous tutorial I did just point to a different compiler and add your library path as explained.

    here is the error i get from the previous tut “hell_world.c” with the new toolchain
    //———————————————————————————————————————————–
    10:45:28 **** Incremental Build of configuration Debug for project Raspberry_Pi_V001 ****
    make -j4 all
    Building file: ../src/Raspberry_Pi_V001.c
    Invoking: Cross GCC Compiler
    arm-linux-gnueabihf-gcc -I”C:\Cygwin\opt\cross\gcc-linaro-arm-linux-gneabihf-raspbian\arm-linux-gnueabihf\include” -I”C:\Cygwin\opt\cross\gcc-linaro-arm-linux-gneabihf-raspbian\arm-linux-gnueabihf\libc\usr\include” -I”C:\Cygwin\opt\cross\gcc-linaro-arm-linux-gneabihf-raspbian\lib\gcc\arm-linux-gnueabihf\4.7.2\include-fixed” -I”C:\Cygwin\opt\cross\gcc-linaro-arm-linux-gneabihf-raspbian\lib\gcc\arm-linux-gnueabihf\4.7.2\include” -I”C:\Cygwin\opt\cross\gcc-linaro-arm-linux-gneabihf-raspbian\lib\gcc\arm-linux-gnueabihf\4.7.2\finclude” -I”C:\Cygwin\opt\cross\x-tools\arm-unknown-linux-gnueabi\arm-unknown-linux-gnueabi\sysroot\usr\lib” -I”C:\Cygwin\opt\cross\x-tools\arm-unknown-linux-gnueabi\arm-unknown-linux-gnueabi\sysroot\usr\include” -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF”src/Raspberry_Pi_V001.d” -MT”src/Raspberry_Pi_V001.d” -o “src/Raspberry_Pi_V001.o” “../src/Raspberry_Pi_V001.c”
    cygwin warning:
    MS-DOS style path detected: C:\Users\Wouter\workspace\Raspberry_Pi_V001\Debug
    Preferred POSIX equivalent is: /cygdrive/c/Users/Wouter/workspace/Raspberry_Pi_V001/Debug
    CYGWIN environment variable option “nodosfilewarning” turns off this warning.
    src/subdir.mk:18: recipe for target `src/Raspberry_Pi_V001.o’ failed
    Consult the user’s guide for more details about POSIX paths:
    http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
    /opt/cross/gcc-linaro-arm-linux-gneabihf-raspbian/bin/arm-linux-gnueabihf-gcc: line 1: /opt/cross/gcc-linaro-arm-linux-gneabihf-raspbian/bin/arm-linux-gnueabihf-gcc-4.7.2: cannot execute binary file
    make: *** [src/Raspberry_Pi_V001.o] Error 126
    //———————————————————————————————————————————–
    then i decided to create a new “Hello_World.c” project and compile
    then i get this error

    //———————————————————————————————————————————–
    Invoking: Cross GCC Compiler
    arm-linux-gnueabihf-gcc -I”C:\Cygwin\opt\cross\gcc-linaro-arm-linux-gneabihf-raspbian\arm-linux-gnueabihf\include\” -I”C:\Cygwin\opt\cross\gcc-linaro-arm-linux-gneabihf-raspbian\arm-linux-gnueabihf\libc\usr\include” -I”C:\Cygwin\opt\cross\gcc-linaro-arm-linux-gneabihf-raspbian\lib\gcc\arm-linux-gnueabihf\4.7.2\include-fixed” -I”C:\Cygwin\opt\cross\gcc-linaro-arm-linux-gneabihf-raspbian\lib\gcc\arm-linux-gnueabihf\4.7.2\include” -I”C:\Cygwin\opt\cross\gcc-linaro-arm-linux-gneabihf-raspbian\lib\gcc\arm-linux-gnueabihf\4.7.2\finclude” -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF”src/Hello_linaro.d” -MT”src/Hello_linaro.d” -o “src/Hello_linaro.o” “../src/Hello_linaro.c”
    /bin/sh: -c: line 0: unexpected EOF while looking for matching `”‘
    src/subdir.mk:18: recipe for target `src/Hello_linaro.o’ failed
    /bin/sh: -c: line 1: syntax error: unexpected end of file
    make: *** [src/Hello_linaro.o] Error 1
    //———————————————————————————————————————————–

    • halherta says:

      WDB,
      I do not use windows ( I run only Linux (Fedora and Debian) on my PCs) anymore so I can’t try to reproduce what you are doing here. Having said that, I would try cross compiling the hello world program from the command line (DOS/cmd window) by typing something like “arm-linux-gnueabihf-gcc -Wall -o helloworld.exe helloworld.c”. If this compiles successfully, then your problem is not in the toolchain and is in eclipse. If it doesn’t then there may be a problem with the toolchain. If the problem is eclipse based you might be missing somethng in your settings.

      BTW you would need to add the toolchain’s bin directory to your PATH variable in windows in order for eclipse to detect the toolchain.

  47. Pingback: Developing for Raspberry Pi. | Cognosfera

  48. Peter says:

    Awesome, best tutorial I’ve seen, many thanks!

  49. Cristian Prado says:

    Thanks !!!!!!!!!!!!

  50. Hélio Mendonça says:

    Do you know if there is an easy way of creating a new RPi Eclipse project without having to always change the settings you mentioned (proper gcc/gdb/as programs, all the include and lib paths, etc.)? Some kind of template/wizard that could set all those things automatically.

    Regards and congratulations for all your tutorials.

    • halherta says:

      Helio, There is such a way. I learned about recently. I intend to update this tutorial with it soon. Basically when creating a new project in Figure 6, choose “cross gcc” instead of “linux gcc”. You will then be asked to locate the bin directory of your toolchain. That should be it .Eclipse will take care of everything else.

    • Valdir says:

      I aways copy and paste an existing project and change the names. I think this is the easier way.

  51. Pingback: 想问一下在linux32里面写的sdk程序怎样编译,使其可以在树莓派上面运行呢? - 树莓派 - 开发者问答

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>