What a Sysroot Is and Why Cross-Compiling Needs One
A sysroot is a directory that stands in for the target's root filesystem at build time. See what it holds, how GCC uses it, and how to confirm it is used.

When you compile a C program, the compiler quietly reads dozens of files you never name: the header for printf, the C library to link against, the startup code that runs before main. On your own computer those files are in /usr/include and /usr/lib, and they belong to your computer. When you cross-compile for another processor, that is exactly the wrong set of files. The headers describe your laptop's C library, and the library files contain x86 machine code. A sysroot is the fix: a directory on your computer that holds a copy of the files from the target's root filesystem that the build needs. This post explains what is in one, how GCC uses it, and how to see that it is actually being used, with a 32-bit Arm board and GCC 13 as the example.
The problem a sysroot solves
Think about what the compiler needs to build #include <stdio.h> into a program for an Arm board. It needs three things from the target's world, not yours:
The header files, because the sizes of types and the names of constants can differ between C libraries and architectures.
The C library itself, as Arm object code, to link the finished program against.
The startup files such as
crt1.o, which hold the code that runs beforemainand call it.
A cross compiler is a compiler that emits Arm code. It does not, by itself, contain these three things for the target. They come from the C library the board runs, built for Arm. The sysroot is a folder that gathers them in the same layout the board has, so the compiler can treat that folder as if it were the root of the target filesystem. The word means "the root of the system, as used for building".
Without a sysroot, a cross compiler falls back to searching the host's directories. It finds /usr/include/stdio.h, which compiles, and then it tries to link and finds only x86 libraries, which fails with an error about file format. Worse, some builds find a header that is close enough, compile, and produce a binary that breaks on the board. A sysroot removes the host's directories from the search entirely.
What is inside one
A sysroot mirrors the directories of a Linux root filesystem, with only the build-time files present. A typical one looks like this:
sysroot/
lib/
ld-linux-armhf.so.3
libc.so.6
libm.so.6
usr/
include/
stdio.h
stdlib.h
zlib.h
lib/
crt1.o
crti.o
libc.so
libc_nonshared.a
libz.so
pkgconfig/
zlib.pcThe include directory holds headers. The usr/lib directory holds the linker's view of each library: libc.so is a small linker script that says which real files to use, crt1.o is the startup code, and libz.so is a symlink to the real shared object, which sits at lib/libz.so.1 or in usr/lib. The dynamic loader ld-linux-armhf.so.3 is there as well, because the linker records its path in every program it builds. The pkgconfig directory holds the .pc files that describe each library.
You do not write a sysroot by hand. You get one from a toolchain tarball, a Yocto SDK, a Buildroot SDK, or by copying the relevant directories from the board's root filesystem.
How GCC uses it
You tell GCC where the sysroot is with the --sysroot option. The compiler then treats every absolute path it would search as relative to that directory: the header search for /usr/include becomes <sysroot>/usr/include, and the library search for /usr/lib becomes <sysroot>/usr/lib.
arm-linux-gnueabihf-gcc --sysroot=$HOME/sdk/sysroot -o hello hello.cSome toolchains are built with a default sysroot, so the option is not needed. Ask the compiler:
arm-linux-gnueabihf-gcc -print-sysroot/opt/arm-toolchain/arm-linux-gnueabihf/libcIf the command prints a path, the toolchain has a built-in sysroot, and the compiler uses it without an explicit option. If it prints an empty line, as the Debian and Ubuntu cross compilers do, the toolchain relies on its own system directories, and you pass --sysroot yourself for anything beyond the C library the package installs. The path above is an example from a toolchain tarball, and yours will differ.
Prove which headers were used
The best test of a cross build is not whether it compiled but which files it read. Ask the preprocessor to print its search path with -v:
echo '#include <stdio.h>' | arm-linux-gnueabihf-gcc --sysroot=$HOME/sdk/sysroot -E -v -x c - 2>&1 | sed -n '/search starts here/,/End of search/p'#include <...> search starts here:
/home/dev/sdk/sysroot/usr/include
End of search list.The directory printed is inside the sysroot. If you see /usr/include with no sysroot prefix, the host's headers are being used and the build is not safe. The second check is the linker's. Ask the compiler where it found the C library:
arm-linux-gnueabihf-gcc --sysroot=$HOME/sdk/sysroot -print-file-name=libc.so/home/dev/sdk/sysroot/usr/lib/libc.soA bare libc.so with no path means the compiler could not find it at all. A path outside the sysroot means a different library will be linked.
A sysroot is not the board's filesystem
It is easy to confuse the two, and the difference matters. The sysroot is a build-time copy, and it lives on your computer. The board's filesystem is what runs. They should agree on the C library and the libraries your program uses, but they do not need to be identical. The sysroot has headers and static linker files that the board does not need, and the board has programs and configuration the sysroot does not need.
The sysroot must match the board in one respect: the versions of the shared libraries. If you link against libz.so.1 from a sysroot with zlib 1.3.1 and the board has an older zlib, the program may call a function the board does not have. The cleanest way to keep them in step is to produce both from the same build, which is what Yocto and Buildroot do. The Yocto SDK's sysroot is generated from the image's package list. Copying a sysroot from a running board is also valid, with rsync over SSH, as long as the development packages (the headers) are present on the board when you copy.
When no sysroot is wrong
A build with no sysroot is wrong in a particular, recognizable way. The classic symptoms:
| Symptom | What happened |
|---|---|
Linker error file format not recognized or incompatible with arm | The linker found an x86 library on the host |
| Binary builds, then crashes on the board with an illegal instruction | The compiler used host headers with the wrong type sizes or macros |
fatal error: foo.h: No such file or directory | The header is on the target, but the sysroot does not contain it |
| Works on the SDK emulator, not on the board | The sysroot's library version is newer than the board's |
For the missing header, the sysroot has to be built with the development files for that library included. In Yocto, this means adding the -dev package of the library to the SDK, and in Buildroot, selecting the library package, which installs its headers into the staging directory that serves as the sysroot.
Check a built binary against the sysroot
After a build, readelf shows what the binary expects from the target. These are the facts the sysroot determined:
arm-linux-gnueabihf-readelf -l hello | grep interpreter
arm-linux-gnueabihf-readelf -d hello | grep NEEDED [Requesting program interpreter: /lib/ld-linux-armhf.so.3]
0x00000001 (NEEDED) Shared library: [libc.so.6]The interpreter path and the NEEDED library names come from the sysroot's C library. They must exist on the board at the same names. That comparison, binary against board, is the final test of whether the sysroot was the right one.
The primary sources for this post are the GCC directory options and Yocto SDK Manual.


