Dies ist eine alte Version des Dokuments!


Getting started with Yocto and C/C++

This guide will go through setting up a build environment and building an x86_64 image, and adding a small „hello world“ application to it. First the application will be compiled using the SDK , then it will be added to the image directly by creating a recipe .

Setting up the build environment

Create a directory in a suitable place to store everything related to this build. This guide will assume everything is in a directory called yocto in your the home of the user ubuntu: /home/ubuntu/yocto. This folder can by anywhere on the system though.

Next, clone poky, yocto's reference distribution. At the time of writing yocto-3.0.2 is the newest release, feel free to substitute it for a newer/different version.

git clone -b yocto-3.0.2 git://git.yoctoproject.org/poky

Next, create a build directory:

mkdir build

Now we can initialize yocto using:

. poky/oe-init-build-env build/

This will populate the build directory with all necessary files and give you access to bitbake, the build system used by yocto .

Setting the machine

Setting the machine tells bitbake what hardware to build for. This build will be a 64bit qemu image, so we can set the machine to qemu86-64. This means we can run this image in Qemu.

Open local.conf

nano conf/local.conf

and add this line to set the machine:

MACHINE = "qemux86-64"

The build environment is now set up.

Building an image

You can now build an image. This command will build a minimal image provided by poky:

bitbake core-image-minimal

Once the image is built, it can be run in qemu using

runqemu qemux86-64 nographic

runqemu is a script that comes with bitbake, qemux86-64 specifies the machine, and nographic tells qemu to run in the terminal rather than its own window. This is useful when building on a system with no graphical environment such as a container or when building in the cloud.

Creating a Hello World C++ Application

This section will show you how to create a hello world application in C++ with Cmake.

First, create a directory in your yocto folder. This will contain the application itself. The location is arbitrary, and was chosen merely for convenience. It could be anywhere. Assuming you are still in the build directory:

mkdir ../helloworld

Next, create a main.cpp in the new directory

cd ../helloworld
nano main.cppp

with the following content:

#include<iostream>

int main()
{
        std::cout << "Hello World!\n";
        return 0;
}

Now create a CMakeLists.txt for cmake:

nano CMakeLists.txt

CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

project(yocto-test LANGUAGES CXX)

add_executable(hello helloworld.cpp)

install(TARGETS hello DESTINATION bin)

Optional: Verifying the application works

This is an optional step to verify the application itself is built correctly before adding it to the image. Create a build directory inside the application folder for cmake to store its data in. Then execute cmake inside that folder to create the build configuration and build the applucation by running make. Now there should be a hello executable that prints „hello world“ when executed. After confirming it works, change back to yocto/build before continuing with the next step.

mkdir build
cd build
cmake ..
make
./hello
cd ../build