# Cross compile for aarch64 on Ubuntu

**URL:** https://discourse.cmake.org/t/cross-compile-for-aarch64-on-ubuntu/2161
**Category:** Usage
**Tags:** os:linux, gen:makefiles
**Created:** [November 12, 2020, 4:15am UTC](https://discourse.cmake.org/t/cross-compile-for-aarch64-on-ubuntu/2161 "2020-11-12T04:15:20Z")
**Posts on this page:** 1
**Showing post:** 10

<div class="post-metadata">

### Author: ![godfool](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/g/dfb087/32.png) [@godfool](https://discourse.cmake.org/u/godfool)
#### Post date: [November 19, 2020, 2:14pm UTC](https://discourse.cmake.org/t/cross-compile-for-aarch64-on-ubuntu/2161/10 "2020-11-19T14:14:56Z")

</div>

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

```auto
#
# 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!

---

_[View the full topic](https://discourse.cmake.org/t/cross-compile-for-aarch64-on-ubuntu/2161)._
