you can use arm-linux-gnueabihf-gcc hello.c -o hello command. And before you should install arm-linux-gnueabihf-gcc in your linux system .
here is full sample that work for me
Answer from reza rezaee on Stack OverflowWhich gcc is safe for armv7l?
c++ - How to compile a gcc compiler for arm on a X86 PC - Stack Overflow
c++ - GCC 8 Cross Compiler outputs ARMv7 executable instead of ARMv6 - Stack Overflow
Cross-compile on Arch Linux host x86_64 for ARM ⇒ Docker image with Rust?
On the topic of cross-compiling from x86_64 to ARM, it doesn't really require much and Docker may be overkill, depending on your goal.
Have you checked the RasPi community for a distro-agnostic cross-compiler tarball?
While my OpenPandora isn't a RasPi, I was able to cross-compile pure Rust code simply by unpacking such a tarball (no Docker needed), identifying the LLVM equivalent to that GCC's target triple (arm-unknown-linux-gnueabi), and then running this command...
rustup target add arm-unknown-linux-gnueabi
...and adding this to ~/.cargo/config:
[target.arm-unknown-linux-gnueabi] linker = "/home/ssokolow/opt/pandora-dev/arm-2011.09/bin/pandora-gcc"More on reddit.com
Hello everyone.
I run gentoo for development and use the crossdev tools to generate an arm cross compiler.
I'm having problems finding a version of gcc that behaves when compiling code for armv7l. (gnueabihf).
11.2.X seems to have pie problem (I don't get what pie is, I mean I do conceptually but I don't understand why it is suddenly enabled by default)
I was using 10.5, but gnueabihf wasn't working so I dropped the hf and it worked, but shouldn't it work with Cyclone V?
And I'm confused about how the arch is specified.
Right now I'm using arm-linux-gnueabi, but I wonder if I should be using arm-none-linux-gnueabihf...
Can you guys suggest a known working version of the cross arm tools.
ARM seems to recommend 7.5 and that is way too old for modern linux.
It is a hard but doable task.
It depends on what ARM series you are compiling against. On many systems you find a Cortex-A (armv7-a or higher) or ARM11 (armv6) processor running a "mini Linux distribution", this is the case for many POS (Point of Sale) devices. In such a case your target can be one of the following:
- arm-linux-gnueabi
- arm-linux-gnueabihf
Where hf stands for hard-float, meaning that the CPU has a FPU (floating-point unit), all Cortex-A support this, but also depends on the underlying OS. It is also very important to know the version of the glibc on the embedded Linux, because if versions are not the same between compiler and OS, unexpected behavior may occur.
If you are compiling against an ARM processor without MMU (Memory Magament Unit) like the Cortex-R or Cortex-M series, and that by consequence do not support Linux (only microkernels like FreeRTOS) your target would be (also known as bare-metal):
- arm-none-eabi
ARM now distributes the binaries of GCC:
https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-a/downloads
Previously it was Linaro. Yet they keep GCC 7.x for arm-linu-gnueabi (soft-float) if you need it.
https://releases.linaro.org/components/toolchain/binaries/latest-7/
Linaro also used a build system known as ABE, I have not found much documentation on it, but they used it to configure and build the toolchains distributed by them.
If none of the above works for you, you can still build your own toolchain, for that task I suggest using crosstool-ng: https://crosstool-ng.github.io/
Works best under a Linux OS like Ubuntu (you can still try to use Cygwin to build it on Windows, but it may take more time). Note that you do not need to compile it in the machine you want to run it, meaning that you can compile on Ubuntu the GCC that will run on Windows which will produce programs that run on ARM processor, this is known as Canadian Cross.
I recommend getting a cross-compiler binary. On a Linux distribution it is often already packaged.
However, if you insist on building GCC from source (but beware of dependencies), read about Installing GCC and about Configuring GCC. You should build it outside of the source tree, and you want to pass some particular --target= option. I also recommend something like --program-suffix=_my
(I have no idea what --target is relevant for your particular configuration & board; you need to find out)
Notice that a target is not only a CPU, but also an OS....
I ended up compiling GCC from source, following this post. I didn't need all of the steps (I compiled everything using GCC 8 instead of compiling GCC 6.3 first, and I didn't edit any source files.)
I posted a Dockerfile with all build steps on GitHub.
The architecture of the executables produced is correct now, but I can't test it on target yet to check if it actually runs.
By default, newer GCC versions do not create correct binaries for ARMv6. Even though you pass the correct -mcpu= flag to gcc, it will create startup code for the newer ARMv7 architecture. Running them on your RasPI Zero will cause an "Illegal Instruction" exception.
this info is mentioned here https://github.com/Pro/raspi-toolchain
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-gnueabihf
This brings in the complete cross-compile environment, including binutils.
For using this GCC in the build process write:
CC=arm-linux-gnueabihf-gcc make
64-bit ARM
For 64-bit ARM, the toolchain prefix is aarch64 and usage is:
sudo apt install gcc-aarch64-linux-gnu
aarch64-linux-gnu-gcc -o main.out main.c
You 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.out
will output:
hello
Then a few fun things you can do to quickly see that ARM is actually running under the hood:
- GDB step debug it: https://stackoverflow.com/questions/20590155/how-to-single-step-arm-assembly-in-gdb-on-qemu/51310791#51310791
- log the executed ARM instructions with:
qemu-aarch64 -d in_asm,out_asm main.outhttps://stackoverflow.com/questions/13005303/how-does-native-android-code-written-for-arm-run-on-x86/44505097#44505097
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: https://stackoverflow.com/questions/31929092/trying-to-run-a-cross-compiled-executable-on-target-device-fails-with-no-such-f/49993116#49993116
- binary incompatibility with the glibc and any other libraries you link against: https://stackoverflow.com/questions/11107263/how-compatible-are-different-versions-of-glibc
Raspberry PI cross compilation
For RPI in particular, the provided cross compilers are available at: https://github.com/raspberrypi/tools and can be used as explained at: https://raspberrypi.stackexchange.com/questions/64273/installing-raspberry-pi-cross-compiler/83215#83215
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
Ubuntu 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: https://stackoverflow.com/questions/47557262/how-to-download-the-torvalds-linux-kernel-master-recompile-it-and-boot-it-wi/49349237#49349237
This builds everything from source, including the toolchain and the image, and ensures that everything is compatible.