rikka0w0 / buildcrossgcc.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
#! /bin/sh |
mkdir ~ /tmp |
cd ~ /tmp |
# See |
# Typical ARM triples (see «armv7l-linux-gnueabi») |
# https://bgamari.github.io/posts/2019-06-12-arm-terminology.html |
# GCC -march options |
# https://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html |
# # error «Assumed value of MB_LEN_MAX wrong» |
# https://www.linuxquestions.org/questions/linux-software-2/tar-cross-compile-error-arm-linux-gnueabi-4175642648/ |
# GCC Cross Toolchain names |
# https://blog.csdn.net/weixin_39832348/article/details/110578256 |
# Notes |
# https://askubuntu.com/questions/1235819/ubuntu-20-04-gcc-version-lower-than-gcc-7 |
# deb http://dk.archive.ubuntu.com/ubuntu/ xenial main |
# deb http://dk.archive.ubuntu.com/ubuntu/ xenial universe |
# apt install -y curl git make cmake automake binutils build-essential gcc-4.7-arm-linux-gnueabi |
# update-alternatives —install /usr/bin/arm-linux-gnueabi-gcc arm-linux-gnueabi-gcc /usr/bin/arm-linux-gnueabi-gcc-4.7 0 |
# update-alternatives —install /usr/bin/arm-linux-gnueabi-cpp arm-linux-gnueabi-cpp /usr/bin/arm-linux-gnueabi-cpp-4.7 0 |
# cross_compile_build_zlib.sh remove -mfpu= |
# file /usr/arm-linux-gnueabi/lib/libc.so.6 |
# arm-linux-gnueabi-gcc -print-file-name=libc.so |
# Download binutils, gcc, the linux kernel, glibc |
# https://gist.github.com/rikka0w0/612149263721050f69acdc0497bf9fb8 |
# https://blog.csdn.net/fickyou/article/details/51671006 |
wget http://ftpmirror.gnu.org/binutils/binutils-2.24.tar.gz |
wget http://ftpmirror.gnu.org/gcc/gcc-4.9.2/gcc-4.9.2.tar.gz |
wget —no-check-certificate https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.18.31.tar.xz |
wget http://ftpmirror.gnu.org/glibc/glibc-2.21.tar.xz |
for f in * .tar * ; do tar xf $f ; done |
mv binutils-2.24 binutils |
mv linux-3.18.31 linux |
mv gcc-4.9.2 gcc |
mv glibc-2.21 glibc |
# define the prefix |
export PREFIX=/opt/armv7l |
# change PATH to include the target directory |
export PATH= $PREFIX /bin: $PATH |
# Build binutils |
cd ~ /tmp/binutils |
./configure —prefix= $PREFIX —target=arm-linux-gnueabi —with-arch=armv7l —with-float=soft —disable-multilib —disable-werror |
make -j4 |
make install |
# Install the Linux kernel headers |
cd ~ /tmp/linux |
make ARCH=arm INSTALL_HDR_PATH= $PREFIX /arm-linux-gnueabi headers_install |
# Build gcc (first phase) |
cd ~ /tmp/gcc |
./contrib/download_prerequisites |
mkdir build ; cd build |
../configure —prefix= $PREFIX —target=arm-linux-gnueabi —enable-languages=c,c++ —with-arch=armv7 —with-float=soft —disable-multilib —disable-werror |
make -j4 all-gcc |
make install-gcc |
# Build glibc (first phase) |
cd ~ /tmp/glibc |
mkdir build ; cd build |
../configure —prefix= $PREFIX /arm-linux-gnueabihf —build= $MACHTYPE —host=arm-linux-gnueabihf —target=arm-linux-gnueabihf —with-arch=armv7l —with-fpu=vfp —with-float=hard —with-headers= $PREFIX /arm-linux-gnueabihf/include —disable-multilib libc_cv_forced_unwind=yes |
make install-bootstrap-headers=yes install-headers |
make -j4 csu/subdir_lib |
install csu/crt1.o csu/crti.o csu/crtn.o $PREFIX /arm-linux-gnueabihf/lib |
arm-linux-gnueabihf-gcc -nostdlib -nostartfiles -shared -x c /dev/null -o $PREFIX /arm-linux-gnueabihf/lib/libc.so |
touch $PREFIX /arm-linux-gnueabihf/include/gnu/stubs.h |
# Build gcc (second phase, libgcc) |
cd ~ /tmp/gcc/build |
make -j4 all-target-libgcc |
make install-target-libgcc |
# Build glibc (second phase) |
cd ~ /tmp/glibc/build |
make -j4 |
make install |
# Build libstdc++ |
cd ~ /tmp/gcc/build |
make -j4 |
make install |
How to cross compile for ARM?
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)?
2 Answers 2
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
when I tried to compile I got openssl/ossl_typ.h not found error though I already installed libssl-dev .
If you get source code from apt-get source , after apt-get build-dep you can normally just compile with dpkg-buildpackage -b -uc -us -r Is there a way to build a .deb this easily for ARM without changing the makefile?
@Maratyszcza could you please be more specific regarding the execution of line and then just use arm-linux-gnueabi-gcc instead of gcc for compilation ? When I compile I use a call to make which, AFAIK, naturally points to /usr/lib/gcc or something close to that. How could I call arm-linux-gnueabi-gcc directly?
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.c
You can try it out on this C hello world with QEMU:
sudo apt install qemu-user qemu-aarch64 main.out
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.out https://stackoverflow.com/questions/13005303/how-does-native-android-code-written-for-arm-run-on-x86/44505097#44505097
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
git clone https://github.com/raspberrypi/tools export PATH="$(pwd)/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin:$" printf '#include \nint main() < puts("hello world"); >\n' > hello_world.c printf '#include \nint main() < std::cout \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?
This builds everything from source, including the toolchain and the image, and ensures that everything is compatible.