How to install the Raspberry Pi cross compiler on my Linux host machine?

pqvst

I am attempting to get cross-compiling for Raspberry Pi working on my Ubuntu machine.

During my initial attempts I was using the arm-linux-gnueabi compiler, which is available in the Ubuntu repo. I got this working. I was able to build all my dependencies and use the cross-compiler in my cmake project.

However, I believe I should be using the hf version, so I switched to arm-linux-gnueabihf. Then I realized that this does not work with Raspberry Pi since it is armv6.

After some Googling, I then found the pre-built toolchain from GitHub.

I downloaded the toolchain, but I don't really understand how to "install" it. I extracted the files to my home directory. The directory structure looks like this:

/gcc-linearo-arm-linux-gnueabihf-raspbian
    /arm-linux-gnueabihf
        /bin
            (contains g++, gcc, etc)
        /lib
            (contains libstdc++ library)
    /bin
        (contains arm-linux-gnueabihf-g++, arm-linux-gnueabihf-...)
    /lib
        (gcc lib stuff)

If I change directory to the INNER bin folder I am able to compile a test program from the terminal without any problems.

~/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/
arm-linux-gnueabihf/bin$ g++ test.cpp -o test

I then tried to compile a test program in the OUTER bin folder, which contains the prefixed versions of the tools.

 ~/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin$ 
 arm-linux-gnueabihf-g++ test.cpp -o test

However, when I try to use the compiler now (from outside the inner bin directory), it is unable to find the libstdc++ shared library that comes with the toolchain:

arm-linux-gnueabihf-gcc: error while loading shared libraries: 
libstdc++.so.6: cannot open shared object file: No such file or directory.

Furthermore, I want to be able to use the compiler without having to navigate to the bin directory. So I tried adding the OUTER bin directory (since I want the prefixed versions) and both lib directories to my PATH:

export PATH=$PATH:~/tools/.../bin
export PATH=$PATH:~/tools/.../lib
export PATH=$PATH:~/tools/.../.../lib

However, this results in the same error. How should I "install" the toolchain so that I can use the toolchain from everywhere, just like I can when I use the cross-compilers from the Ubuntu repo?

Stenyg

I'm gonna try to write this as a tutorial for you so it becomes easy to follow.

NOTE: This tutorial only works for older raspbian images. For the newer Raspbian based on Debian Buster see the following how-to in this thread: https://stackoverflow.com/a/58559140/869402

Pre-requirements

Before you start you need to make sure the following is installed:

apt-get install git rsync cmake ia32-libs

Let's cross compile a Pie!

Start with making a folder in your home directory called raspberrypi.

Go in to this folder and pull down the ENTIRE tools folder you mentioned above:

git clone git://github.com/raspberrypi/tools.git

You wanted to use the following of the 3 ones, gcc-linaro-arm-linux-gnueabihf-raspbian, if I did not read wrong.

Go into your home directory and add:

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

to the end of the file named ~/.bashrc

Now you can either log out and log back in (i.e. restart your terminal session), or run . ~/.bashrc in your terminal to pick up the PATH addition in your current terminal session.

Now, verify that you can access the compiler arm-linux-gnueabihf-gcc -v. You should get something like this:

Using built-in specs.
COLLECT_GCC=arm-linux-gnueabihf-gcc
COLLECT_LTO_WRAPPER=/home/tudhalyas/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/../libexec/gcc/arm-linux-gnueabihf/4.7.2/lto-wrapper
Target: arm-linux-gnueabihf
Configured with: /cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.b
 uild/src/gcc-linaro-4.7-2012.08/configure --build=i686-build_pc-linux-gnu --host=i686-build_pc-
 linux-gnu --target=arm-linux-gnueabihf --prefix=/cbuild/slaves/oort61/crosstool-ng/builds/arm-l
 inux-gnueabihf-raspbian-linux/install --with-sysroot=/cbuild/slaves/oort61/crosstool-ng/builds/
 arm-linux-gnueabihf-raspbian-linux/install/arm-linux-gnueabihf/libc --enable-languages=c,c++,fo
 rtran --disable-multilib --with-arch=armv6 --with-tune=arm1176jz-s --with-fpu=vfp --with-float=
 hard --with-pkgversion='crosstool-NG linaro-1.13.1+bzr2458 - Linaro GCC 2012.08' --with-bugurl=
 https://bugs.launchpad.net/gcc-linaro --enable-__cxa_atexit --enable-libmudflap --enable-libgom
 p --enable-libssp --with-gmp=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-rasp
 bian-linux/.build/arm-linux-gnueabihf/build/static --with-mpfr=/cbuild/slaves/oort61/crosstool-
 ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-mpc
 =/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-
 gnueabihf/build/static --with-ppl=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf
 -raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-cloog=/cbuild/slaves/oort61/cros
 stool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --wi
 th-libelf=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/a
 rm-linux-gnueabihf/build/static --with-host-libstdcxx='-L/cbuild/slaves/oort61/crosstool-ng/bui
 lds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static/lib -lpwl' --ena
 ble-threads=posix --disable-libstdcxx-pch --enable-linker-build-id --enable-plugin --enable-gol
 d --with-local-prefix=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-li
 nux/install/arm-linux-gnueabihf/libc --enable-c99 --enable-long-long
Thread model: posix
gcc version 4.7.2 20120731 (prerelease) (crosstool-NG linaro-1.13.1+bzr2458 - Linaro GCC 2012.08
 )

But hey! I did that and the libs still don't work!

We're not done yet! So far, we've only done the basics.

In your raspberrypi folder, make a folder called rootfs.

Now you need to copy the entire /liband /usr directory to this newly created folder. I usually bring the rpi image up and copy it via rsync:

rsync -rl --delete-after --safe-links [email protected]:/{lib,usr} $HOME/raspberrypi/rootfs

where 192.168.1.PI is replaced by the IP of your Raspberry Pi.

Now, we need to write a cmake config file. Open ~/home/raspberrypi/pi.cmake in your favorite editor and insert the following:

SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)
SET(CMAKE_C_COMPILER $ENV{HOME}/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER $ENV{HOME}/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++)
SET(CMAKE_FIND_ROOT_PATH $ENV{HOME}/raspberrypi/rootfs)
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

Now you should be able to compile your cmake programs simply by adding this extra flag: -D CMAKE_TOOLCHAIN_FILE=$HOME/raspberrypi/pi.cmake.

Using a cmake hello world example:

git clone https://github.com/jameskbride/cmake-hello-world.git 
cd cmake-hello-world
mkdir build
cd build
cmake -D CMAKE_TOOLCHAIN_FILE=$HOME/raspberrypi/pi.cmake ../
make
scp CMakeHelloWorld [email protected]:/home/pi/
ssh [email protected] ./CMakeHelloWorld

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to link libraries on Raspberry Pi cross compiler?

From Dev

How do I prepare a Raspberry Pi with Raspbian so I can cross compile Qt5 programs from a Linux host?

From Dev

How do I prepare a Raspberry Pi with Raspbian so I can cross compile Qt5 programs from a Linux host?

From Dev

How to install ncurses on raspberry Pi?

From Dev

How to install FreeRTOS on Raspberry Pi?

From Dev

cross compiler to linux (build and host) to freebsd

From Dev

How to install Windows 10 Core on Raspberry Pi 2 without a real (physical) machine

From Dev

Cross compiling for Raspberry PI

From Dev

how to install PIL with JPEG support on a raspberry pi?

From Dev

How to install polyglot on Raspberry Pi? - problems with compatibility

From Dev

How deploy these on my raspberry pi running Apache?

From Dev

How to access my Raspberry Pi remotely?

From Dev

Raspberry pi install sailsJS

From Dev

Raspberry pi install sailsJS

From Dev

How to install cross-compiler on Ubuntu 18.04?

From Dev

How to cross-compile programs for the Raspberry Pi with gcc?

From Dev

How to cross-compile a Qt Quick application for the Raspberry Pi?

From Dev

How to cross-compile programs for the Raspberry Pi with gcc?

From Dev

Cross compiling rethinkdb for Raspberry Pi

From Dev

Cross compile a raspberry pi kernel

From Dev

Cross compiling rethinkdb for Raspberry Pi

From Dev

CMake cross compilation fails during linking stage on host (target is Raspberry Pi)

From Dev

Identifying a Raspberry Pi host with configure

From Dev

Raspberry Pi: "Cannot find host"

From Dev

How to determine host value for configure when using cross compiler

From Dev

How to install apache on a Raspberry Pi 2 Ubuntu Snappy Core?

From Dev

How can I install Ubuntu with APT (not Snappy) on the Raspberry Pi 2?

From Dev

How to host a website from my local machine?

From Dev

How to host a website from my local machine?

Related Related

  1. 1

    How to link libraries on Raspberry Pi cross compiler?

  2. 2

    How do I prepare a Raspberry Pi with Raspbian so I can cross compile Qt5 programs from a Linux host?

  3. 3

    How do I prepare a Raspberry Pi with Raspbian so I can cross compile Qt5 programs from a Linux host?

  4. 4

    How to install ncurses on raspberry Pi?

  5. 5

    How to install FreeRTOS on Raspberry Pi?

  6. 6

    cross compiler to linux (build and host) to freebsd

  7. 7

    How to install Windows 10 Core on Raspberry Pi 2 without a real (physical) machine

  8. 8

    Cross compiling for Raspberry PI

  9. 9

    how to install PIL with JPEG support on a raspberry pi?

  10. 10

    How to install polyglot on Raspberry Pi? - problems with compatibility

  11. 11

    How deploy these on my raspberry pi running Apache?

  12. 12

    How to access my Raspberry Pi remotely?

  13. 13

    Raspberry pi install sailsJS

  14. 14

    Raspberry pi install sailsJS

  15. 15

    How to install cross-compiler on Ubuntu 18.04?

  16. 16

    How to cross-compile programs for the Raspberry Pi with gcc?

  17. 17

    How to cross-compile a Qt Quick application for the Raspberry Pi?

  18. 18

    How to cross-compile programs for the Raspberry Pi with gcc?

  19. 19

    Cross compiling rethinkdb for Raspberry Pi

  20. 20

    Cross compile a raspberry pi kernel

  21. 21

    Cross compiling rethinkdb for Raspberry Pi

  22. 22

    CMake cross compilation fails during linking stage on host (target is Raspberry Pi)

  23. 23

    Identifying a Raspberry Pi host with configure

  24. 24

    Raspberry Pi: "Cannot find host"

  25. 25

    How to determine host value for configure when using cross compiler

  26. 26

    How to install apache on a Raspberry Pi 2 Ubuntu Snappy Core?

  27. 27

    How can I install Ubuntu with APT (not Snappy) on the Raspberry Pi 2?

  28. 28

    How to host a website from my local machine?

  29. 29

    How to host a website from my local machine?

HotTag

Archive