To cross-compile a kernel for AArch64 on Ubuntu, you need to install gcc-aarch64-linux-gnu, and run the build as

make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- ...

Thus

make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig

to configure, and

make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- all

to build the kernel and modules.

If you can’t find an option you’re expecting, use / in menuconfig to look for it; that will tell you what else needs to be enabled.

(These are the same instructions as for Debian; the core cross-compiler packages are named the same in Debian and Ubuntu nowadays.)

Answer from Stephen Kitt on Stack Exchange
Top answer
1 of 2
10

To cross-compile a kernel for AArch64 on Ubuntu, you need to install gcc-aarch64-linux-gnu, and run the build as

make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- ...

Thus

make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig

to configure, and

make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- all

to build the kernel and modules.

If you can’t find an option you’re expecting, use / in menuconfig to look for it; that will tell you what else needs to be enabled.

(These are the same instructions as for Debian; the core cross-compiler packages are named the same in Debian and Ubuntu nowadays.)

2 of 2
0

Want to add to @Stephen Kitt answer's and emphasize that the settings of the ARCH and CROSS_COMPILE variables (but mainly the ARCH) is curical EVEN before the configuration phase.

In this example, I would assume the OTHER WAY AROUND (i.e. corss compile Linux kernel from Mac which is aarch64 to Intel which is x86_64), but the idea is the same and relevant also in your "cross compilation dicrection":

Let's say your target machine's kernel config has this setting:

CONFIG_DRM_I915=y
CONFIG_DRM_I915_GVT=y

Without ARCH=x86_64 set, what happens (during kernel configuration phase on the host machine which is aarch64):

  1. Kernel sees host ARCH = aarch64
  2. Checks if CONFIG_DRM_I915 is valid for aarch64
  3. Intel graphics driver is x86_64-only
  4. CONFIG_DRM_I915=y gets SILENTLY DROPPED

(see how this driver's compilation/configuration is dependent on the X86 compilation flag here)

  1. Your .config (in the kernel's source folder on your host machine) loses Intel graphics support.

With ARCH=x86_64 set the relevant configuration for the CONFIG_DRM_I915 will be set correctly as you probably desired.

🌐
PINE64
wiki.pine64.org › wiki › Cross-compiling
Cross-compiling - PINE64
April 2, 2023 - $ rustc --target=aarch64-unknown-linux-gnu -C linker=aarch64-linux-gnu-gcc main.rs ... $ scp main user@ipadress:/home/user/Downloads $ ssh user@ipadress /home/user/Downloads/main Hello, world! To cross-compile a project with cargo, open the folder of the project in a terminal.
Discussions

linux - What am I supposed to specify for "--host" to cross compile gcc from x86_64 for aarch64? - Stack Overflow
I want to build gcc as mentioned here: https://gcc.gnu.org/install/ for my machine. The build machine is: Ubuntu 22.04 x86_64 Intel i7 I successfully built gcc 13.1.0 on this machine. Next I want... More on stackoverflow.com
🌐 stackoverflow.com
Cross compile for aarch64 on Ubuntu
I’ve got the tool chain file fairly well figured out. The problem I run into is that the sysroot is at /usr/aarch64-linux-gnu, while the arm64 libs and includes are in /usr/lib/aarch64-linux-gnu and /usr/include/aarch64-linux-gnu, where CMake’s find_package doesn’t find them. More on discourse.cmake.org
🌐 discourse.cmake.org
1
1
November 12, 2020
build kernel with aarch64-linux-gnu-gcc - Stack Overflow
I am trying to build the kernel source on my platform with the following command: make ARCH=arm CROSS_COMPILE=aarch64-linux-gnu- generic-v8-defconfig However, the error messages appear as shown in... More on stackoverflow.com
🌐 stackoverflow.com
Linux cross-compile x64 to aarch64
Unless anyone can convince me of otherwise, I’ve given up on cmake being able to cross-compile on Linux from x64 to aarch64. I started this topic about it earlier on a C group: https://groups.google.com/forum/#!topic/c… More on discourse.cmake.org
🌐 discourse.cmake.org
0
0
August 26, 2020
🌐
Reddit
reddit.com › r/cpp_questions › what are the arch and cross_compile options for the make command?
r/cpp_questions on Reddit: What are the ARCH and CROSS_COMPILE options for the make command?
April 19, 2024 -

I'm interested in cross-compilation these days, and encountered this answer:

https://unix.stackexchange.com/a/657884

It uses a command like the following:

make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig

I'm trying to find what other options I have for `ARCH` and `CROSS_COMPILE` (as enum values maybe). But `make --help` doesn't show them. I don't think they are environment variables as they should appear before make. So what are they and where can I find more docs about them?

🌐
Jensd's I/O buffer
jensd.be › 1126 › linux › cross-compiling-for-arm-or-aarch64-on-debian-or-ubuntu
Cross compiling for arm or aarch64 on Debian or Ubuntu | Jensd's I/O buffer
January 26, 2021 - To find out for which of these ... for ARM. These include the standard tools needed for compiling native: jensd@deb10:~$ sudo apt install gcc make gcc-arm-linux-gnueabi ......
🌐
GitHub
github.com › uablrek › cross-compile
GitHub - uablrek/cross-compile: Howto cross-compile on Linux for arm64 (aarch64) · GitHub
The Linux kernel is simple to cross compile. Always use the O= option to kernel make to keep source and objects separated. This allows the same source tree to be used for different builds.
Author   uablrek
🌐
GitHub
github.com › apritzel › cross
GitHub - apritzel/cross: GCC Crosscompiler build scripts and instructions · GitHub
Example configure options and make targets for a cross aarch64 compiler with /usr/gnemul/aarch64 as sysroot, assuming a non-multiarch build: $ ../binutils-gdb/configure --prefix=/usr --with-gnu-ld --with-gnu-as \ --target=aarch64-linux-gnu \ --build=x86_64-linux-gnu --host=x86_64-linux-gnu \ --with-sysroot=/usr/gnemul/aarch64 \ --disable-bootstrap \ --disable-shared --disable-nls --enable-multilib \ --enable-threads \ --enable-plugins \ --enable-gold=yes --enable-ld=default \ --with-lib-path=/usr/aarch64-linux-gnu/lib64:/usr/gnemul/aarch64/usr/local/lib64:/usr/gnemul/aarch64/lib64:/usr/gnemul/aarch64/usr/lib64 $ make && make DESTDIR=$(pwd)/root install
Starred by 59 users
Forked by 19 users
Languages   Shell
Find elsewhere
🌐
CMake Discourse
discourse.cmake.org › usage
Linux cross-compile x64 to aarch64 - Usage - CMake Discourse
August 26, 2020 - Unless anyone can convince me of otherwise, I’ve given up on cmake being able to cross-compile on Linux from x64 to aarch64. I started this topic about it earlier on a C group: https://groups.google.com/forum/#!topic/comp.lang.c/B9msZxHI-as I think tomorrow I will take the CMakeLists.txt files, generate GNU Makefiles from them, and then dismantle the GNU Makefiles so that I have simple one-liner commands like: aarch64-linux-gnu-gcc -o interface.o -c interface.c aarch64-linux-gnu-gcc -o compu...
🌐
Kodi Forum
forum.kodi.tv › showthread.php
Cross compiling to aarch64
January 10, 2023 - Quote:make -C samba-gplv3 make[2]: Entering directory '/home/xbmc/kodi/tools/depends/target/samba-gplv3' cd aarch64-linux-gnu-debug; WAF_MAKE=1 ./buildtools/bin/waf --targets=smbclient Waf: Entering directory `/home/xbmc/kodi/tools/depends/target/samba-gplv3/aarch64-linux-gnu-debug/bin/default' Selected embedded Heimdal build [ 818/2011] Linking bin/default/source4/heimdal_build/libcom_err-samba4.so /usr/bin/ld: source4/heimdal/lib/com_err/com_err.c.129.o: in function `error_message': com_err.c.text+0x9f): undefined reference to `rep_strlcpy' /usr/bin/ld: source4/heimdal/lib/com_err/error.c.12
🌐
96Boards
96boards.org › documentation › guides › crosscompile › commandline.html
Cross Compile files on x86 Linux host for 96Boards ARM systems - 96Boards
We use a staged Install process by using the DESTDIR environment variable (below) to redirect the install step into a temporary location so we can move it into the proper cross compile location. $ git clone https://github.com/JackMitch/libsoc.git $ cd libsoc · $ autoreconf -i $ ./configure --host aarch64-linux-gnu --enable-board=<your board name> --with-board-configs $ make $ sudo make DESTDIR=/tmp/stage install
🌐
DPDK
doc.dpdk.org › guides-19.11 › linux_gsg › cross_build_dpdk_for_arm64.html
4. Cross compile DPDK for ARM64 — Data Plane Development Kit 19.11.14 documentation
git clone https://github.com/numactl/numactl.git cd numactl git checkout v2.0.13 -b v2.0.13 ./autogen.sh autoconf -i ./configure --host=aarch64-linux-gnu CC=aarch64-linux-gnu-gcc --prefix=<numa install dir> make install · The numa header files and lib file is generated in the include and lib folder respectively under <numa install dir>. ... This way is optional, an alternative is to use extra CFLAGS and LDFLAGS, depicted in Cross Compiling DPDK using Meson below.
🌐
FreeBSD
forums.freebsd.org › development › userland programming and scripting
C - How to cross-compile ARM64 binary (custom code) from x64 system | The FreeBSD Forums
June 13, 2023 - You can also bless your arm64 binaries under amd64, which allows you to run aarch64 executables while actually operating under amd64. This worked for me, for example: https://forums.freebsd.org/threads/raspberry-pi-3-and-poudriere.63470/ ... A little bit off the path but these are some things to consider: 1) use a Makefile! If nothing else it gives you the opportunity to supply configuration and target params in a more generic manner 2) set up a crossbuild toolchain on your platform such that the CC CXX CROSS_COMPILE commands are executed as $(CROSS_COMPILE)$(CC) in the Makefile 3) CROSS_COMPILE is generally the prefix name of the platform compiler of your target: such as CROSS_COMPILE=arm-gnueabi-hf- so that the Makefile sees it as arm-gnueabi-hf-cc when CROSS_COMPILE is set.
🌐
DPDK
doc.dpdk.org › guides-22.07 › linux_gsg › cross_build_dpdk_for_arm64.html
4. Cross compiling DPDK for aarch64 and aarch32 — Data Plane Development Kit 22.07.0 documentation
... c_args = ['-target', 'aarch64-linux-gnu', '--sysroot', '<cross_install_dir>/gcc-arm-9.2-2019.12-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/libc'] c_link_args = ['-target', 'aarch64-linux-gnu', '-fuse-ld=lld', '--sysroot', '<cross_install_dir>/gcc-arm-9.2-2019.12-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/libc', '--gcc-toolchain=<cross_install_dir>/gcc-arm-9.2-2019.12-x86_64-aarch64-none-linux-gnu'] Assuming the file with augmented c_args and c_link_args is named arm64_armv8_linux_clang, use the following command to cross-compile DPDK for the target machine:
🌐
Google
android.googlesource.com › platform › external › armnn › + › refs › heads › upstream-master › BuildGuideCrossCompilation.md
How to Cross-Compile Arm NN on x86_64 for arm64
CXXFLAGS="-fPIC" cmake .. -DCMAKE_C_COMPILER=/usr/bin/aarch64-linux-gnu-gcc \ -DCMAKE_CXX_COMPILER=/usr/bin/aarch64-linux-gnu-g++ \ -DFLATBUFFERS_BUILD_FLATC=1 \ -DCMAKE_INSTALL_PREFIX:PATH=$HOME/armnn-devenv/flatbuffers-arm64 \ -DFLATBUFFERS_BUILD_TESTS=0 make all install
Top answer
1 of 4
74

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
2 of 4
23

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.out https://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.

🌐
Windriver
forums.windriver.com › wind river labs › vxworks sdk
$ CROSS_COMPILE=aarch64-linux-gnu- make - VxWorks SDK - Wind River Labs Forum
December 20, 2023 - Hi, After command $CROSS_COMPILE=aarch64-linux-gnu-make, i have the problem below: UPD include/generated/timestamp_autogenerated.h HOSTCC tools/aisimage.o In file included from tools/imagetool.h:24, from tools/aisimage.c:7: include/image.h:1397:12: fatal error: openssl/evp.h: No such file or directory 1397 | # include | ^~~~~~~~~~~~~~~ compilation terminated. make[1]: *** [scripts/Makefile.host:112: tools/aisimage.o] Error 1 make: *** [Makefile:1858: t...
🌐
xkfan
xkfan.github.io › cross-toolchain › aarch64.html
Build a GCC-based cross compiler toolchain for Linux – AArch64 | xkfan
$ mkdir ${builddir}/build-gcc-stage2 $ cd ${builddir}/build-gcc-stage2 $ ${srcdir}/gcc-11.1.0/configure --prefix=${installdir} \ --build=x86_64-linux-gnu \ --host=x86_64-linux-gnu \ --target=${targetarch} \ --enable-languages=c,c++,fortran \ --enable-shared \ --enable-tls \ --disable-libsanitizer \ --disable-libmudflap \ --disable-libssp \ --disable-libquadmath \ --disable-nls \ --disable-bootstrap $ make $ make install · After this stage is finished, we are ready to use the cross toolchain. To use the cross-compiler toolchain, set the following environment: $ export CROSSGCCDIR=${installdir} $ export PATH=${CROSSGCCDIR}/bin:$PATH · Then we are ready to use aarch64-unknown-linux-gnu-gcc, aarch64-unknown-linux-gnu-g++, and aarch64-unknown-linux-gnu-gfortran.