How do I set up GCC for cross compiling for the ARM processor? The host would be on x86_64 ( AMD64 - Ubuntu 12.04 ) and the target would be ARM (Raspberry Pi as well as Pandaboard - will do separate compilations for each)?
02 Answers
Install gcc-arm-linux-gnueabi and binutils-arm-linux-gnueabi packages, and then just use arm-linux-gnueabi-gcc instead of gcc for compilation.
You need to be careful on what flavour of linux and binutils you have on your target system. The newest stuff is hardfloat, in this case you would do:
sudo apt-get install gcc-arm-linux-gnueabihfThis brings in the complete cross-compile environment, including binutils.
For using this GCC in the build process write:
CC=arm-linux-gnueabihf-gcc make 5 64-bit ARM
For 64-bit ARM, the toolchain prefix is aarch64 and usage is:
sudo apt install gcc-9-aarch64-linux-gnu
aarch64-linux-gnu-gcc -o main.out main.cYou can try it out on this C hello world with QEMU:
main.c
#include <stdio.h>
int main(void) { puts("hello");
}and then:
sudo apt install qemu-user
qemu-aarch64 main.outwill output:
helloThen a few fun things you can do to quickly see that ARM is actually running under the hood:
- GDB step debug it:
- log the executed ARM instructions with:
qemu-aarch64 -d in_asm,out_asm main.out
Tested in Ubuntu 19.10.
For reliability in serious applications, the disk image provider must also provide a compatible cross compiler
Although you can install a cross compiler with apt conveniently, I must warn you that this is not necessarily reliable unless explicitly supported by the image provider.
If you pick the cross compiler wrongly, the following may happen:
- the dynamic linker is at the wrong path:
- binary incompatibility with the glibc and any other libraries you link against:
Raspberry PI cross compilation
For RPI in particular, the provided cross compilers are available at: and can be used as explained at:
git clone
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.cppUbuntu cross compilation
If you want to cross compile for Ubuntu arm64, I have never been able to find a clear reference on which cross compilers support which distro version: What are the officially supported cross compilers for Ubuntu server alternative architectures like ARM?
Buildroot
My favorite alternative is to build your own image with Buildroot:
This builds everything from source, including the toolchain and the image, and ensures that everything is compatible.