Skip to content
systemcoding.dev
Embedded Linux

Cross-Compile a C Program With the Yocto SDK Environment Script

Install a Yocto SDK, source its environment script, and cross-compile a C program with the CC variable it sets, then check the result before running it.

7 min read
Summarize in AI
Flat illustration of a single-board computer with its SoC, memory, header and ports, with the C, Linux and NXP logos.

If your board runs a Yocto-built image, the easiest way to write programs for it is the SDK that Yocto builds from the same recipes. It contains a cross compiler, a copy of the board's libraries and a small shell script that points your shell at both. Run that script, and a normal gcc-style build works for the board without you naming a single path. This post walks through installing the SDK, sourcing the script, compiling a C program, and checking the binary on your computer before it goes near the board. It uses Yocto Scarthgap 5.0 and a 32-bit Arm board, and every step is something a beginner can follow in order.

What you are installing

The SDK is a single installer file, a self-extracting shell script. Someone on your team builds it with BitBake from your image recipe, so it matches the image on the board. The file name encodes what is in it:

Text
poky-glibc-x86_64-core-image-minimal-cortexa7t2hf-neon-myboard-toolchain-5.0.8.sh

Read it from left to right. poky is the distribution, glibc is the C library, x86_64 is the machine the installer runs on, core-image-minimal is the image it was generated from, cortexa7t2hf-neon is the processor tuning, and 5.0.8 is the Yocto release. If the tuning part does not match your board, stop and ask for the right SDK, because a program built for the wrong tuning may crash with an illegal instruction.

If you have the Yocto build environment yourself, you make the installer with one command:

Shell
MACHINE=myboard bitbake core-image-minimal -c populate_sdk

The installer appears under tmp/deploy/sdk/ in the build directory.

Install it

Run the installer. It asks where to put the files, or you can give the directory on the command line:

Shell
chmod +x poky-glibc-x86_64-core-image-minimal-cortexa7t2hf-neon-myboard-toolchain-5.0.8.sh
./poky-glibc-x86_64-core-image-minimal-cortexa7t2hf-neon-myboard-toolchain-5.0.8.sh -d $HOME/sdk -y
Text
Poky (Yocto Project Reference Distro) SDK installer version 5.0.8
=================================================================
You are about to install the SDK to "/home/dev/sdk". Proceed [Y/n]? Y
Extracting SDK...............................done
Setting it up...done
SDK has been successfully set up and is ready to be used.
Each time you wish to use the SDK in a new shell session, you need to source the environment setup script e.g.
 $ . /home/dev/sdk/environment-setup-cortexa7t2hf-neon-poky-linux-gnueabi

The last line is the important one, and it contains a hint that trips up beginners. You must source the script in every new terminal. The installer does not change your shell's startup files, and the settings it makes last only for the terminal where you ran it.

Source the environment script

The script sets environment variables. Run it with a leading dot, which means "run in this shell", not ./script, which would run in a child shell and discard the variables when it exits:

Shell
. $HOME/sdk/environment-setup-cortexa7t2hf-neon-poky-linux-gnueabi

Now look at what it set:

Shell
echo "$CC"
echo "$CROSS_COMPILE"
echo "$ARCH"
echo "$SDKTARGETSYSROOT"
Text
arm-poky-linux-gnueabi-gcc -mthumb -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=/home/dev/sdk/sysroots/cortexa7t2hf-neon-poky-linux-gnueabi
arm-poky-linux-gnueabi-
arm
/home/dev/sdk/sysroots/cortexa7t2hf-neon-poky-linux-gnueabi

Notice that CC is not just the compiler name. It is the compiler plus the processor flags and the --sysroot option, all in one string. That is the trick that makes the SDK easy: anything that uses $CC is automatically pointed at the right target libraries. CROSS_COMPILE and ARCH are for build systems such as the Linux kernel's, which ask for them by those names.

Write and build a program

Write a small C program that reports something about the machine it runs on, so you can tell the build worked once it is on the board:

sysinfo.cc
#include <stdio.h>
#include <sys/utsname.h>

int main(void)
{
	struct utsname u;

	if (uname(&u) != 0) {
		perror("uname");
		return 1;
	}
	printf("running on %s, machine %s\n", u.sysname, u.machine);
	return 0;
}

Compile it with $CC. Because CC holds several words, do not put it in quotes:

Shell
$CC -Wall -O2 -o sysinfo sysinfo.c
ls -l sysinfo
Text
-rwxr-xr-x 1 dev dev 8700 Nov 23 11:02 sysinfo

The size will differ. Now check what you built. This is the habit that saves the most time, and it takes one command:

Shell
file sysinfo
Text
sysinfo: ELF 32-bit LSB pie executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, BuildID[sha1]=5d0e..., for GNU/Linux 3.2.0, not stripped

ARM and 32-bit confirm that the compiler produced code for the board, not for your computer. If it had said x86-64, the environment script was not sourced in this terminal.

Copy it to the board and run it

Copy the file over the network and run it. The board's address here is an example:

Shell
scp sysinfo [email protected]:/tmp/
ssh [email protected] /tmp/sysinfo
Text
running on Linux, machine armv7l

armv7l is how the kernel reports a 32-bit Armv7 processor. The same program on your computer would print x86_64, which is the simplest proof that you are running the build you made for the board.

Use it with a Makefile

Most small projects use a Makefile that calls $(CC). After sourcing the script, make picks the right compiler from the environment with no change to the Makefile:

Makefilemakefile
CFLAGS ?= -O2 -Wall

sysinfo: sysinfo.c
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $<

clean:
	rm -f sysinfo
Shell
make
file sysinfo | cut -d, -f1-3
Text
sysinfo: ELF 32-bit LSB pie executable, ARM, EABI5 version 1 (SYSV)

A subtle point: make has a built-in default for CC of cc, but an environment variable named CC takes precedence over the built-in default, so the SDK's value wins. If a Makefile sets CC = gcc itself, that assignment overrides the environment, and the build produces a host binary. The remedy is to change it to CC ?= gcc, which only sets a default, or to run make CC="$CC", which overrides the Makefile from the command line.

Common beginner mistakes

Four problems account for nearly every first-day failure.

  1. Forgot to source the script in this terminal. echo $CC prints nothing, and file shows an x86 binary. Source it again.

  2. **Quoted $CC.** Writing "$CC" -o app app.c treats the whole string as one command name and fails with command not found. Leave it unquoted.

  3. **Used plain gcc.** gcc is still your computer's compiler. The SDK leaves it alone, and only $CC and the prefixed tools are cross tools.

  4. Mixed terminals. Sourcing the script in one terminal and building in another. The variables do not carry over.

Another one is worth knowing: the script changes PATH, so tools such as python or cmake may resolve to copies inside the SDK. Check with which:

Shell
which arm-poky-linux-gnueabi-gcc
Text
/home/dev/sdk/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc

The compiler is under the SDK's own sysroots/x86_64-pokysdk-linux directory, the "native" part that runs on your computer, separate from the target sysroot that holds the board's libraries.

When the SDK is the wrong tool

An SDK is for application developers who do not rebuild the whole system. If you are changing the kernel, the bootloader or the image itself, work in the Yocto build tree instead, and use devtool to modify recipes. And if you only need a quick program for a board that runs Debian, a distribution cross compiler is simpler than a Yocto SDK.

Build a program with a second source file and a library

A real program has more than one file and usually links a library. The SDK handles both the same way, because $CC carries the sysroot. This version splits the program into two source files and links the math library, which lives in the sysroot as libm:

stats.cc
#include <math.h>
#include <stddef.h>

double rms(const double *v, size_t n)
{
	double sum = 0.0;

	for (size_t i = 0; i < n; i++)
		sum += v[i] * v[i];
	return sqrt(sum / (double)n);
}
main.cc
#include <stdio.h>

double rms(const double *v, size_t n);

int main(void)
{
	const double samples[] = { 3.0, 4.0, 12.0 };

	printf("rms = %.3f\n", rms(samples, 3));
	return 0;
}
Shell
$CC -Wall -O2 -c stats.c main.c
$CC -o rms main.o stats.o -lm
arm-poky-linux-gnueabi-readelf -d rms | grep NEEDED
Text
 0x00000001 (NEEDED)                     Shared library: [libm.so.6]
 0x00000001 (NEEDED)                     Shared library: [libc.so.6]

The NEEDED lines list what the board must have: libm.so.6 and libc.so.6. Both are in every glibc-based image. On the board, the program prints the root mean square of the three samples. The mean of the squares is (9 + 16 + 144) divided by 3, which is 56.333, and its square root is 7.506:

Text
rms = 7.506

The step to take away is the link line: libraries go after the object files that use them, and -lm comes last. Placing -lm before the objects fails on linkers that process arguments in order, with an error about an undefined reference to sqrt.

The primary sources for this post are the Yocto SDK usage guide and Yocto variables glossary.

Share

Keep reading

Flat illustration of user space above the system call boundary, the kernel and the hardware, with the Linux, C and GNU logos.
Kernels

Write a Pin Controller Driver With pinctrl

Write a Linux 6.12 pin controller driver: pinctrl_desc, pins, groups and functions, the set_mux callback, and the device tree states that consumers select.

8 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.