
Recall first from Cross-compilation and the build environment
- A cross compiler runs on the build machine but emits code for the host machine, named by a triplet such as aarch64-linux-gnu.
- --sysroot points the compiler and linker at the target's headers and libraries instead of the host's /usr/include and /usr/lib.
- CC, CXX, AR, CFLAGS and LDFLAGS are the environment variables every build system reads to steer compilation, assembly and linking.
Make: variables, implicit rules and DESTDIR
5 min readWith the environment variables set, the next question is how each build tool honours them. This lesson answers how Make's variables and DESTDIR and CMake's toolchain file steer a cross build.
A a file of targets, prerequisites and recipes that make reads to decide what to rebuild is mostly variables and rules. The variables that matter for C builds are conventional: CC names the compiler, CPPFLAGS holds preprocessor flags such as -I, CFLAGS holds compiler flags, LDFLAGS holds linker flags such as -L, and LDLIBS holds libraries such as -lm. GNU make's built-in a pattern rule make already knows, such as building any .o from a .c of the same name for C compiles with $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c (that is the COMPILE.c variable printed by make -p) and links, for a program built from one object, with $(CC) $(LDFLAGS) $(TARGET_ARCH) plus the objects and $(LDLIBS) (LINK.o). Note that CFLAGS is not on the link line of that rule. You can read every built-in rule with make -p, and see the exact commands for your project without running them with make -n.
That is the whole trick for a simple Make project: the rules stay, the variables change. Start with a short program:
#include <stdio.h>
int main(void)
{
if (puts("hello from the target") == EOF)
return 1;
return 0;
}
Save it as hello.c next to this Makefile, which contains no compile or link commands at all:
CC ?= gcc
CFLAGS ?= -Wall -Wextra -std=c17
prefix ?= /usr/local
hello: hello.o
hello.o: hello.c
install: hello
install -D hello $(DESTDIR)$(prefix)/bin/hello
clean:
rm -f hello hello.o
The lines hello: hello.o and hello.o: hello.c give prerequisites only; make fills in the recipes from its implicit rules. Run make -n and you will see it plans to compile with $(CC) and link with $(CC), so one variable steers both steps. This is the real output on Ubuntu 24.04 (GNU make 4.3), first with the defaults and then with make -n CC=aarch64-linux-gnu-gcc:
cc -Wall -Wextra -std=c17 -c -o hello.o hello.c
cc hello.o -o hello
aarch64-linux-gnu-gcc -Wall -Wextra -std=c17 -c -o hello.o hello.c
aarch64-linux-gnu-gcc hello.o -o hello
The first pair shows cc rather than gcc: the Makefile's CC ?= gcc does not take effect because make already defines CC as cc by default, so ?= finds it set. Use CC = gcc if you want gcc, or leave the line out.
Now the cross build. On Ubuntu 24.04, sudo apt install gcc-aarch64-linux-gnu provides the AArch64 cross compiler (GCC 13) together with a minimal target C library under /usr/aarch64-linux-gnu, enough to link a program like this one. Then:
make clean
make CC=aarch64-linux-gnu-gcc
file hello
The file output in the real run began hello: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked. A variable written on the command line, CC=..., overrides ordinary assignments inside the makefile, including := and ?=, and it is passed down to sub-makes. This is stronger than the environment. In a real test with a makefile containing CC = gcc and a recipe that prints $(CC), CC=foo make printed gcc (the makefile beat the environment), CC=foo make -e printed foo (-e makes the environment win) and make CC=bar printed bar. Only an explicit override directive in the makefile can resist the command line.
Some makefiles ignore the convention and write gcc literally inside recipes, or compute flags by running the host compiler. A command-line variable cannot fix a hardcoded name. Run make -n first and read the planned commands; if you see plain gcc, the makefile needs a patch.
Installing is the second half. The convention is that prefix is the path the program will occupy on the target, while a staging directory prepended to every install path at install time only, without changing the configured prefix redirects the copy on your host:
make install prefix=/usr DESTDIR="$PWD/stage"
find stage -type f
The binary lands at stage/usr/bin/hello, ready to be copied into a root filesystem image, while any path compiled into the program still says /usr. Never set prefix to your staging directory instead: the program would then look for its data files under a path that only exists on your host.
CMake: steering the build with a toolchain file
3 min readCMake works in two phases: configure, where it probes the compiler and searches for dependencies, and build, where it runs the generated build files. A cross build must be decided during configure, because the probes themselves must run the cross compiler. The mechanism is the a CMake script, passed with CMAKE_TOOLCHAIN_FILE, that describes the target system and compilers before the project is configured. For the same hello.c, the project is four lines:
cmake_minimum_required(VERSION 3.22)
project(hello C)
add_executable(hello hello.c)
install(TARGETS hello DESTINATION bin)
And the toolchain file, saved as aarch64.cmake:
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR aarch64)
set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)
set(CMAKE_SYSROOT "$ENV{HOME}/sysroot")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
Each line does a specific job. CMAKE_SYSTEM_NAME is the trigger: setting it tells CMake the build is a cross build, and CMAKE_CROSSCOMPILING becomes true even if the target architecture matches the host. CMAKE_SYSTEM_PROCESSOR should match what uname -m reports on the target. CMAKE_C_COMPILER (and CMAKE_CXX_COMPILER for C++) names the cross compiler; CMake detects the compiler by compiling small test programs (the real run printed Detecting C compiler ABI info - done; it printed Check for working C compiler: /usr/bin/aarch64-linux-gnu-gcc - skipped), and those tests already use the toolchain file. CMAKE_SYSROOT points at a directory that mirrors the target's root filesystem; CMake passes it to the compiler as --sysroot (in the real run, cmake --build --verbose showed aarch64-linux-gnu-gcc --sysroot=/root/sysroot ... on both the compile and the link line), and also uses it to prefix the paths searched by the find_* commands. For hello you can omit CMAKE_SYSROOT entirely: in the test, hello built and linked even though the sysroot directory was empty or missing, because Ubuntu's cross-compiler package finds its own target C library under /usr/aarch64-linux-gnu by built-in paths. The exercise below therefore leaves the line out. A real project with target libraries needs a populated sysroot.
Configure, build and stage:
cmake -B build-aarch64 -DCMAKE_TOOLCHAIN_FILE=aarch64.cmake \
-DCMAKE_INSTALL_PREFIX=/usr
cmake --build build-aarch64
file build-aarch64/hello
DESTDIR="$PWD/stage" cmake --install build-aarch64
The install step honours the DESTDIR environment variable exactly like the Make convention: files land under stage/usr/bin while the install prefix stays /usr. One caution: the toolchain file is read more than once (CMake re-reads it for compiler tests), so keep it to plain set() commands and avoid searches or side effects inside it. Also configure into a fresh build directory per toolchain; CMAKE_C_COMPILER is cached, and reusing a host build directory for a cross build does not work.
How find_* searches when cross-compiling
3 min readThe remaining question is where CMake looks for dependencies. find_path() searches for headers, find_library() for libraries, find_program() for executables, and find_package() wraps one of two strategies for finding a whole dependency. Under cross-compilation the danger is specific: your host has headers and libraries in /usr, they look perfect, and linking them into an AArch64 binary fails or, worse, silently produces a broken mix.
Going deeperExplore the mechanism
find_package(Foo) first tries module mode: it looks for a FindFoo.cmake script, shipped either by CMake itself (for classics like ZLIB) or by your project. That script is ordinary CMake code that calls find_path(Foo_INCLUDE_DIR ...) and find_library(Foo_LIBRARY ...). If no module is found, CMake falls back to config mode: it looks for FooConfig.cmake or foo-config.cmake, files installed by the library itself, usually under lib/cmake/Foo/ in its install prefix. A config file defines imported targets with the library's location and usage requirements already computed. Passing CONFIG to find_package skips module mode entirely.
The cross-compilation control is CMAKE_FIND_ROOT_PATH: a list of directories under which searches are re-rooted. With a sysroot at $HOME/sysroot, a search that would normally check /usr/include and /usr/lib instead checks $HOME/sysroot/usr/include and $HOME/sysroot/usr/lib. CMAKE_SYSROOT is automatically part of this list. Four mode variables decide which searches are re-rooted, each taking NEVER (host paths only), ONLY (re-rooted paths only) or BOTH (re-rooted first, then host): CMAKE_FIND_ROOT_PATH_MODE_PROGRAM, _LIBRARY, _INCLUDE and _PACKAGE. The standard recipe, used in the toolchain file above, is NEVER for programs, because build tools such as code generators must run on the host, and ONLY for libraries, includes and packages, because those must come from the target.
Config mode has one extra trap. A FooConfig.cmake inside the sysroot was generated when that library was cross-built, and may contain absolute paths. If the library was built with the correct final prefix and then staged into the sysroot, re-rooting repairs those paths. If the config file hardcodes paths from the machine that built it, re-rooting cannot help, and you must fix the file or the library's build. This is the same class of problem the later lesson on pkg-config and sysroot pitfalls covers for .pc files.
A concrete failure makes this real. Install the host zlib headers (sudo apt install zlib1g-dev) and configure a project that calls find_package(ZLIB REQUIRED) natively: it finds /usr/lib/x86_64-linux-gnu/libz.so. Configure the same project with the toolchain file above and an empty sysroot directory, and configure fails with Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR) (the real message of the test run, CMake 3.28.3): with _LIBRARY and _INCLUDE set to ONLY, the host zlib is invisible, which is exactly what you want. The fix is not to relax the mode to BOTH; it is to populate the sysroot with a target-built zlib. A search that succeeds against the host during a cross build is a bug you have not noticed yet.
Make and CMake cover most projects you will meet, but two more families are common in embedded source trees: Meson and Autotools. The next lesson, "Building with Meson and Autotools", shows how each of them takes the same information, a cross compiler and a sysroot, in its own format.
Key takeaways
- A variable set on the make command line overrides ordinary makefile assignments, and make's implicit rules use CC, CPPFLAGS, CFLAGS, LDFLAGS and LDLIBS
- DESTDIR is a staging prefix prepended only at install time; the configured prefix stays the target's real path
- Setting CMAKE_SYSTEM_NAME in a toolchain file marks the build as cross-compiling, even when host and target share an architecture
- CMAKE_FIND_ROOT_PATH re-roots find_* searches into the sysroot; program searches are normally set to NEVER so host tools still work
Reflect
- Explain to a colleague, in two sentences: override CC on the make command line and explain why it beats makefile and environment assignments
- Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
- What surprised you when you ran the commands yourself?
On a real board
Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.
References and further reading
- Man pagemake(1): GNU make utility to maintain groups of programs
- Man pagegcc(1): the GNU project C and C++ compiler
- Man pageenviron(7): user environment variables
Official documentation first. Links checked when the lesson was published.