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

时间:2015-04-25 18:06:44   收藏:0   阅读:427

转自别人的文章(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 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

  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.

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

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

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.

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

技术分享

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

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

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

技术分享

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

技术分享

Figure 6. C++ Project Window

技术分享

Figure 7. New Hello World Project initial setup

技术分享

Figure 8. New source File Window.

技术分享

技术分享

Figure 9.

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

技术分享

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

技术分享

Figure 14.

技术分享

Figure 15.

技术分享

Figure 16.

技术分享

Figure 17.

技术分享

Figure 18.

技术分享

Figure 19.

原文:http://www.cnblogs.com/miaohw/p/4456095.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!