9

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: https://github.com/raspberrypi/tools.

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?

1
  • 1
    "arm-linux-gnueabi compiler, which is available in the Ubuntu repo" -> Most distro cross-compilers are ARMv7, so not compatible with Raspbian. "so I switched to arm-linux-gnueabihf. Then I realized that this does not work with Raspberry Pi since it is armv6" -> You haven't said which model or target OS you want. Raspbian is compiled for ARMv6, more specifically a variant with hardware float support. So there is a good chance that is the right one.
    – goldilocks
    Apr 2, 2017 at 13:26

4 Answers 4

6

The primary target of the Ubuntu arm-linux-gnueabi cross compiler is to compile for a different architecture of the same Ubuntu release leveraging the Debian/Ubuntu multiarch capabilities. Example: You compile on Ubuntu 16.04 amd64 for Ubuntu 16.04 armel.

If you would like to do a similar thing for Raspbian I suggest the following approach:

  • On your Ubuntu host install Debian Jessie amd64 within a chroot or a LXC container.
  • Enable the foreign architecture armhf.
  • Install the cross compiler from the emdebian tools repository.
  • Tweak the cross compiler (it would generate code for ARMv7-A by default) by writing a custom gcc specs file.
  • Install armhf libraries (libstdc++ etc.) from the Raspbian repository.
  • Build your source code.

Since this is a lot of work I have automated the above setup. You can read about it here: http://www.get-edi.io/Cross-Compiling-for-Raspbian/

2
  • This answer should have a disclosure that the referenced automated setup installs an LXC configured as "privileged". According to current LXC security policy: "[privileged containers] can be thought as old-style containers, they're not safe at all and should only be used in environments where unprivileged containers aren't available and where you would trust your container's user with root access to the host .... LXC upstream's position is that those containers aren't and cannot be root-safe." Mar 16, 2019 at 16:46
  • @CraigHicks: There is no reason to be worried: The container user is the same as the host user. Furthermore the host user is typically a development user that has root access anyway. The container is switched into a privileged container if and only if you share a folder between the container and the host system. Sharing folders is a lot more cumbersome without privileged containers. If you would like to run multiple cross compilation containers for different users on a server then just do not share folders and you will automatically get unprivileged containers. Mar 17, 2019 at 21:32
6

I cannot reproduce the problem anymore

As of https://github.com/raspberrypi/tools/tree/5caa7046982f0539cf5380f94da04b31129ed521 it just works no matter which directory I'm on, just adding to PATH is enough:

git clone https://github.com/raspberrypi/tools
export PATH="$(pwd)/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin:${PATH}"
printf '#include <stdio.h>\nint main() { puts("hello world"); }\n' > hello_world.c
printf '#include <iostream>\nint main() { std::cout << "hello world" << std::endl; }\n' > hello_world.cpp
arm-linux-gnueabihf-gcc -std=c99 -o hello_world_c hello_world.c
arm-linux-gnueabihf-g++ -std=c++11 -o hello_world_cpp hello_world.cpp

Tested in Ubuntu 17.10.

The official documentation also documents that this should work: https://www.raspberrypi.org/documentation/linux/kernel/building.md (GitHub)

Related: https://stackoverflow.com/questions/19162072/how-to-install-the-raspberry-pi-cross-compiler-on-my-linux-host-machine

1

You can try my Latest Pre-Built Open-Sourced GCC Toolchains for Raspberry Pi in this Sourceforge Project:

Latest Set of Pre-Built GCC Cross & Native Compiler Binaries for Raspberry Pi - v2.5

Project Summary: This project contains the UpToDate set of Precompiled/Pre-Built Raspberry pi GCC Cross & Native Compilers Binaries, saving your tons of time(No compiling or Error Handling needed whatsoever). Just Extract, Link & Enjoy complete GCC(Raspberry Pi) functionality in your Machine. You can use its native compilers for Raspberry Pi(Can be used along with old & slow 6.3.0 GCC), Or use the Cross-Compiler in any Linux Machine(Tested on Latest Ubuntu/bionic x64) to compile programs for your Raspberry Pi. All these compilers binaries are Raspberry Pi hardware optimized for enhanced overall performance.

Supported GCC Versions:

  • GCC 6.3.0
  • GCC 7.4.0
  • GCC 8.2.0
  • GCC 8.3.0

Supported Raspberry Pis:

  • All Raspberry Pi versions/models are currently supported.
  • Any other ARM Devices with similar Hardware configurations may also work.

Supported Environments:

  • Cross-Compiler: All Linux Distros (x32/x64) are currently supported.
  • Native-Compiler: All Raspberry Pi version/model with Raspbian OS is supported. Other OS may/may-not work.

Supported Languages:

  • C++
  • C
  • Fortran
8
  • Apparently, "this active github project" is in fact your personal project, and you forgot to disclose that. Furthermore, your whole answer is a verbatim copy of stackoverflow.com/a/52138329/4350586 Mar 7, 2019 at 10:31
  • @DmitryGrigoryev you are saying I have to promote this as my Personal Project instead of Active Project. Also, Do I have to write different wording for the same two answers? Also please tell, how that violates the Terms of Services of this website. Kindly enlighten me?
    – abhiTronix
    Mar 8, 2019 at 6:07
  • Check out this FAQ page, specifically the second paragraph from the bottom. I hope that will enlighten you somehow, if not, you may also want to see what our code of conduct says about sarcasm. Mar 8, 2019 at 7:39
  • Hello and welcome, AbhiTronix. Per our FAQ: "you must disclose your affiliation in your answers." and beyond that I use this answer on MSE as a rule of thumb. That being said, I really don't see what you'd be losing in declaring your affiliation with said project.
    – Ghanima
    Mar 8, 2019 at 8:15
  • As for the other issue I have to say that there is technically no such thing as a cross-site duplicate. It is however strongly discouraged. If there is no additional value in the question being on two sites, flag the question on the site where it is less on-topic, using the custom description, and explain that it is a cross-post, and should be removed. source.
    – Ghanima
    Mar 8, 2019 at 8:16
0

I created a project on Github which contains build files to build your own toolchain with correction versions. Especially the --with-arch=armv6 flag is important.

The corresponding project can be found here: https://github.com/Pro/raspi-toolchain

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.