树莓派-交叉编译环境搭建(Eclipse)

转自别人的文章(http://www.cnblogs.com/emouse/archive/2013/06/07/3124063.html),一些看不清楚的图片替换了一下。

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 alreadyinstalled a Raspbian Wheezy image on your RPi boardand 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 2. Choose “Other Icons” and hit “Browse” button to select “icon.xpm” file in the eclipse directory

  • 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.

  • 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

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. Remote system view

  • 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 typed “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!
时间: 2024-08-08 03:08:36

树莓派-交叉编译环境搭建(Eclipse)的相关文章

树莓派 交叉编译环境搭建(Win 7)

虽然树莓派带有gcc,可以编译C/C++程序,但是如果工程比较庞大,使用树莓派进行编译会浪费大量的时间,我们可以在PC上编译好程序,然后将可执行文件传递到树莓派上,执行程序,这样充分利用了PC上的资源,加快了开发进度. 首先需要在PC上搭建交叉编译环境,这里我选择了Cygwin作为交叉编译环境,在官网上即可下载. 安装程序Cygwin 下载arm-tool chain 下载地址:https://github.com/IanLinsdell/Raspberrypi 下载完成后解压其中的hardfp

javacard开发环境搭建 Eclipse+jcop+jcdk 配置完成的开发环境

javacard  开发环境搭建 Eclipse+jcop+jcdk  配置完成的开发环境,下载后安装jdk,并完成环境变量配置,即可开发javacard applet: 虽然有许多大神提供配置说明,但是配置很麻烦.提供一个配置好的环境给大家.如果好用,别忘了评价. 由于csdn上传限制,需要分别下载几个文件 http://download.csdn.net/detail/wyx100/7941899 http://download.csdn.net/detail/wyx100/7941669

linux 交叉编译环境搭建及nfs 文件系统挂载

交叉编译环境搭建 1. 准备好交叉编译工具链 arm-linux-gcc-4.3.2.tgz ,并放到ubuntu linux 系统中 我是通过在windows 上建立共享文件,与虚拟机中的linux 系统进行共享, 首先在windows 下创建共享文件夹(不懂请百度),然后在虚拟机上设置 虚拟机->设置->选项->共享文件夹,然后把启用勾上,并选择路径为刚才在windows 上建立的共享文件夹,之后启动ubuntu 系统,cd  /mnt/hgfc/  就可以看到有共享目录和里面的内容

嵌入式交叉编译环境搭建

1,嵌入式交叉编译环境搭建 所谓的搭建交叉编译环境,即安装.配置交叉编译工具链.在该环境下编译出嵌入式Linux系统所需的操作系统.应用程序等,然后再上传到目标机上. 交叉编译工具链是为了编译.链接.处理和调试跨平台体系结构的程序代码.对于交叉开发的工具链来说,在文件名称上加了一个前缀,用来区别本地的工具链.例如,arm-linux-表示是对arm的交叉编译工具链:arm-linux-gcc表示是使用gcc的编译器.除了体系结构相关的编译选项以外,其使用方法与Linux主机上的gcc相同,所以L

Exynos4412交叉编译环境搭建

Exynos4412交叉编译环境搭建 交叉编译:在PC机(x86平台)上开发程序,在ARM板上运行,提高开发.编译速度. 环境: Tiny4412SDK1506开发板 需要软件: arm-linux-gcc-4.5.1-v6-vfp-20120301.tgz 检测系统是否已经安装有交叉编译环境: # arm-linux-gcc -v bash: arm-linux-gcc: command not found 1. 安装步骤: 1)找到arm-linux-gcc-4.5.1-v6-vfp-201

搭建树莓派交叉编译环境

本学期选了嵌入式开发课程,所以第一步就是要准备环境.硬件环境自不必说,购买了树莓派二代.sd卡,读卡器.路由器.网线这些都是以前就有的.软件环境的搭建包括如下几步: 1.搭建虚拟机Linux环境2.烧录树莓派系统3.在虚拟机linux环境中搭建本地开发与编译环境4.在虚拟机linux上搭建交叉编译环境5.开发一段简单的程序,通过本地编译后,再进行交叉编译.之后将编译好的程序放到树莓派上执行进行测试. 具体步骤: 1.搭建虚拟机linux理论上说,在windows环境下如果也能找到满足要求的交叉编

Cocos2dx 3.0开发环境搭建--Eclipse上构建一个Android项目

一.前言: 本篇主要介绍Cocos2d-x 3.0的一些基础内容,以及在Eclipse上上编译我们的Cocos2d-x项目,成功把Helloworld运行起来了.看完本篇博客之后,你就会知道Cocos2d-x 3.0竟然发生了如此大的变化,变得如此简单,环境搭建.项目创建.编译的方式更加人性化了. 二.环境准备: 1.下载ADT Bundle(Eclipse捆绑了SDK和ADT),或者是Eclipse安装了ADT插件. 2.安装JDK.NDK.Ant .Python,然后配置环境. 3.Coco

Python1——开发环境搭建Eclipse+Pydev

本人安装环境:window7+jdk1.7+Eclipse4.3(Kepler)+Python2.7.13+Pydev jdk与eclipse的安装这里就不赘述了,从安装Python开始 一.Python安装 1.官网下载Python2.7:https://www.python.org/downloads/ 2.安装Python:双击傻瓜式安装: 3.安装完成后,将D:\python和D:\python\Scripts,添加到环境变量path下(我的安装在了D盘). 二.在Eclipse中安装p

Android开发环境搭建Eclipse+JDK+ADT+AVD(系列一)

引言 本系列适合0基础的人员,因为我就是从0开始的,此系列记录我步入Android开发的一些经验分享,望与君共勉!作为Android队伍中的一个新人的我,如果有什么不对的地方,还望不吝赐教. 在开始Android开发之旅启动之前,首先要搭建环境,然后创建一个简单的HelloWorld.本文的主题如下: 1.环境搭建 1.1.JDK安装 1.2.Eclipse安装 1.3.Android SDK安装 1.4.ADT安装 1.5.创建AVD 2.HelloWorld 1.环境搭建 1.1.JDK安装