Cross compile for aarch64 on Ubuntu

Alright, I figured it out. Here’s the toolchain file:

#
# CMake Toolchain file for crosscompiling on ARM.
#
# Target operating system name.
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR aarch64)

# Name of C compiler.
set(CMAKE_C_COMPILER "/usr/bin/aarch64-linux-gnu-gcc-8")
set(CMAKE_CXX_COMPILER "/usr/bin/aarch64-linux-gnu-g++-8")

# Where to look for the target environment. (More paths can be added here)
set(CMAKE_FIND_ROOT_PATH /usr/aarch64-linux-gnu)
#set(CMAKE_SYSROOT /usr/aarch64-linux-gnu)

# Adjust the default behavior of the FIND_XXX() commands:
# search programs in the host environment only.
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

# Search headers and libraries in the target environment only.
#set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
#set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
#set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE arm64)

I investigated CMAKE_LIBRARY_ARCHITECTURE, since that’s used to access libraries in /usr/lib/<arch>, thinking it hadn’t been set right. But it was set.

Reading the documentation of find_file and find_library, specifically the search strategy, clarified that it’ll search for libraries in the arch-specific directories first. It just didn’t in my case because I told it not to by setting set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY). My libs were obviously not in the root path, so this broke it all. Keeping that setting at its default BOTH, CMake looked in the right place immediately and found my aarch64 libs, and compiled for arm64 correctly.

I hope this is helpful to someone!

2 Likes