打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
树莓派交叉编译环境搭建介绍(英文Blog转载)

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….(在宿主机上开发会更快更流畅,RPI本质上还是一个资源不足的电脑,性能远低于我们自己的桌面电脑)

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.(用GIT从GItHub上下载交叉编译工具链,实测速度慢到哭)
  • 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 typingsudo 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!!!

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
干货 | protobuf-c之嵌入式平台使用
[ 编译篇 ] 嵌入式平台 使用GoogleTest框架测试C++代码
PC上安装交叉编译器(arm-linux-gcc 4.9.3)
Getting Started with the LLVM System — LLVM 16.0.0...
查看一个可执行文件或者库
Linaro Releases
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服