1. 程式人生 > >How to build a cross compiler for your Raspberry Pi

How to build a cross compiler for your Raspberry Pi

Step 1. Build the Toolchain

Since we are going to run in laptop with an Intel processor, and we want to build object code for the ARM processor at the heart of the Raspberry Pi, we need a cross-compiler and its associated tools, which is usually called a "toolchain". Here we are using "crosstool-ng

" to build such tool chain.

Step 1.1 Download crosstool-ng

and download the most recent version, that at the time of writing this blog was:  1.17.0
note that in the download page, the version numbers are sorted alphabetically (not numerically).
In my first visit, I went straight to the bottom of the page and erroneously grabbed version 1.9.3, 
just because it was at the bottom of the page...

This link below, with the downloads sorted by date, might be useful to you:

The file 00-LATEST-is-1.17.0 should have also be a hint... if I were paying attention...  :-)

We created a directory to host it and then downloaded and extracted the sources by doing:

  • mkdir -p  ~/src/RaspberryPi/toolchain
  • cd ~/src/RaspberryPi/toolchain
  • wget http://crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.17.0.tar.bz2
  • tar xjf crosstool-ng-1.17.0.tar.bz2
  • cd crosstool-ng-1.17.0

Step 1.2 Configure and Build

We chose to configure the tool to be installed in a local directory inside our home directory.

  • cd ~/src/RaspberryPi/toolchain/crosstool-ng-1.17.0
  • mkdir -p ~/local/crosstool-ng
  • ./configure --prefix=/home/ibanez/local/crosstool-ng

to get this to work, we had to install the following Ubuntu packages (most of which were listed in Andrew's recipe),

  • bison
  • cvs
  • flex
  • gperf
  • texinfo
  • automake
  • libtool

The whole is done with the command:

  • sudo aptitude install bison cvs flex gperf texinfo automake libtool 

then we can do

  • make
  • make install

and add to the PATH the bin directory where crosstool-ng was installed:

  • export PATH=$PATH:/home/ibanez/local/crosstool-ng/bin/

In some cases, it might be necessary to unset the LD_LIBRARY_PATH, 
to prevent the toolchain from grabbing other shared libraries from the host machine:

  • unset LD_LIBRARY_PATH

Step 1.3 Build Raspberry Toolchain

Create a staging directory. This is a temporary directory where the toolchain will be configured and built, but it is not its final installation place.

  • mkdir -p ~/src/RaspberryPi/staging
  • cd ~/src/RaspberryPi/staging/
  • ct-ng  menuconfig

You will see a menu similar to:

  • Go into the option "Paths and misc options"
  • Enable the option "Try features marked as EXPERIMENTAL"

  • In the option "Prefix Directory (NEW)", one can set the actual destination directory where the toolchain will be installed.

  • In this case we choose to install in ${HOME}/local/x-tools/${CT_TARGET}.
    Others may prefer /opt/cross/x-tools/${CT_TARGET}, for example.

  • After you select < Ok >
  • Select the < Exit > option to go back to the main menu
  • There, select "Target options".

  • Change the Target architecture to arm.

  • Leave Endianness set to Little endian and 
  • Bitness set to 32-bit.

  • Use again the < Exit > option to go back to the main menu
  • Select "Operating System"

  • There, change the "Target OS" option from (bare-metal)

  • to the option "linux"

  • Take the <Select> option
  • Use the < Exit > option to get back to the main menu
  • Select "Binary utilities"

  • Select "binutils version"

  • Take the most recent version that is not marked as EXPERIMENTAL.
    In our case, that was version 2.21.1a

  • Go back to the main menu
  • Select "C compiler"

  • Enable the Show Linaro versions (EXPERIMENTAL) option.

  • Here we selected the "linaro-4.7-2012.10 (EXPERIMENTAL) "
    This is a bit newer than the version "linaro-4.6-2012.04 (EXPERIMENTAL)" 
    that Chris Boot was using in his blog post, so here we are taking our chances...

  • Select that option.
  • Exist the configuration and
  • Save the changes

Note contributed by Scott Determan:

If you want to cross-compile code that used C++11 futures/promises
then gcc needs to be build with the flag

                   --with-arch=armv6.

To do this, use the command

            ct-ng menuconfig,

and go to the item:

       Target options -> Architecture level

and set it to "armv6" (without quotes),

then you will be able to cross compile code that uses futures/promises.

Thanks Scott !

 Then, start the build process by typing

  • ct-ng  build

  • It was nice to see that the build projects uses the proper "make -j " options for parallel building 
    and therefore makes use of all the available cores:

Not to be a whiner.... but,... 
the problem with this, 
is that it only gives us 18minutes and 9 seconds for the Coffee break   :-)

  • When the build process finishes, we end up with the toolchain installed in the "prefix" directory.
    In our case: ${HOME}/local/x-tools/${CT_TARGET}
  • More specifically: 
    /home/ibanez/local/x-tools/arm-unknown-linux-gnueabi
  • Where we will find the following collection of executables in the "bin" directory:

  • We now add this directory to the PATH:
    export PATH=$PATH:/home/ibanez/local/x-tools/arm-unknown-linux-gnueabi/bin
  • We can then test the toolchain with a "hello world" small C program.
  • Compiling it locally as "helloworld" (in "aleph" which is the name of our Ubuntu Laptop).
  • Then copying it into the Raspberry Pi
  • and finally running it there

Step 1.4 Build the C++ compiler in the toolchain

By default, our process above only built the C compiler.

We are now going to build the C++ compiler as well.

To build the C++ compiler we do the following:

  • We go back to the staging directory:
    /home/ibanez/src/RaspberryPi/staging


  • and run the configuration process
    ct-ng menuconfig

  • We go into the "C compiler" option

  • Enable the option "C++"

  • Save and Exit
  • and type again
       "ct-ng  build"
    to build the toolchain.

This time it took 13 minutes 18 seconds

and we have now the new C++ components in the toolchain binary directory

 Time to test the C++ compiler with a Hello World.

  • We build it locally
  • Copy the executable to the Raspberry Pi
  • Login in the Raspberry Pi
  • Execute the cross-compiled executable

This completes the set up of the tool chain.

We are now ready to use CMake to cross compile bigger projects.

Step 2. One CMake File to Rule Them All !

We now turn our attention to the Cross Compilation instructions of the CMake Wiki

The first step here is to write a .cmake file that points to the toolchain.

In our case we choose to call this file:

  • Toolchain-RaspberryPi.cmake

and put on it the following content

# this one is important
SET(CMAKE_SYSTEM_NAME Linux)
#this one not so much
SET(CMAKE_SYSTEM_VERSION 1)

# specify the cross compiler
SET(CMAKE_C_COMPILER
/home/ibanez/local/x-tools/arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi-gcc)

SET(CMAKE_CXX_COMPILER
/home/ibanez/local/x-tools/arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi-g++)

# where is the target environment
SET(CMAKE_FIND_ROOT_PATH
/home/ibanez/local/x-tools/arm-unknown-linux-gnueabi)

# search for programs in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

Note here that the path

  • /home/ibanez/local/x-tools/arm-unknown-linux-gnueabi

is the base directory where we installed the toolchain.

and the file names

  • arm-unknown-linux-gnueabi-gcc
  • arm-unknown-linux-gnueabi-g++

are the names of the generated C and C++ compilers respectively.

We now put the cmake toolchain file in the directory:

  • /home/ibanez/bin/RaspberryPi/CMakeToolChain

Then we create a CMake-based Hello World example:

  • mkdir -p /tmp/hello/src
  • mkdir -p /tmp/hello/bin
  • cd /tmp/hello/src

write here a CMakeLists.txt file with just:

cmake_minimum_required(VERSION 2.8)
project(HelloWorld)
add_executable(HelloWorld HelloWorld.cxx )
target_link_libraries(HelloWorld)


and the associated HelloWorld.cxx file with:

#include <iostream>
int main() {
  std::cout << "Hello World++ !" << std::endl;
  return 0;
  }

Then, we can change directories to the bin directory and configure with CMake, by pointing to the toolchain file as:

  • cd /tmp/hello/bin
  • cmake -DCMAKE_TOOLCHAIN_FILE=/home/ibanez/bin/RaspberryPi/CMakeToolChain/Toolchain-RaspberryPi.cmake ../src
  • and simply build with "make"
  • Then copy the resulting executable to the Raspberry Pi, and run it.

Step 3. Setting up additional bin and lib files for cross compiling.

If you need access to the libraries on the RaspberryPi for compiling
(for example if you have built the latest boost libriaries on the RaspberryPi 
and it is installed in /usr/local), you can copy these to a directory on your 
host computer using rsync that will preserve the symlinks.
  • On your host machine you may also have to install rsync
    •  sudo aptitude install rsync
  • On the RaspberryPi install rsync:
    •  sudo apt-get istall rsync
  • Create a folder on the cross compiling machine. For example, here we call it: ~/bin/RaspberryPi
    • mkdir -p ~/bin/RaspberryPi
  • cd to this folder
    • cd  ~/bin/RaspberryPi
  • and do the following:

Remember to run these rsync commands whenever new libraries are added to the RaspberryPi system or when the RaspberryPi is upgraded.

相關推薦

How to build a cross compiler for your Raspberry Pi

Step 1. Build the Toolchain Since we are going to run in laptop with an Intel processor, and we want to build object code for the ARM p

How to Choose a Blockchain Platform for Your Business

How to Choose a Blockchain Platform for Your BusinessThe growing popularity of crypto investments has aroused a keen interest in blockchain technologies an

How to build a case for a product redesign

The overarching theme that emerged was that our product’s information hierarchy was unclear, which I pitched to stakeholders as the primary problem we shou

How to build a Deep Learning Image Classifier for Game of Thrones dragons

Performance of most flavors of the old generations of learning algorithms will plateau. Deep learning, training large neural networks, is scalable and perf

Machine Learning: How to Build a Model From Scratch

As an online travel booking company, Momentum Travel realized early on that identifying and preventing fraud is a vital part of their business. Hear from S

How to build a usability lab anytime, anywhere

How to build a usability lab anytime, anywhereKeeping the fundamentals of a usability test in a remote location.Have you ever seen or conducted a conventio

Ask HN: How to build beautiful SVG graphics for websites?

I am really curious if anyone knows about courses / tutorials or other material I could use to learn how to do graphics such as the ones at:a) www.stripe.c

How to build a project inside a Docker container

I wanted to participate in the Hacktoberfest 2018, but one thing kept worrying me. I had to download someone’s code from the Internet and run it on my mach

How To Build A Money Data Type In JavaScript

Last time I wrote a step-by-step example of how to apply Inside Out Test-Driven Development to a problem using JavaScript. That post used the Number type t

Useful hints to build a perfect design for iPhone Xs

Apple presents new gadgets every year, and each of this device deserves the attention. But when iPhone X was presented to the public, rules of app designin

How to build a simple Vue CLI plugin

How to build a simple Vue CLI pluginIf you’re using Vue framework, you probably already know what Vue CLI is. It’s a full system for rapid Vue.js developme

How to create role based accounts for your Saas App using FEAN? (Part 1)

Setup firebase in your angular app and express js// Front-endng new exampleAppcd exampleApp && cd exampleApp// For adding firebase to angular appng

How to Build A.I. We Can Relate to

The singularity, to the extent that any emerges — unevenly distributed — will be intentional. If it is to give us what we most deeply yearn for, and not wh

Ask HN: How to build a Game Center on recurring revenue?

My company sells gaming desktop computers for about 10 years. We accomplished a well know website, social media followers and customer base, but business i

Parrots really know how to build a nest egg

A total of 33 parrots of four different species completed this study. The study participants included eight African grey parrots, Psittacus erithacus, and

How to choose the best channel for your chatbot

Use of cookies: We our own and third-party cookies to personalise our services and collect statistical information. If you continue browsing the site, you

How to build a front-line concussion monitoring system using AWS IoT and serverless data lakes

In part 1 of this series, we demonstrated how to build a data pipeline in support of a data lake. We used key AWS services such as Amazon Kinesis

Assessing Annotator Disagreements in Python to Build a Robust Dataset for Machine Learning

Assessing Annotator Disagreements in Python to Build a Robust Dataset for Machine LearningTea vs. Coffee: the perfect example of decisions and disagreement

系統技術非業餘研究 » How to Build a Debug Enabled Erlang RunTime System

很多朋友在問如何除錯Erlang的驅動程式碼等等,其實otp原始碼下的INSTALL.md寫的很清楚, 摘抄下: How to Build a Debug Enabled Erlang RunTime System ————————————————– After completing all th

How to Buy Cloud Computing Services for Your Agency

Amazon Web Services is Hiring. Amazon Web Services (AWS) is a dynamic, growing business unit within Amazon.com. We are currently hiring So